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