Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl can't make some easy arithmetics :(

by rsFalse (Chaplain)
on Oct 24, 2014 at 20:21 UTC ( [id://1104911]=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Today I spend time to understand why do I get wrong answers, and I don't know an answer. Later I tried and found how to overcome problem: use bigint and add zeroes. See this:
while(<>){ ($a, $b)=split/ /; print $a - $b, " "; print (($a+0)-($b+0), " "); use bigint; print $a - $b, " "; print (($a+0)-($b+0), "\n"); }
STDIN: 9 7 900000000000000009 900000000000000007 STDOUT: 2 2 2 2 0 0 0 2

Replies are listed 'Best First'.
Re: rsFalse can't make some easy arithmetics :)
by LanX (Saint) on Oct 24, 2014 at 20:51 UTC
    Understanding the limitations of floating point arithmetics is a basic requirement for any programmer - no matter which language.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      From a language design perspective, what do we gain by using machine-precision floating point as our default numeric representation? Nothing, really, except that it's easy to implement. I think modern languages should follow Python's lead and switch to extended-precision formats.
        what do we gain by using machine-precision floating point as our default numeric representation?

        I just watched an HD movie on my PC. In the 105 minutes of runtime, approximately 100 billion calculations are performed.

        If these calculations were done using bigint, then the movie would take 34.5 days to watch...

        cmpthese -1, { a=>q[my $t=0; $t *= $_ for 1 .. 1e3;], b=>q[use bigint; my $t=0; $t *= $_ for 1 .. 1e3 ] };; Rate b a b 23.8/s -- -100% a 11263/s 47292% --

        I'm more than happy to have to consider the limitations of native machine precision.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        I don't know much about "Python's extended-precision formats" and I'd probably agree.

        The OP wasn't very clear about what his problem is, it looked like the usual "why is FP arithmetics not DWIM".

        As it turned out later, he has a problem with some bigint automatisms and not Perl (like his rather naive title indicated).

        Anyway - modern language or not - understanding FP's limitations is still crucial.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Perl can't make some easy arithmetics :(
by Loops (Curate) on Oct 24, 2014 at 20:52 UTC

    What version of Perl are you using?

    perl 5, version 18, subversion 4 (v5.18.4) on 64bit:

    2 2 2 2
    2 2 2 2
    
      Could it be you are on a 64 bit system?

      Perl automatically switches to floating point representation² if the integer range is exceeded¹.

      update

      Oh yes you did an update showing the bits :)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      ¹) 2**31 < 900000000000000009 < 2**63

      ²) IIRC does Perl's FP mantissa have 56 bits so 7.2e+16 ~~ 2**56  < 900000000000000009 ~~ 9e+17

      I used ideone.com for calculating. It says version is 5.16.2

        Okay, ideone.com is running on 32bit, and that is what you're running into. On my 64bit system with this input:

        9 7
        900000000000000009 900000000000000007
        99223372036854775810 99223372036854775808
        
        The output is:
        2 2 2 2
        2 2 2 2
        0 0 0 2
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104911]
Approved by Loops
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-09-19 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.