Floating Point Issues with Math::BigFloat 
Author Message
 Floating Point Issues with Math::BigFloat

Hello -

I have been running into rounding errors summing up lists of numbers,
and have turned to the Math::BigFloat module for help.  However, the
accuracy with this module doesn't seem to be any better than working
with regular floating point numbers.  Am I just being too picky, asking
for more accuracy than I am entitled to, or am I actually using this
module incorrectly?

Thanks in advance for the help!

- Pete Cion

Here is the result:

Here is 10 (reg_number) divided by 3:
3.33333333333333348136306995002087205648422241210938
Here is 10. (big_float) divided by 3:
3.33333333333333348136306995002087205648422241210938

(Identical to 50 decimal places)

Here is the code:

     1  #!/usr/local/bin/perl
     2
     3  use Math::BigFloat;
     4
     5  $reg_number = 10.0;
     6
     7  $big_float = new Math::BigFloat ("10.0000000000 0000000000
0000000000 0000000000 0000000000");
     8
     9  printf("Here is $reg_number (reg_number) divided by 3: %.50f\n",
$reg_number/3);
    10
    11  $new_float=$big_float->fdiv("3.0",50);
    12  printf("Here is $big_float (big_float) divided by 3:
%.50f\n",$new_float );

- PC



Sat, 23 Dec 2000 03:00:00 GMT  
 Floating Point Issues with Math::BigFloat

Quote:

> I have been running into rounding errors summing up lists of numbers,
> and have turned to the Math::BigFloat module for help.
 [...]
> Am I just being too picky, asking
> for more accuracy than I am entitled to, or am I actually using this
> module incorrectly?
> Here is 10 (reg_number) divided by 3:
> 3.33333333333333348136306995002087205648422241210938

A single precision number has something like 6.x decimal
digits of precision, and a double has something like 17
decimal digits of precision.  To 17 digits, your answer is
3 + 1/3, which is what is expected.  Everything beyond that
is noise coming from how the library prints numbers.

Quote:
> Here is 10. (big_float) divided by 3:
> 3.33333333333333348136306995002087205648422241210938

I suspect that you are not dividing like quantities here.

Quote:

> (Identical to 50 decimal places)

> Here is the code:

>      1  #!/usr/local/bin/perl
>      2
>      3  use Math::BigFloat;
>      4
>      5  $reg_number = 10.0;
>      6
>      7  $big_float = new Math::BigFloat ("10.0000000000 0000000000
> 0000000000 0000000000 0000000000");

Never used this package, but do you need spaces here?

Quote:
>      8
>      9  printf("Here is $reg_number (reg_number) divided by 3: %.50f\n",
> $reg_number/3);

I'd prefer to see $reg_number / 3.0, otherwise perl must
first convert the integer to double, and then do the division.

Quote:
>     10
>     11  $new_float=$big_float->fdiv("3.0",50);
>     12  printf("Here is $big_float (big_float) divided by 3:
> %.50f\n",$new_float );

> - PC

I suspect the problem here is the conversion of "3.0" to
a big float.  Why not specify 3.0 the same way you specified
10.0 above?

Gordon Haverland



Sat, 23 Dec 2000 03:00:00 GMT  
 Floating Point Issues with Math::BigFloat

Quote:

> I have been running into rounding errors summing up lists of
> numbers, and have turned to the Math::BigFloat module for help.
> However, the accuracy with this module doesn't seem to be any better
> than working with regular floating point numbers.  Am I just being
> too picky, asking for more accuracy than I am entitled to, or am I
> actually using this module incorrectly?

> Thanks in advance for the help!
>     12  printf("Here is $big_float (big_float) divided by 3:
> %.50f\n",$new_float );
> Here is 10. (big_float) divided by 3:
> 3.33333333333333348136306995002087205648422241210938

I suspect it's `printf's internal conversion to a float before
printing that's the problem.

using `print "$big_float\n"' prints the correct value:

+33333333333333333333333333333333333333333333333333E-49

--
  Ken MacLeod



Sat, 23 Dec 2000 03:00:00 GMT  
 Floating Point Issues with Math::BigFloat


Quote:
>      9  printf("Here is $reg_number (reg_number) divided by 3: %.50f\n",
> $reg_number/3);

While the bigfloat module is designed to handle these numbers, printf
isn't. I think that that might be your problem.

If I had it installed, I'd check.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    |
Commercial Dynamics Pty. Ltd.       | Curiouser and curiouser, said Alice.
NSW, Australia                      |



Mon, 25 Dec 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Perl's brain-dead floating point math

2. Question concerning Math::MatrixReal / Floating Point

3. Floating point math errors

4. Question about floating point math w/perl

5. floating point issue?

6. rounding bug in Math::BigFloat?

7. Math::BigFloat

8. Math::BigFloat::fcmp Bug?

9. Serious problem in Math::BigFloat or BigInt?

10. Help with Math::BigFloat si vous plait

11. Math::BigFloat question Please.

12. Math::BigFloat fsqrt

 

 
Powered by phpBB® Forum Software