From: Boyd, Brooks D Date: Thu, 11 Sep 2003 22:38:19 +0000 (+0000) Subject: [perl #23788] int of a fraction errors X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=32969b6e12eabd4800008f3f0270114b7962c5e1;p=p5sagit%2Fp5-mst-13.2.git [perl #23788] int of a fraction errors From: "Boyd, Brooks D" (via RT) Message-ID: Add a separate faq entry for int(). (Yes, a bit redundant with the existing "why are my numbers broken" entry.) p4raw-id: //depot/perl@21204 --- diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index af08e47..e2054e4 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -28,6 +28,24 @@ L<"Floating Point Arithmetic"|perlop> for more details. my $number = sprintf "%.2f", 10/3; +=head2 Why is int() broken? + +Your int() is most probably working just fine. It's the numbers that +aren't quite what you think. + +First, see the above item "Why am I getting long decimals +(eg, 19.9499999999999) instead of the numbers I should be getting +(eg, 19.95)?". + +For example, this + + print int(0.6/0.2-2), "\n"; + +will in most computers print 0, not 1, because even such simple +numbers as 0.6 and 0.2 cannot be presented exactly by floating-point +numbers. What you think in the above as 'three' is really more like +2.9999999999999995559. + =head2 Why isn't my octal data interpreted correctly? Perl only understands octal and hex numbers as such when they occur as