Perl floating point problem 
Author Message
 Perl floating point problem

From the output below, should I conclude there is a problem
with the Perl floating point routines?  Am I expecting too
many significant digits?  If there is a patch for this, please
let me know.

Thanks , Tom Benjamin

{raven}[11]: perl -e "print 3.1 ;"
3.1000000000000000888

{raven}[12]: perl -e "print 3.2
3.2000000000000001776

{raven}[14]: perl -v

This is perl, version 4.0

$RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $
Patch level: 19



Tue, 26 Aug 1997 05:24:01 GMT  
 Perl floating point problem

Quote:
>{raven}[11]: perl -e "print 3.1 ;"
>3.1000000000000000888

>{raven}[12]: perl -e "print 3.2
>3.2000000000000001776

These numbers are not directly representable in binary floating
point. Perl is giving you the closest representable number, to the
precision of the machine.

If this isn't in the FAQ, it definitely should be...

--
Doug McNaught       Systems Integrator        Towson State University


            WWW Home Page: http://www.towson.edu/~doug/



Tue, 26 Aug 1997 18:36:03 GMT  
 Perl floating point problem

Quote:

>From the output below, should I conclude there is a problem
>with the Perl floating point routines?

Yes.  perl4 prints too many digits.  perl5 works around the problem by
printing too few.  Nobody's gotten around to fixing this; it's harder
than it looks.  For most users perl5's floating point bugs are
preferable to perl4's.

For now, if you want accurate and concise floating point printout,
you'll have to use something other than perl.  E.g. Scheme (:-).



Tue, 02 Sep 1997 01:58:26 GMT  
 Perl floating point problem

Quote:

>For now, if you want accurate and concise floating point printout,
>you'll have to use something other than perl.  E.g. Scheme (:-).

What in hell's wrong with

printf("the value is %6.2f\n", $val);

???

Other than efficiency (not that Scheme or C would be any better).

--
Doug McNaught       Systems Integrator        Towson State University


            WWW Home Page: http://www.towson.edu/~doug/



Tue, 02 Sep 1997 02:04:20 GMT  
 Perl floating point problem

Quote:


> > For now, if you want accurate and concise floating point printout,
> > you'll have to use something other than perl.  E.g. Scheme (:-).
> What in hell's wrong with
> printf("the value is %6.2f\n", $val);

That might lose information, so it's not accurate.
For example, suppose $val is 0.00390625 exactly;
your example printf outputs "0.00", which isn't accurate.

If you print a floating point value without formatting advice,
perl's goal is to print that value both accurately and concisely.
Unfortunately, it doesn't achieve this goal.

By default, perl4's output is accurate but not concise;
e.g. it might print "0.10000000000000001" for the closest floating
point approximation X to 0.1, even though "0.1" is more concise.

perl5 works around perl4's problem by printing something that is
concise but not accurate; e.g. it prints 9999999999999996 as "1e+16",
the same as what it prints for 10000000000000000, even though it
represents both numbers exactly.

Accuracy and conciseness are not mutually exclusive goals; e.g. the
Scheme standard requires output that is both accurate and concise, so
Scheme users don't have to worry either the perl4 or the perl5 problems
mentioned above.  However, doing this right takes some work and
nobody's gotten around to doing it for perl yet.

(In all examples, I'm assuming a correctly-working IEEE floating point
implementation; for other implementations you may need slightly
different examples.)



Wed, 03 Sep 1997 10:16:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem compiling perl with software floating point emulation

2. Problems with floating-point operations

3. Addition of floating point problem

4. Rounding & floating point problems

5. Problem w/unpack, eval and floating point

6. Sybase::DBlib, floating point type problem.

7. Perl's brain-dead floating point math

8. Perl floating point okay to represent $ amounts?

9. Handling floating point number input in Perl CGI scripts

10. Perl 5.001e *** Floating point exception

11. Floating points in Perl

12. perl floating points

 

 
Powered by phpBB® Forum Software