Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Loops (Curate)
on Oct 24, 2014 at 22:03 UTC ( [id://1104924]=note: print w/replies, xml ) Need Help??


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

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"; }

Replies are listed 'Best First'.
Re^6: Perl can't make some easy arithmetics :(
by no_slogan (Deacon) on Oct 24, 2014 at 22:19 UTC
    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
Re^6: Perl can't make some easy arithmetics :(
by LanX (Saint) on Oct 24, 2014 at 22:21 UTC
    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".

      Maybe it would be better if Perl has such magic precedence: 1) look at the operator, 2) if operator is for numerals (+,++) -> change operands to numerals ("4" -> 4 (change to bigint if used, otherwise to int), "40..(many)..0" -> 40..(many)..0 (change to bigint, if used, otherwise to int)). Now it seems that after looking to operator, if numeric, Perl change operands to int, then make operation, and later convert result to bigint (or not convert?).

        Perl already interconverts strings and numbers according to operator context in a manner similar to what you suggest. The bigint module introduces operator overloadings on top of existing Perl operators. These overloadings may or may not be optimal, but that's a discussion you must have with bigint and not with Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found