nicklockwood / Expression

A cross-platform Swift library for evaluating mathematical expressions at runtime
MIT License
824 stars 50 forks source link

inaccurate result for 7+6.3+5.3+4.42 #22

Closed wangjiejacques closed 5 years ago

wangjiejacques commented 5 years ago

the result of 7+6.3+5.3+4.42 is 23.02, but what it returns is 23.020000000000003, I know it's because Double is not accurate enough for this result.

nicklockwood commented 5 years ago

Okay. But since you already know the reason, what would you like me to do about it?

nicklockwood commented 5 years ago

Depending on your use case, you could possibly work around this by printing the result with fewer significant figures?

If you really need it to be precise, the only option I can think of is to use AnyExpression with NSDecimalNumber (or some BigNum library) values, but then you'd have to implement all the math operators and functions yourself as custom operators.