in reply to Determining when Math::BigFloat is necessary?
This C code:
produces the exact same result as this perl code:int main(void) { double d; d=1.0; while(d>0.0) { printf("%100.80e\n",d); d = d/2.0; } return 1; }
But this is completely different from this perl code, which includes a conversion to string:my $d; $d=1.0; while($d>0.0) { printf("%100.80e\n",$d); $d = $d/2.0; }
The output from the perl program with the string conversion is clearly messed up; it prints these two successive values:my $d; $d=1.0; while($d>0.0) { $d= "$d"; printf("%100.80e\n",$d); $d = $d/2.0; }
Where the C code and perl code without the string conversion has the correct result:2.38418579101562500000000000000000000000000000000000000000000000000000 +000000000000e-07 1.19209289550780998537093783879586839091757610731292515993118286132812 +500000000000e-07
It would be nice if there were a way to tell perl "don't ever convert this number to a string unless I say it's okay." Short of that, you have to either be careful to avoid string conversion, write your numerical code in XS, use BigFloat a lot, or use straight C or FORTRAN. The Inline module also makes calling C easy. C is almost as easy as perl for numerical programming anyway. The PDL module may also do a better job for what you need, especially if you can formulate your problem in terms of vectors or matrices. That way you would gain in both speed and precision!2.38418579101562500000000000000000000000000000000000000000000000000000 +000000000000e-07 1.19209289550781250000000000000000000000000000000000000000000000000000 +000000000000e-07
It should work perfectly the first time! - toma
|
|---|