Sure, it cannot be
double
. Too many significant digits.
None of the Intel floating point types provide such precision.
The best precision if provided by the type extended, which is a 80-bit type (double is 64 bits), but even
extended
supports about 19 digits of mantissa. This type in not supported by .NET.
Small hand calculators use different model of real numbers and calculations called digit-by-digits.
With Intel, better precision can be provided by software simulation of bigger floating point type.
I think the base way of doing so can be developed using
System.Numerics.BigInteger
(only available in the .NET Framework v.4). This is not a very simple task.
[EDIT]
Answering a follow-up question on my idea to use
System.Numerics.BigInteger
.
This is of course a ready-to-use class, not the different one, just to support integer calculations with unlimited precision. This class is just great.
There is nothing which would implement calculation model of floating-point calculation with unlimited (or just high but limited precision).
My idea is not easy to implement, will need essential effort, it just better than doing it from scratch. You can use a structure with couple of
BigInteger
fields in it, one representing mantissa, another one — the exponent field (or 10 or 2, depending on choice of design). You need to define set of operations on this structure, conversions from and to string using formats, etc.
You can find the basic directions on how to represent the emulated floating-point carry out calculations here:
http://en.wikipedia.org/wiki/Floating_point[
^].
The requirement of having non-standard precision (for the CPU) is very unusual, so it can be difficult to find existing codes. Maybe, some codes could be inherited from the old-days codes when FPU were rare.
Again, the problem is solvable, but not really easy.
As a simple alternative, you can use
BigInteger
as a fixed-precision fractional. This is very simple. Just add a field "number of decimal places" and do all calculation with integers. Assume in the final result the decimal point is moved to left on appropriate number of positions.
—SA