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


In reply to Re: Perl 5 numeric type and simplifications (updated) by LanX
in thread Perl 5 numeric type and simplifications by pango

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.