1

I am in a project of making a scientific calculator in c# (windows forms). I need to perform operations like log,sqrt,trignometric fns etc with decimal type. Is there any way to perform this? System.Math operate only on double values. thank you

  • 1
    there is a decimal datatype available in C#. check [Decimal Type](https://msdn.microsoft.com/en-us/library/system.decimal.aspx) – akhil kumar Apr 13 '15 at 04:40
  • 1
    and if `Decimal` isn't enough, see http://stackoverflow.com/a/13813535/103167 – Ben Voigt Apr 13 '15 at 04:58
  • I want to perform operations on decimal type. – Pearl .Net Apr 13 '15 at 05:25
  • is this only a toy-calculator? Don't get me wrong but usually it's no big deal to just convert to `double` and then back to `decimal` if you just want to to write your own *calc.exe* – Random Dev Apr 13 '15 at 05:36
  • it doesn't work for a number with more than 16 digits. – Pearl .Net Apr 13 '15 at 05:51
  • Who said? **Decimal has roughly 28 digit precision.** if you need more you need something else, if not... – Adriano Repetti Apr 13 '15 at 06:36
  • obviously Decimal has roughly 28 digit precision. but double doesn't. that I said. and I need functions accept decimal type as arguments. – Pearl .Net Apr 13 '15 at 07:22
  • 1
    Build you own. There most of your functions can be found in the math library of the multiprecision calculator bc, http://code.metager.de/source/xref/gnu/bc/1.06/bc/libmath.b Obviously, `scale` is fixed for the Decimal type. – Lutz Lehmann Apr 13 '15 at 08:02

1 Answers1

0

Consider using BigInteger: https://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28v=vs.110%29.aspx

Split the project so that your calculation logic works with big integer and your GUI shows a user friendly representation of it. For example:

In the logic:

BigInteger pi = new BigInteger(31415926535);

In the GUI:

string pi = (pi/10).ToString(format);

Even if you don't work with WPF or MVVM pattern, it is good to have such differences between logic and presentation.

Fleve
  • 400
  • 1
  • 5
  • 19