FAQ
cvsuser 03/09/19 23:33:26

Modified: . perlfaq4.pod
Log:
Change 21204 by jhi@kosh on 2003/09/13 06:47:23

Subject: [perl #23788] int of a fraction errors
From: "Boyd, Brooks D" (via RT) <perlbug-followup@perl.org>
Date: 11 Sep 2003 22:38:19 -0000
Message-ID: <rt-23788-64511.8.7546744242259@rt.perl.org>

Add a separate faq entry for int(). (Yes, a bit redundant
with the existing "why are my numbers broken" entry.)

Affected files ...

... //depot/perl/pod/perlfaq4.pod#86 edit

Revision Changes Path
1.48 +19 -1 perlfaq/perlfaq4.pod

Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -w -r1.47 -r1.48
--- perlfaq4.pod 25 Aug 2003 05:58:04 -0000 1.47
+++ perlfaq4.pod 20 Sep 2003 06:33:25 -0000 1.48
@@ -1,6 +1,6 @@
=head1 NAME

-perlfaq4 - Data Manipulation ($Revision: 1.47 $, $Date: 2003/08/25 05:58:04 $)
+perlfaq4 - Data Manipulation ($Revision: 1.48 $, $Date: 2003/09/20 06:33:25 $)

=head1 DESCRIPTION

@@ -27,6 +27,24 @@
printf "%.2f", 10/3;

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?

Search Discussions

  • Robert Spier at Sep 20, 2003 at 6:37 am
    cvsuser 03/09/19 23:37:43

    Modified: . perlfaq4.pod
    Log:
    From: Iain Truskett <spoon@cpan.org>
    To: perlfaq-workers@perl.org
    Subject: [PATCH perlfaq4] 'is a number?' updated
    Date: Sun, 14 Sep 2003 00:13:29 +1000

    [1 <text/plain; us-ascii (7bit)>]
    Update to reflect the inclusion of Scalar::Util into core.

    Style note: I note the FAQ takes the correct approach and
    calls it 'the CPAN'. Contemporary usage has it as just
    'CPAN'. I assume 'correct' is favoured over 'popular'?

    Revision Changes Path
    1.49 +12 -8 perlfaq/perlfaq4.pod

    Index: perlfaq4.pod
    ===================================================================
    RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
    retrieving revision 1.48
    retrieving revision 1.49
    diff -u -w -r1.48 -r1.49
    --- perlfaq4.pod 20 Sep 2003 06:33:25 -0000 1.48
    +++ perlfaq4.pod 20 Sep 2003 06:37:43 -0000 1.49
    @@ -1,6 +1,6 @@
    =head1 NAME

    -perlfaq4 - Data Manipulation ($Revision: 1.48 $, $Date: 2003/09/20 06:33:25 $)
    +perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date: 2003/09/20 06:37:43 $)

    =head1 DESCRIPTION

    @@ -2008,13 +2008,17 @@
    if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
    { print "a C float\n" }

    -You can also use the L<Data::Types|Data::Types> module on
    -the CPAN, which exports functions that validate data types
    -using these and other regular expressions, or you can use
    -the C<Regexp::Common> module from CPAN which has regular
    -expressions to match various types of numbers.
    +There are also some commonly used modules for the task.
    +L<Scalar::Util> (distributed with 5.8) provides access to perl's
    +internal function C<looks_like_number> for determining
    +whether a variable looks like a number. L<Data::Types>
    +exports functions that validate data types using both the
    +above and other regular expressions. Thirdly, there is
    +C<Regexp::Common> which has regular expressions to match
    +various types of numbers. Those three modules are available
    +from the CPAN.

    -If you're on a POSIX system, Perl's supports the C<POSIX::strtod>
    +If you're on a POSIX system, Perl supports the C<POSIX::strtod>
    function. Its semantics are somewhat cumbersome, so here's a C<getnum>
    wrapper function for more convenient access. This function takes
    a string and returns the number it found, or C<undef> for input that
    @@ -2037,7 +2041,7 @@

    sub is_numeric { defined getnum($_[0]) }

    -Or you could check out the L<String::Scanf|String::Scanf> module on the CPAN
    +Or you could check out the L<String::Scanf> module on the CPAN
    instead. The POSIX module (part of the standard Perl distribution) provides
    the C<strtod> and C<strtol> for converting strings to double and longs,
    respectively.
  • Robert Spier at Sep 21, 2003 at 5:59 am
    cvsuser 03/09/20 22:59:37

    Modified: . perlfaq4.pod
    Log:
    Some cleanups from p5p

    Revision Changes Path
    1.50 +5 -4 perlfaq/perlfaq4.pod

    Index: perlfaq4.pod
    ===================================================================
    RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
    retrieving revision 1.49
    retrieving revision 1.50
    diff -u -w -r1.49 -r1.50
    --- perlfaq4.pod 20 Sep 2003 06:37:43 -0000 1.49
    +++ perlfaq4.pod 21 Sep 2003 05:59:37 -0000 1.50
    @@ -1,6 +1,6 @@
    =head1 NAME

    -perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date: 2003/09/20 06:37:43 $)
    +perlfaq4 - Data Manipulation ($Revision: 1.50 $, $Date: 2003/09/21 05:59:37 $)

    =head1 DESCRIPTION

    @@ -243,7 +243,7 @@

    $bin = sprintf("%b", 3735928559);

    -Using unpack;
    +Using unpack:

    $bin = unpack("B*", pack("N", 3735928559));

    @@ -2064,8 +2064,9 @@
    =head2 How do I print out or copy a recursive data structure?

    The Data::Dumper module on CPAN (or the 5.005 release of Perl) is great
    -for printing out data structures. The Storable module, found on CPAN,
    -provides a function called C<dclone> that recursively copies its argument.
    +for printing out data structures. The Storable module on CPAN (or the
    +5.8 release of Perl), provides a function called C<dclone> that recursively
    +copies its argument.

    use Storable qw(dclone);
    $r2 = dclone($r1);

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcvs-perlfaq @
categoriesperl
postedSep 20, '03 at 6:33a
activeSep 21, '03 at 5:59a
posts3
users1
websitelearn.perl.org...

1 user in discussion

Robert Spier: 3 posts

People

Translate

site design / logo © 2018 Grokbase