Roman Elizarov
1 min readOct 23, 2017

--

I don’t need to run your example code to see that it would not produce a correct result. It adds up 10⁸ numbers up to 10⁶ each with 2 digits after the decimal point. The intermediate result in the middle of the loop is going to be on the order order 10¹⁴ with two digits after the decimal point. This requires 16 decimal digits of precision to compute correctly, while double-precision floating point has only 15.

The point of this story was not that using floating point for decimal arithemtics is easy. The point is that it is possible, if you keep in mind all the constraints.

--

--

Roman Elizarov
Roman Elizarov

Written by Roman Elizarov

Project Lead for the Kotlin Programming Language @JetBrains

No responses yet