Perl's brain-dead floating point math 
Author Message
 Perl's brain-dead floating point math


:
: Why is Perl so stupid when it comes to floating point math?  
:
: Example:
:
:       $num = 43545.0;
:
:       $num = $num / 10.0;
:       print "$num\n";
:
:       $num = $num / 10.0;
:       print "$num\n";
:
:       $num = $num / 10.0;
:       print "$num\n";
:
: Results:
:
: 4354.5
: 435.44999999999998863
: 43.545000000000001705
:
:
:
: This example program has worked incorrectly on every machine I have
: tried it on, therefore I have concluded that it isn't a problem with
: the installation on my machine.
:
: Any thoughts?

This is improved in Perl 5, because the Configure process (thank Andy
et al) now figures out the number of significant digits to use for its
internal conversion, and the above prints out (on my machine, anyway)

    4354.5
    435.45
    43.545

Still and all, it's floating point underneath, and that will always
have its ramifications.

Larry



Tue, 18 Mar 1997 05:26:06 GMT  
 Perl's brain-dead floating point math

|>
|> Why is Perl so stupid when it comes to floating point math?  
|>
|> Example:
|>
|>   $num = 43545.0;
|>
|>   $num = $num / 10.0;
|>   print "$num\n";
|>
|>   $num = $num / 10.0;
|>   print "$num\n";
|>
|>   $num = $num / 10.0;
|>   print "$num\n";
|>
|> Results:
|>
|> 4354.5
|> 435.44999999999998863
|> 43.545000000000001705
|>
|>
|>
|> This example program has worked incorrectly on every machine I have
|> tried it on, therefore I have concluded that it isn't a problem with
|> the installation on my machine.
|>
|> Any thoughts?
|>
|> Rob

What did you expect?  More precision?  17 digits is not too bad.

I think you are wrong blaming perl in any case - it is most
likely relying on your systems RTL or perhaps even the FPU to do
the calculations.

Maybe you are just reacting to the fact that the print routine
does not round off the result to a nice even decimal number
hiding the error from you.  If you like that sort of thing, try

printf "%g\n", $num;

--

If only we had 16 fingers ... and who needs decimal numbers
anyway??

--

Philips Interactive Media, Inc.          (310) 444-6673
11050 Santa Monica Blvd.             Fax (310) 477-4953
Los Angeles, CA 90025



Mon, 17 Mar 1997 08:52:09 GMT  
 Perl's brain-dead floating point math

Quote:

> If you BigFloats, you won't have this problem.

Which begs the question: "How *does* one BigFloats, anyway?"
--

Compaq Computer Corporation                   Houston, TX  77269-2000
"Good taste is timeless... and a good time is usually tasteless."


Tue, 18 Mar 1997 06:31:05 GMT  
 Perl's brain-dead floating point math

Quote:

>> If you BigFloats, you won't have this problem.
> Which begs the question: "How *does* one BigFloats, anyway?"

_You_ begged for this one...

You LittleFloat, but at twice the pressure.

(Better use the mil-spec LittleFloat: Civilian models might burst)

-Allen S. "Bouyant" Rout

--

<     (904)373-0906  >| say: I have strong opinions, but a wide-open
< 1936 N.W. 2nd Avenue       >|  mind.
< Gainesville, FL 32603      >| BUT KEEP THE FLAMES TO E-MAIL!  (geez!)
-"My marriage is on the rocks, but only because we can't find a blanket."
<a href=http://www.cis.ufl.edu/~asr/asr.html>My Home Page</a>



Tue, 18 Mar 1997 09:51:20 GMT  
 Perl's brain-dead floating point math

Quote:

>If only we had 16 fingers ... and who needs decimal numbers
>anyway??

Well, I had 8 fingers at the last count, but blame the French and their
desire to make *everything* base 10 for the inclusion of thumbs as
"fingers" ;-) ... Of course an English pint is 20 fl oz, but as this wins
me 4 oz of beer a pint I'm happy ;*)

I'm sure I could close the loop from beer -> floating point, but...

Mike
--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | 130C Baker Ave. Ext

Meiko tel: (508) 371 0088 x124    |



Wed, 19 Mar 1997 04:13:02 GMT  
 
 [ 36 post ]  Go to page: [1] [2] [3]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software