Good sleuthing -- I am highly intrigued by this so I wait with bated breath for your test sample. Wanna learn wanna learn wanna learn!
| [reply] |
I am highly intrigued by this
Most likely it's standard Math::BigInt behaviour and standard Perl behaviour:
use strict;
use warnings;
use Math::BigInt;
my $x = Math::BigInt->new(17);
my $y = 70 / $x;
print $y, "\n";
$x = "$x.00";
#or:
#$x = "$x";
$y = 70 / $x;
print $y, "\n";
__END__
Outputs:
4
4.11764705882353
Cheers, Rob | [reply] [d/l] |
You're right. BigInt was used in the module - and then anything done with the variable returned from the module forces integer answers. And my problem is I'd like to find a way to override that reliably.
| [reply] |
New code is up in the main post. I want to understand what's going on as well. But I have to admit, since I'm under crunch time, I also am eager for an answer for practical reasons, too! (I really want to get this fixed so I can get a good night's sleep!)
| [reply] |
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 | [reply] [d/l] [select] |