Re: Storing more decimal places in a variable
by Zaxo (Archbishop) on Dec 15, 2004 at 23:35 UTC
|
| [reply] |
|
|
Thank you very much :P
now just to nag my webserver to install it...
and a quick question on syntax for Math::BigFloat :
first of all, if I say "Math::BigFloat->precision(5);" it will only apply to results from "$x->bmul($y);" etc., correct?
thanks again for your prompt help :P
| [reply] |
|
|
The docs say "All operators (inlcuding basic math operations) are overloaded", which means it should work work *, +, etc. (provided one of the operands is of class Math::Float). Try and find out.
| [reply] |
|
|
use Math::BigFloat;
Math::BigFloat->accuracy(200);
$x = Math::BigFloat->new(2);
print $x->bsqrt();
-- All code is 100% tested and functional unless otherwise noted.
| [reply] [d/l] |
|
|
If using BigFloat or BigInt, be mindful that they are SLOW. They can handle 'big' numbers because they're not bound by the word-length limitations of your CPU's add/sub/mul/div instructions, as they implement their own versions in perl storing the data in encoded strings. Trouble is, there's an awful lot of overhead in doing this.
I have some code which uses BigInt (it's in Net::CIDR, for dealing with IPv6 addresses) and even taking account of the fact that the BigInt version deals with more data than the int version, it still runs at of the order of 1/100th to 1/1000th or worse of the speed.
| [reply] |
|
|
It doesn't sound as though the OP's application needs blazing speed for MP arithmetic. In any case, Math::BigFloat's speed can be improved by using an external library like Pari or GMP, eg.
use Math::BigFloat lib => 'GMP';
| [reply] [d/l] |
Re: Storing more decimal places in a variable
by sleepingsquirrel (Chaplain) on Dec 16, 2004 at 00:32 UTC
|
You could also think of using a different algorithm, one which doesn't depend on perl's floating point numbers. Here I'm thinking of something like the Unbounded Spigot Algorithms for the Digits of π...
Rabinowitz and Wagon call their algorithm a spigot algorithm, because it yields digits incrementally, and does not use digits after they have been computed. The digits drip out one by one, as if from a leaky tap. In contrast, most algorithms for computing the digits of π deliver no output until the whole computation is completed.
Update: fixed broken link.
-- All code is 100% tested and functional unless otherwise noted.
| [reply] |
|
|
That link appears to be broken... but thanks for the info anyway :P
| [reply] |
Re: Storing more decimal places in a variable
by mattr (Curate) on Dec 17, 2004 at 02:20 UTC
|
Funny, when I first read the title I had a flash vision of appending more digits as letters in a string.
I wonder if there is any code for arithmetic overrides using base 26 (or 36). On second thought, if you prefix with a single alphabet letter you should be able to use base 10 within a string. I guess BigInt does something smarter. | [reply] |