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

> whereas reputable languages like JavaScript, Raku, and Python

Perl was born before these reputable languages existed. C++ outputs the same number as Perl:

#include<iostream> int main(int argc, char** argv) { std::cout << 1.4/10 << std::endl; }
Output:
0.14

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^6: [OT: JavaScript] JS remainder operation ('%')
by syphilis (Archbishop) on Jan 19, 2024 at 00:57 UTC
    C++ outputs the same number as Perl

    I think there was, and probably still is to some extent, the view that if a 15-significant-digit representation isn't a good enough approximation, then it's up to the user to provide the code that does what's wanted.
    That might be just using printf "%.17g", ..., or using a certain library of your choice.

    Interestingly, every finite double can be expressed exactly in base 10 in no more than 767 mantissa digits and, if one wants to get anal about it, one can even get to see that exact representation by doing printf("%.767g", $val).
    And that will probably work on most perls (and also C, C++), unless you've got an ancient system:
    D:\>perl -le "printf '%.767g', 0.1;" 0.1000000000000000055511151231257827021181583404541015625 and D:\>perl -le "printf '%.767g', 2 ** -1074;" 4.94065645841246544176568792868221372365059802614324764425585682500675 +50727020875186529 9836361635992379796564695445717730926656710355939796398774796010781878 +12630071319031140 4527845817167848982103688718636056998730723050006387409153564984387312 +47339727316961514 0031715385398074126238565591171026658556686768187039560310624931945271 +59149245532930545 6544401127480129709999541931989409080416563324524757147869014726780159 +35523861155013480 3526493472019379026810710749170333222684475333572083243193609238289345 +83680601060115061 6980975307834227731832924790498252473077637592724787465608477820373446 +96995336470179726 7771758512566055119913150489110145103786273816725095583738973359899366 +48099411642057026 37090279242767544565229087538682506419718265533447265625e-324
    Mind you, I don't think I've ever found a practical use for this capability.

    Cheers,
    Rob
      Perl's weirdnesses are sometimes just inherited from it's host language C.

      Choroba didn't tell us if C has the same output too, but I remember the strange rules to numify ASCII strings to integers stemming from an atoi routine in C.

      use warnings; say "12x45"+1; __END__ Argument "12x45" isn't numeric in addition 13

      It's not that Larry came up with all of this on his own.

      FWIW I don't think this warning was always there...

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

        > Choroba didn't tell us if C has the same output too

        Well, it kind of has, but it's a bit tricky as you have to explicitly use printf.

        #include <stdio.h> int main(int argc, char** argv) { printf("%f\n", 1.4/10); printf("%.17f\n", 1.4/10); return 0; }
        Output:
        0.140000 0.13999999999999999

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]