in reply to Re: floating point addition
in thread floating point addition

I think bignum just gives you more precision but does not eliminate the problem.

To eliminate the problem you could use bigrat and use fractions.

Compare:

perl -Mbignum -e ' printf "%20.17f\n", 20.1;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 20.1;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 20+1/10; 20.00000000000000000

Replies are listed 'Best First'.
Re^3: floating point addition
by choroba (Cardinal) on Jan 12, 2015 at 18:09 UTC
    You are right about bignum. Your bigrat example, though, doesn't work. One needs
    perl -Mbigrat -le 'print 20/1 + 1/10'
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      perl -Mbigrat -e ' printf "%20.17f\n", 20/1+1/10;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 201/10;' 20.10000000000000142
      Does not seem to work either - I don't know why not...
        Because %f converts the Math::BigRat object to "a floating-point number, in fixed decimal notation" (sprintf).
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ