Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by HalNineThousand (Beadle)
on Sep 23, 2013 at 23:27 UTC ( [id://1055404]=note: print w/replies, xml ) Need Help??


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

I think I've found the culprit, but I will be providing code this evening (USA East Coast time). This is part of a big project (bookkeeping, not programming) that I'm on a deadline on. I put together a sloppy and quick test program I'll tidy up for posting here. It seems the issue is one module used Math::BigInt. I'll include new information when I add the code tonight.
  • Comment on Re^5: All Calculations Done with One Variable Give Integer Answers

Replies are listed 'Best First'.
Re^6: All Calculations Done with One Variable Give Integer Answers
by marinersk (Priest) on Sep 23, 2013 at 23:42 UTC
    Good sleuthing -- I am highly intrigued by this so I wait with bated breath for your test sample. Wanna learn wanna learn wanna learn!
      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
        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.
      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!)


        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found