fiddler42 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, All,

I am struggling with a number precision problem. Ideally, I would like to see every number that is calculated in my script (through division, multiplication, whatever...) automatically limited to 4 numbers to the right of the decimal point. According to this:-

http://perldoc.perl.org/Math/BigFloat.html

I should be able to manage that requirement through this command:-

Math::BigFloat->precision(-4);

...but I cannot get this to work at all. Has anyone tried to define a global setting like this before? If you didn't use Math::BigFloat, what was the alternative?

A simple example is below. Note that a *lot* of calculations are being exercised in the real script, so I would really like to get a global setting working.

use Math::BigFloat; Math::BigFloat->precision(-4); for ($m = 1; $m <= 3; $m++) { for ($n = 1; $n <= 3; $n++) { $x = $m * $n * rand(3); print ("$x "); } print ("\n"); }
Thanks,

Fiddler42

Replies are listed 'Best First'.
Re: Math::BigFloat question (precision)
by syphilis (Archbishop) on May 09, 2008 at 12:37 UTC
    Hi fiddler42,

    Try:
    use Math::BigFloat; Math::BigFloat->precision(-4); for ($m = 1; $m <= 3; $m++) { for ($n = 1; $n <= 3; $n++) { my $x = Math::BigFloat->new($m * $n * rand(3)); print $x, " "; } print ("\n"); }
    The Math::BigFloat precision that you specify applies only to Math::BigFloat objects.

    Cheers,
    Rob
Re: Math::BigFloat question (precision)
by Anonymous Monk on May 09, 2008 at 12:33 UTC
    $x is not a Math::BigFloat, 'use bignum;'