Re: Ask for recommended module for precise number
- From: "Sisyphus" <sisyphus1@xxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 04:50:57 +1100
"Tom" <chi.lun@xxxxxxxxx>
> ===============================================================
> C:
>
> double addOne(double x)
> {
> double y = x + 1.0;
>
> return y;
> }
> ===============================================================
> PERL:
>
> my $x = new Math::BigFloat(0.1);
> $x = addOne($x);
> printf "%.5f\n", $x;
> ===============================================================
>
> It is amazing that it output "1.10000" to me !
Yes - I was surprised by that, too. I get the same when I run the following
script:
-- start --
use Math::BigFloat;
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1; # see any compiler warnings
use Inline C => <<'EOC';
double addOne(double x)
{
double y = x + 1.0;
return y;
}
EOC
my $x = new Math::BigFloat(0.1);
$x = addOne($x);
printf "%.5f\n", $x;
__END__
-- end --
If you have Inline::C installed, then you'll be able to run that script,
too.
What's I find surpising is that you (and I) are able to pass a
Math::BigFloat object to a C function that takes a *double* as its argument,
and not get an error. Furthermore, addOne() also gets hold of the *correct*
value. I don't know exactly how that is achieved. It seems that when you do
'addOne($x);', where $x is a Math::BigFloat object then addOne() receives,
as its argument, whatever it is that the overloaded string operator ("")
returns for $x. I think this would always be the value you see when you
'print $x;'.
The same thing happens with other modules (eg Math::GMP) which use
overloading. That is, I can succesfully 'addOne($x);' where $x is a
Math::GMP object.
With Math::MPFR the overloaded string operator returns values in the form
'.127@2' - instead of the usual '12.7'. If I 'addOne($x);' where $x is a
Math::MPFR object that has the value of (say) 1.2, then I get the warning:
Argument ".12@1" isn't numeric in subroutine entry .....
and the argument that addOne() receives in this case is '0.12' instead of th
e desired '1.2'. (Note that when perl treats a string as a number it ignores
the first "garbage" character it finds, and anything that comes after that
"garbage" character. The first "garbage" character in '.12@1' is the "@",
so perl ignores it and everything that follows it - leaving just the number
'.12'.)
If Math::MPFR's overload string function returned the value as '1.2' instead
of '.12@1', then Math::MPFR objects could be passed successfully to addOne()
in exactly the same way as happens with Math::BigFloat.
>
> After that I modify the program as:
> ===============================================================
> PERL:
>
> my $x = new Math::BigFloat(0.1);
> foreach my $i (0..99) {
> $x = $x * 0.1;
> }
> my $x = 10.0;
> $x = Simplest::addOne($x);
>
> printf "%.5f\n", $x;
> ===============================================================
>
> And it can output "1.00000" to me!
I don't see how that is possible - and I get exactly what I expect to get:
11.00000
Or, if warnings are enabled I get:
"my" variable $x masks earlier declaration in same scope at try.pl line 24.
11.00000
which is also to be expected.
Cheers,
Rob
.
- References:
- Ask for recommended module for precise number
- From: Tom
- Re: Ask for recommended module for precise number
- From: Sisyphus
- Re: Ask for recommended module for precise number
- From: Tom
- Re: Ask for recommended module for precise number
- From: harryfmudd [AT] comcast [DOT] net
- Re: Ask for recommended module for precise number
- From: Tom
- Re: Ask for recommended module for precise number
- From: Tom
- Re: Ask for recommended module for precise number
- From: harryfmudd [AT] comcast [DOT] net
- Re: Ask for recommended module for precise number
- From: Tom
- Ask for recommended module for precise number
- Prev by Date: Re: Ask for recommended module for precise number
- Next by Date: How do I force a close connection in SOAP
- Previous by thread: Re: Ask for recommended module for precise number
- Next by thread: Re: Ask for recommended module for precise number
- Index(es):
Relevant Pages
|
|