in reply to Perl 5 numeric type and simplifications

> (can I rely on this behaviour of simplifying the number?)

It depends on what you think the "complicated" number is.

Many decimal fractions can't be represented without a tiny loss as binary floats.°

But print rounds the last digit(s) before output so it'll look accurate at first,

DB<80> $x = 1.100000000000; DB<81> print $x 1.1

even if it's internal representation is flawed.°

DB<84> printf '%0.16f', 1.1 1.1000000000000001

but as soon as the rounding error exceeds print's tolerance, you'll get bitten

DB<82> $x += 1.1 for 1..55; DB<83> print $x 61.6000000000001

So, yes you can mostly rely on it, if the number was never part of arithmetic operations which accumulated rounding errors.

BUT you should know that the print wasn't just cutting off trailing zeros but also rounding to achieve this.

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

update

°) Some simple rational numbers (e.g., 1/3 and 1/10) cannot be represented exactly in binary floating point, no matter what the precision is. ... Software packages that perform rational arithmetic represent numbers as fractions with integral numerator and denominator, and can therefore represent any rational number exactly....

It's an inherent problem of floating point arithmetic. Not many languages offer alternatives to avoid this "flaw" with rational numbers. (Raku seems to be one of the exceptions here). NB: But this won't help with non-rational numbers like Pi or sqrt(2).

Replies are listed 'Best First'.
Re^2: Perl 5 numeric type and simplifications
by syphilis (Archbishop) on May 07, 2021 at 05:19 UTC
    ... even if it's internal representation is flawed

    It would be better to instead say "even if it's internal representation is rounded." Rounding is not a flaw - unless, of course, the rounding was done in an incorrect manner.

    ... part of arithmetic operations which accumulated rounding errors

    Similarly, I would rewrite that as (something like) "part of arithmetic operations which were rounded".

    Just to be clear - both "flawed" internal representations and "rounding errors" are very rare in perl. I think both of those phrases should be reserved to apply to cases where rounding has been done incorrectly.
    If those errors are encountered in perl-5.30.0 or later then I'd recommend reporting them.

    On USE_QUADMATH builds of perl, some sqrt() operations round incorrectly (by one unit of least precision). And, with USE_LONGDOUBLE builds of perl on Windows there's a specific range of values that don't assign correctly.
    There's also some BSD and Solaris (and perhaps other) systems around that have some pretty weird rules regarding USE_LONGDOUBLE arithmetic. (I think that they are "flawed", though it might be arguable.)
    Offhand, I struggle to recall there being any flawed internal representations or rounding errors on perl-5.30 or later when perl's NV is "double".

    Cheers,
    Rob
      What Every Computer Scientist Should Know About Floating-Point Arithmetic

      • Rounding Error
      ... given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation. ...

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

      Did I say it's a Perl problem?

      I learned the terms "rounding error" and "accumulated rounding error" in CS classes at university for these effects. °

      To be sure it's not a translation problem, I searched and found (among many others) this English Wikipedia article

      > Rounding error

      A roundoff error, also called rounding error, is the difference between the result produced by a given algorithm using exact arithmetic and the result produced by the same algorithm using finite-precision, rounded arithmetic.

      Rounding errors are due to inexactness in the representation of real numbers and the arithmetic operations done with them.

      So what exactly is your problem now?

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

      update

      °) actually I think I already learned it in school in physics classes. The concept is older than CS. I suppose it was introduced a century earlier by Carl Friedrich Gauß.

        A roundoff error, also called rounding error, is the difference between the result produced by a given algorithm using exact arithmetic and the result produced by the same algorithm using finite-precision, rounded arithmetic.

        I didn't realize this was a CS forum. (For some reason I thought it was a perl discussion forum ;-)
        Still, I now better understand where you're coming from.

        If you can now just show us where/how this jargon implies that any of these so-called "errors" should be deemed to be a "flaw", then you'll have presented your case most admirably.

        So what exactly is your problem now?

        Hmmm ... for the moment, my main all-consuming problem is that I'm mortal, and will one day no longer continue to exist :-(
        On the plus side, however, I'm well aware that this problem will go away at some point in the future. (Yay !!!)

        Cheers,
        Rob