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

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

by rsFalse (Chaplain)
on Oct 24, 2014 at 21:31 UTC ( #1104922=note: print w/replies, xml ) Need Help??


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

Seems that there is a problem with magic: if use bigint and take some string, Perl doesn't convert correctly string to bigint?
  • Comment on Re^4: Perl can't make some easy arithmetics :(

Replies are listed 'Best First'.
Re^5: Perl can't make some easy arithmetics :(
by Loops (Curate) on Oct 24, 2014 at 22:03 UTC

    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".

        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?).

Log In?
Username:
Password:

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

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











    Results (38 votes). Check out past polls.

    Notices?