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

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...

Replies are listed 'Best First'.
Re^5: floating point addition
by choroba (Cardinal) on Jan 12, 2015 at 22:49 UTC
    Because %f converts the Math::BigRat object to "a floating-point number, in fixed decimal notation" (sprintf).
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      But this remains strange to me:
      perl -e 'print 201/10 == 20.10000000000000000 ? "eq" : "ne"' eq perl -Mbigrat -e 'print 201/10 == 20.10000000000000000 ? "eq" : "ne"' eq perl -e 'print 201/10 == 20.10000000000000142 ? "eq" : "ne"' eq perl -Mbigrat -e 'print 201/10 == 20.10000000000000142 ? "eq" : "ne" +' ne
      The last two cases is where one can see the difference that bigrat makes, but should the comparison in the second case not yield false?
        There's no 20.10000000000000142 under bigrat:
        $ perl -MO=Deparse -Mbigrat -le '$x = 201/10; $y = 20.1000000000000014 +2; print join " ", $x, $y, $x == $y ? "eq" : "ne"' BEGIN { $/ = "\n"; $\ = "\n"; } use bigrat; BEGIN { $^H{'float'} = q(CODE(0x6003db168)); $^H{'integer'} = q(CODE(0x600495668)); $^H{'binary'} = q(CODE(0x6004a8d28)); $^H{'bigrat'} = q(1); } $x = {_d => [10], _n => [201], sign => '+'}; $y = {_e => [17], _es => '-', _m => ['000000142', '010000000', '2'], s +ign => '+'}; print join(' ', $x, $y, $x == $y ? 'eq' : 'ne'); -e syntax OK
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ