Click here to Skip to main content
15,888,033 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I think of using double, but seemingly not the double test

As windows :2/3 = 0.66666666666666666666666666666667 like that can not be in the double
Posted
Comments
[no name] 9-Apr-11 13:04pm    
The 15,000 other calculator renditions aren't enough for you?
Sergey Alexandrovich Kryukov 10-Apr-11 20:46pm    
No, because the accuracy is greater then FPU can make. Please see my Answer.
--SA
Sandeep Mewara 9-Apr-11 13:07pm    
Tried to Google and see? I guess this is one of the most common assignment/homework given.
Sergey Alexandrovich Kryukov 10-Apr-11 20:47pm    
Almost no C# calculator can help, because the accuracy is greater then FPU can make. Please see my Answer.
--SA
PEIYANGXINQU 9-Apr-11 13:07pm    
Thank you.No,I just wonder how they do that

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
 
Share this answer
 
v5
Comments
PEIYANGXINQU 10-Apr-11 3:17am    
using System.Numerics.BitInteger (only available in the .NET Framework v.4)??Does it provide ready-made class?
Maybe we can write a class,but I think is not simple
Sergey Alexandrovich Kryukov 10-Apr-11 20:09pm    
See my update. For fixed-precision, it's almost ready to use. Floating point will need effort.
--SA
Espen Harlinn 10-Apr-11 6:41am    
Good points, 5ed!
Sergey Alexandrovich Kryukov 10-Apr-11 20:08pm    
Thank you, Espen. I updated the Answer with fixed-precision variant. It can directly use BigInteger and if very simple.
--SA
You can google or bing for "Calculator Precision" to get lots of good results. Here are some good links that you can start with:

http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic[^]

http://support.microsoft.com/kb/125056[^]

http://www.thimet.de/calccollection/Calc-Precision.html[^]

http://www.fractal-landscapes.co.uk/bigint.html[^]
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 6:42am    
Google-fu is an essential skill, 5ed!
Sergey Alexandrovich Kryukov 10-Apr-11 20:06pm    
This is a good reply and useful references, my 5.
However, I don't any ready-to-use unlimited (or high) precision floating-point library, as OP wanted. Do you know any?
Also, I added some detail on my idea based on using BigInteger, please see.
--SA
My 2ct's:
Math.Net Project[^]

As long as it's available, why settle for something less :)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 10-Apr-11 9:20am    
Good link, Espen. Gets my 5.
Espen Harlinn 10-Apr-11 9:29am    
Thanks Nishant!
Sergey Alexandrovich Kryukov 10-Apr-11 20:12pm    
This is the most interesting reference, thank you very much and 5++++++++.

Could you quickly tell us (before I dig into it): are there unlimited or high precision numeric types there? How about floating point calculations of unlimited or high precision?

Thank you.
--SA
Espen Harlinn 11-Apr-11 15:08pm    
Thanks, SAKryukov!
Sergey Alexandrovich Kryukov 10-Apr-11 22:57pm    
Yes, I only found arbitrary precision Natural. It can be used if Framework v.4.0 cannot be used.
It can be used for implementation of fixed-point fractional numbers (which is very easy) or floating-point using the idea in my Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900