Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Perl can't make some easy arithmetics :(

by Loops (Curate)
on Oct 24, 2014 at 21:19 UTC ( #1104921=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl can't make some easy arithmetics :(
in thread Perl can't make some easy arithmetics :(

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
  • Comment on Re^3: Perl can't make some easy arithmetics :(

Replies are listed 'Best First'.
Re^4: Perl can't make some easy arithmetics :(
by rsFalse (Chaplain) on Oct 24, 2014 at 21:31 UTC
    Seems that there is a problem with magic: if use bigint and take some string, Perl doesn't convert correctly string to bigint?

      Yes, unless one of the variables has already been coerced to be a bigint, so even this is enough:

      use bigint; while(<>) { my ($a, $b)= split/ /; $a += 0; print $a - $b, "\n"; }

      However you can just coerce everything when you first read them in as strings:

      use bigint; while(<>) { my ($a, $b)= map { 0+$_ } split/ /; print $a - $b, "\n"; }
        To explain further: use bigint only causes numeric literals to become bigints. It does not change the meaning of operators (like -), no matter what the perldoc might lead you to believe. 0 is a literal, and becomes a bigint. $a and $b are strings, and do not become bigints unless you add 0 to them. Just saying "$a-$b" does not trigger bigint conversion.

        Edit: Here's a quick demonstration:

        > perl -le 'use bigint; print 22/7; print "22"/"7"' 3 3.14285714285714
        I have no time to look into the details but this looks like a flaw in the design.

        According to it's POD is bigint overloading operators, so why restricting this to integers only?

        Maybe it's because of a restriction in the overload mechanics but as I said I have no time to look into the details .... (and this place is full of honorable monks willing to answer it! ;)

        Cheers Rolf

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

        update

        ) to elaborate further: Perl tries hard not to distinguish between different types of scalars, that's one of the reasons why it has a nummeric add + and a string concat . where languages like JS only have + for both.

        But bigint seems to break this "paradigm".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-12-09 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (37 votes). Check out past polls.

    Notices?