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

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

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.

Create A New User
Node Status?
node history
Node Type: note [id://1055437]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?