Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^8: All Calculations Done with One Variable Give Integer Answers

by syphilis (Archbishop)
on Sep 24, 2013 at 04:57 UTC ( [id://1055437]=note: print w/replies, xml ) Need Help??


in reply to Re^7: All Calculations Done with One Variable Give Integer Answers
in thread All Calculations Done with One Variable Give Integer Answers



New code is up in the main post. I want to understand what's going on as well.


There's no problem with "use Math::BigInt;". It makes no difference whether that line is present or not.
What does the damage is "use Math::BigInt ':constant';" because that means all integer values are converted to Math::BigInt objects or, as the docs explain it:
Autocreating constants After "use Math::BigInt ':constant'" all the integer decimal, hexadecimal and binary constants in the given scope are converted +to "Math::BigInt". This conversion happens at compile time.
Effectively, the math operations involving integers will be done using integer arithmetic - which is precisely as you have seen.

You can avoid this behaviour by simply not calling "use Math::BigInt ':constant';".
The only thing you then have to worry about is what happens when an integer value that is too big to fit into an IV comes along.
(perl -V:ivsize will tell you how many bytes your IV's can take up. It depends upon how perl was configured when it was being built and won't necessarily be the same on other builds of perl, even if they are the same version. Typically ivsize is either 4 or 8.)
If all of your integer values will fit into an IV, then there's little more to consider - just remove the offending line of code.

Cheers,
Rob

Replies are listed 'Best First'.
Re^9: All Calculations Done with One Variable Give Integer Answers
by HalNineThousand (Beadle) on Sep 24, 2013 at 06:12 UTC

    Thanks! That's a big help! I can also see if I can limit the scope to just the subroutines that need to use that. I still have to go through the entire module to see why I was using BigInt. I can't remember if it was one or two routines or a number of them.

    But why does this still have an effect on any math done with that variable even when BigInt shouldn't apply?

      In the test code you have posted IntTest.pm, if you change your constants 1900 and 60 to 1900.0 and 60.0, your problem is fixed as well. This is because use Math::BigInt ':constant'; only applies to integer constants. This could be one way to limit the scope.

        Thanks! That's a simple fix I can make.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found