in reply to Re^3: [OT: JavaScript] JS remainder operation ('%')
in thread [OT: JavaScript] JS remainder operation ('%')

> Man, this is weird! I have no idea what's going on.

When using print perl will "smoothen" the last digits of a floating point in order to "hide" potential rounding errors from previous operations.

This has been discussed here many times. Dunno if it's documented.

printf can be used to show the real content, of course only a binary format will be always fully correct.

$a = 1 - 1e-16; say $a; printf "%.16f",$a; __END__ 1 0.9999999999999999

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Re^4: [OT: JavaScript] JS remainder operation ('%') (print's float rounding)
  • Download Code

Replies are listed 'Best First'.
Re^5: [OT: JavaScript] JS remainder operation ('%') (print's float rounding)
by LanX (Saint) on Jan 18, 2024 at 13:02 UTC
    > When using print perl will "smoothen" the last digits of a floating point

    > Dunno if it's documented.

    Can't find it, shouldn't it be mentioned in print ?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Can't find it, shouldn't it be mentioned in print ?

      Sounds like the logical spot for it. (Maybe we're still trying to come up with a justification ;-)

      I recall reading (somewhere) that 15 digits was chosen because that's the value of float.h's DBL_DIG - and DBL_DIG is described as "DBL_DIG specifies the number of digits of precision of a double".
      So you can see that someone could easily get sucked into thinking that 15 digits was the smart thing to do.

      If you go beyond 15 digits, then you hit the case that 2 different 16-digit numbers can have the same value:
      >perl -le "print 'ok' if 9.999999999999999 == 9.999999999999998;" ok
      That's 2 different 16 digit numbers with the same value. Therefore the "number of digits of precision of a double" must be less than 16.
      But you won't get 2 different 15-digit numbers with the same value.

      Cheers,
      Rob