in reply to Behaviour of int() unexpected

But why does it round 895.000000 to 894?

As others have explained, it's not actually doing that.

I just wanted to point out that what you get depends upon the precision of perl's NV (floating point type).
If perl's NV is the 64-bit precision long double, then you'll get what you expected:
D:\>perl -le "$x = 8.95 * 100; print int($x);" 895
Otherwise you get what you did not expect:
D:\>perl -le "$x = 8.95 * 100; print int($x);" 894
(For some precisions, the closest binary approximation of 8.95 is less than 8.95, for other precisions it's greater than 8.95.)
To determine perl's NV type, just run:
D:\>perl -V:nvtype nvtype='long double';
Cheers,
Rob

Replies are listed 'Best First'.
Re^2: Behaviour of int() unexpected
by pryrt (Abbot) on Mar 10, 2025 at 23:47 UTC
    syphilis said,
    64-bit precision long double

    I checked again, just to make sure:

    C:\usr\local\share\github\notepad-plus-plus>perl -V:nvtype nvtype='double'; C:\usr\local\share\github\notepad-plus-plus>perl -V:nvsize nvsize='8';

    Double is the 64 bit (8 byte) precision.

    "Long double" doesn't seem to have much consistentency in the WP article (80, 96, or 128 bits) (update: though re-reading, I now see that GNU C seems to have chosen 80bit, so that's what I'm now guessing your nvsize will show). I don't have access to a perl with nvtype='long double', so could you double-check the nvsize on your "long double" version?

      could you double-check the nvsize on your "long double" version?

      Yes, it's the 80-bit extended precision long double - 1 bit for the sign, 15 bits for the exponent, and 64 bits for the mantissa.
      If it were the IEEE-754 long double, then the one-liner would have returned 894, as for "double" and "__float128".

      If you want to find out which of the various "long double" formats is being honored by your perl, then (assuming perl is not more than about 10 years old) you can run perl -V:longdblkind, which will return a value in the range -1..9.
      The meaning of the returned value is explained in the Config docs:
      "longdblkind" From d_longdbl.U: This variable, if defined, encodes the type of a long double: 0 = double, 1 = "IEEE" 754 128-bit little endian, 2 = "IEEE" 754 128-bit big endian, 3 = x86 80-bit little endian, 4 = x86 80-bit big endian, 5 = double-double 128-bit little endian, 6 = double-double 128-bit big endian, 7 = 128-bit mixed-endian double-double (64-bit LEs in "BE"), 8 = 128-bit mixed-endian double-double (64-bit BEs in "LE"), 9 = 128-bit "PDP"-style mixed-endian long doubles, -1 = unknown format +.
      Cheers,
      Rob