Re: Ask for recommended module for precise number




"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


.



Relevant Pages

  • Re: error messege: "subroutine Cwd::fastcwd redefined"
    ... Attached is the file created by perlbug. ... asking for all warnings even though Cwd is trying (via the older ... case it looks like a bug in the Perl distribution. ... HOME (unset) ...
    (perl.beginners)
  • Re: file rename help
    ... > I am very novice as you will see in my Perl skills. ... use warnings; ... because I am not afraid of undef values, ... even as a piece of shell code, your command is wasting at least two ...
    (comp.lang.perl.misc)
  • Re: Multiple Parameters
    ... The above two lines ask Perl to hold you to the laws of good ... programming. ... you'll get better error messages and warnings ...
    (perl.beginners)
  • Re: Newbie questions, migrating from c++
    ... > to get no warnings using SEEK_SET etc, ... So if I'm to do something with chars in string (or perl ... It is not a good idea to use this method of subroutine invocation unless ... sub no_need { ...
    (comp.lang.perl.misc)
  • Re: Newbie. Use of uninitialized value in print??
    ... > Im having problems getting some Perl code to work (im a programming ... You should ask for all the help the perl can give you. ... Did your online reading include the posting guidelines for this group? ... Warnings: The script below ignores various issues such as the cases ...
    (comp.lang.perl.misc)