Search code examples
floating-pointcasmaxima

maxima CAS: float rat replaced


in Maxima I do:

(%i1) 1.4*28;

(%o1) 39.2

(%i2) is(1.4*28=39.2);

(%o2) false

This is strange to me, but probably has to do with rat replace?

Is there a way to let maxima return 'true' to the input of is(1.4*28=39.2);?


Solution

  • From The Floating-Point Guide:

    Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

    Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

    When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

    In your case, both 1.4 and 39.2 are not exactly representable as a binary fraction and the result of the computation ends up being rounded differently than the literal 39.2.

    If you want to avoid such issues, you'll have to avoid the use of binary floats. I think in Maxima, this is most easily done by using proper fractions:

    is(14/10 * 28 = 392/10)
    

    should work