Re: Ask for recommended module for precise number



Hi Harry,

In fact those number are required to interact with a conjugate gradient
library in C++.
I tried to write a simple program in C and then use SWIG to generate
the PERL interface:

===============================================================
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 !

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!


Tom



harryfmudd [AT] comcast [DOT] net wrote:
> Tom wrote:
> > Sorry, I should call you "Tom"...........
> >
>
> No problem. Harry Mudd is a quasi-alias (quasi- because I always sign my
> real name), and a character from the original Star Trek. It was intended
> to be thrown away when it started collecting too much spam, but so far I
> haven't done that. Though I'm tempted daily.
>
> >
> > Tom wrote:
> >
> >>Hi Harry,
> >>
> >>Thanks for you help. It was really my misunderstanding before. I write
> >>another program which simulate my typical application: calculating
> >>posterior from prior and likelihood, which are very small number.
> >>
> >>================================================================
> >>use strict;
> >>
> >>use Math::BigFloat;
> >>use Carp;
> >>
> >>my $x = new Math::BigFloat(0.1);
> >>my $y = new Math::BigFloat(0.1);
> >>my $num = 100;
> >>
> >>foreach my $i (0..$num) {
> >> $x = $x * 0.1;
> >>}
> >>foreach my $j (0..$num-1) {
> >> $y = $y * 0.1;
> >>}
> >>
> >>my $z = $x/$y;
> >>
> >>printf "z: %.5f\n", $z;
> >>================================================================
> >>Now, the z is "0.1". It fits my usage as those small numbers are only
> >>stored internally. :)
> >>
> >>
> >>For the printf() issue, it seems to be inconsistency between the string
> >>and the printf() function. I have tried the following tiny program:
> >>
> >>================================================================
> >>use strict;
> >>use Carp;
> >>
> >>my $i = 0;
> >>while (1) {
> >> my $zeroString = join('', map { "0" } (1..$i));
> >> my $numString = sprintf("0.%s1", $zeroString);
> >> eval {
> >> printf "%d: %.5f\n", $i, $numString;
> >> };
> >> confess ($@) if ($@);
> >>
> >># confess (qq{Convert to "0" at $i-th iterations.\n}) if ($numString
> >>== 0);
> >>
> >> $i++;
> >>}
> >>================================================================
> >>With the "commented" statement, the program always output "0.00000"
> >>without error (the program was terminated at 9xxx-th iterations).
> >>
> >>With the "commented" statement, the program converts to "0" at around
> >>300 iterations. (it means the Math::BigFloat case is not reproductable
> >>at normal PERL operations?)
> >>
> >>
> >>Tom
> >>
> >>
>
> [snip! /]
>
> I think we can conclude that printf is simply not going to work right
> with Math::BigFloat numbers.
>
> The bottom line is that Math::BigFloat is _almost_ magic. I have not
> used it much, but I get the impression that as long as you're so-called
> "pure Perl" you're OK. That is, if you write a script using BigFloat,
> and don't invoke any functions that you didn't write in Perl, you're
> fine. If you invoke built-ins (for example, log (), sin (), and so
> forth) or libraries that do "C" call-outs, you don't get arbitrary
> precision. At least, that's my explanation for printf's behaviour.
>
> Even if Math::BigFloat were fully magic and worked for the built-ins,
> transcendental functions like log () and sin () would still be unusable,
> because the calculation would never terminate.
>
> If you read the Math::BigFloat docs, you will find methods to "take
> apart" BigFloat numbers, and I suppose you (or someone) could write
> arbitrary-precision functions to replace the built-ins. Precision of
> transcendental functions would have to be specified "up front" in some
> way, to prevent atan2 (1, 0) from trying to compute the last digit of pi.
>
> Whether this has been done I don't know. I personally would hesitate to
> do it because I don't need it, and therefore it would be inadequately
> tested.
>
> If I could change the subject briefly, I'd like to make a couple
> comments on Perl style:
>
> * You don't need to "use Carp" and then "confess" unless you're
> interested in a full call stack trace. The "die" command also terminates
> your program with a message. If the message does not end in "\n", Perl
> appends the line number of the "die".
>
> * For a string of $i zeros, you can say '0' x $i instead of using join
> (), and I think you'll find it is more efficient.
>
> * Similarly, for large numbers of iterations, "for (i = 0; i < $num;
> $i++)" would be preferred to "foreach $i (0 .. $num)", because the "0 ..
> $num" actually builds a list of all the numbers, which might be a bad
> thing if $num were something like 1e9.
>
> Tom Wyant

.



Relevant Pages

  • Re: sssh bang problem
    ... Tom, thanks info. ... When you run a set-id program, you can't give perl arbitrary command ... line flags, because many of those flags would change the meaning of ... the program against the wishes of the program's owner. ...
    (perl.beginners)
  • Re: Ask for recommended module for precise number
    ... but I get the impression that as long as you're so-called "pure Perl" you're OK. ... Even if Math::BigFloat were fully magic and worked for the built-ins, transcendental functions like log and sin would still be unusable, because the calculation would never terminate. ... Tom Wyant ...
    (comp.lang.perl.modules)
  • Re: counting number of multiple occurances in a row
    ... I would like know how to count multiple occurances in a sql query. ... tom, tom, tom, john,pat,pat ... select countfrom commitCurrent where company =(select distinct ... I know its not exactly perl but, I have the script written in perl. ...
    (perl.beginners)
  • Re: (U) What are the best Perl books out there ... More than 1 is fin e ... list as many as you like
    ... > Learning Perl by Randal Schwartz & Tom Phoenix as a good introduction with ... > Programing Perl by Larry Wall, Tom Christiansen and Jon Orwant as the ultimate ... to check out "Learning Perl Objects, References, & Modules" by Randall ...
    (perl.beginners)
  • Re: join two binary numbers
    ... use warnings; ... Perl is doing an bitwise or of each *byte* of the two strings, padding $num of the right with two zero bytes to match the length of $res. ...
    (comp.lang.perl.misc)