DDMathParser icon indicating copy to clipboard operation
DDMathParser copied to clipboard

Doubles are imprecise

Open nico75005 opened this issue 10 years ago • 6 comments

Hi, first thanks for this great library but I have a problem which actually seems well known with NSNumber.

When I evaluate a simple string through evaluateString like "1.2*6", the result is as expected 7.2 but when I call stringValue on this NSNumber(double), I get "7.199999999999999" or 7.1999999999999993 if I use doubleValue.

How can I do?

Thanks for your help

nico75005 avatar Mar 21 '15 23:03 nico75005

Well I just saw you added usesHighPrecisionEvaluation. It seems to fix the issue with stringValue but doesn't fix it with doubleValue

nico75005 avatar Mar 21 '15 23:03 nico75005

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Short of writing my own infinite-precision math framework, I'm not sure there's a way to "solve" this.

davedelong avatar Sep 19 '15 14:09 davedelong

What about porting/wrapping one of the infinite-precision math framework written in C/C++?

I have been toying with the idea every since you started the Swift branch. Someone just needs to find the time. ;)

One of the candidates is MAPM.

JanX2 avatar Sep 20 '15 10:09 JanX2

I would love to see an infinite-precision implementation here. In testing, I'm seeing: 45.25 - 45 = 0.230000000000004. Any easy fix?

joshdistler avatar Nov 10 '17 00:11 joshdistler

No, this is not an easy fix. I'm waiting to implement this until Swift has a standardized "BigNum" library.

I've thought about making Expression generic with respect to its underlying number type, but so far there's too much up and down the stack that depends on having a concrete number.

davedelong avatar Nov 10 '17 00:11 davedelong

Thanks Dave. Definitely looking forward to this very much as it's the only weakness in your otherwise amazing library.

joshdistler avatar Nov 10 '17 01:11 joshdistler