I found out the floating point multiplication in mit-scheme is not accurate, for example,
1 ]=> (* 1991.0 0.1)
will produce
;Value: 199.10000000000002
Could you please help explain the appearance of the weird trailing number “2”?
I found out the floating point multiplication in mit-scheme is not accurate, for example,
1 ]=> (* 1991.0 0.1)
will produce
;Value: 199.10000000000002
Could you please help explain the appearance of the weird trailing number “2”?
Remember that computers are binary,
No matter how many base 2 digits you’re willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction.
In base2 1/10 is 0.0001100110011001100110011... (repeating forever)
Unfortunately this is result of binary floating point, and any language that uses the FPU will have similar results, like Python.
In [1]: 1991.0 * 0.1
Out[1]: 199.10000000000002
In [2]: 0.1 + 0.2
Out[6]: 0.30000000000000004
This is a Representation error because often decimal fractions cannot be represented exactly as binary (base 2) fractions.
Perl, C, C++, Java, Fortran, Python and scheme will all demonstrate this behavior.
This quote is from memory and so probably not quite right but it conveys the essence of the problem: "Operating on floating point numbers is like moving piles of sand: every time you do it, you lose a little sand and you get a bit of dirt" (from Kernighan and Plauger's "Elements of programming style" IIRC). Every programming language has that problem.