Perl floating point okay to represent $ amounts? 
Author Message
 Perl floating point okay to represent $ amounts?

I'm writing a Perl script to deal with dollar amounts.  To what extent do I
need to be concerned about the fact that numeric values are stored as
double-precision floating point, with the inherent inexactness implied by an
mantissa/exponent representation as opposed to an exact integer value?  For
instance, in reading a file of dollar amounts, the simplest thing to do is
to treat something like 1.25 as a floating point value with the decimal
place intact.  The alternative would be to discard the decimal and treat it
as the whole number 125, with two decimal places assumed.  If I do that
instead, am I avoiding the hazards of the possible .0000000000001 (or
whatever) inaccuracies in the floating point representation, or do I end up
in exactly the same place?  This isn't a major banking application or
anything large-scale, but I would like to know if I'm using the wrong tool
for the job (or using it incorrectly!).  Thanks.....
--

----- Don Thomson ----- DoIT (Division of Information Technology) ------------



Sat, 04 Jan 1997 20:10:12 GMT  
 Perl floating point okay to represent $ amounts?

Quote:

>I'm writing a Perl script to deal with dollar amounts.  To what extent do I
>need to be concerned about the fact that numeric values are stored as
>double-precision floating point, with the inherent inexactness implied by an
>mantissa/exponent representation as opposed to an exact integer value?
  [...]
>This isn't a major banking application or
>anything large-scale, but I would like to know if I'm using the wrong tool
>for the job (or using it incorrectly!).  Thanks.....

You could have a look at the bigint library and treat everything as cents
(and take a performance hit).

Mike

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

Meiko tel: (508) 371 0088 x124    |



Sat, 04 Jan 1997 23:12:15 GMT  
 Perl floating point okay to represent $ amounts?

] I'm writing a Perl script to deal with dollar amounts.  To what extent do I
] need to be concerned about the fact that numeric values are stored as
] double-precision floating point, with the inherent inexactness implied by an
] mantissa/exponent representation as opposed to an exact integer value?  

A few years ago I wrote some perl programs to help me as church treasurer.  I
quickly decided that dealing with whole cents rather than fractions of dollars
made my job much easier.  Write a subroutine to handle $$->int conversion and
another to do the inverse.
--
Eric Krohn



Sun, 05 Jan 1997 06:55:33 GMT  
 Perl floating point okay to represent $ amounts?


Quote:
>A few years ago I wrote some perl programs to help me as church
>treasurer.  I quickly decided that dealing with whole cents rather than
>fractions of dollars made my job much easier.

Amen.

Quote:
>Write a subroutine to handle $$->int conversion and another to do the
>inverse.

And another to do rounding properly, for whatever value of "properly"
fits in your case, in case you get fractional pennies.  My case was for
interest calculations for a loan.  (A church treasurer wouldn't have to
worry about such things, since a Christian doesn't charge interest.
Oops, my medievalist interests are showing.)

--
Tim McDaniel, Convex Computer Corporation, Richardson, TX (near Dallas)



Sun, 05 Jan 1997 11:42:03 GMT  
 Perl floating point okay to represent $ amounts?

Quote:
>I'm writing a Perl script to deal with dollar amounts.  To what extent do I
>need to be concerned about the fact that numeric values are stored as
>double-precision floating point, with the inherent inexactness implied by an
>mantissa/exponent representation as opposed to an exact integer value?

Unless you're dealing with huge amounts, Perl's floating point should be
good enough.  Double precision provides about 12 digits of accuracy, so
unless you're handling amounts over a billion, the floating point errors
shouldn't be significant.

Dealing with pennies probably won't help; Perl uses floating point
internally for everything anyway.  The only thing this would avoid is the
error due to converting a number from decimal to floating point (since many
multiples of .1 and .01 cannot be represented as exact floating point
values).
--
Barry Margolin
System Manager, Thinking Machines Corp.




Mon, 06 Jan 1997 02:06:27 GMT  
 Perl floating point okay to represent $ amounts?

Quote:
> Dealing with pennies probably won't help; Perl uses floating point
> internally for everything anyway.  The only thing this would avoid is the
> error due to converting a number from decimal to floating point (since many
> multiples of .1 and .01 cannot be represented as exact floating point
> values).

Actually, I think dealing with pennies is worth the effort.  Typical
double-precision representations (e.g., 64-bit IEEE) have an effective
53 bits of precision, so integer values up to 2**53-1 (nearly 10**16)
are represented and computed exactly.  A 64-bit floating point object
can exactly represent more distince integer values than a 32-bit integer.

It's true that decimal roundoff, due to the inexact representation of
0.01, isn't likely to be much of a problem, but it can build up to
significant errors in obscure circumstances.  Doing computations in
exact pennies means there's one less thing to worry about when you're
trying to track down a bug in your program.

--

TeleSoft^H^H^H^H^H^H^H^H Alsys, Inc.
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718
Press any key to continue.  Press any other key to quit.



Tue, 07 Jan 1997 10:26:57 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Perl's brain-dead floating point math

2. Handling floating point number input in Perl CGI scripts

3. Perl 5.001e *** Floating point exception

4. Problem compiling perl with software floating point emulation

5. Perl floating point problem

6. Floating points in Perl

7. perl floating points

8. Perl Floating Point Rounding Algorithm?

9. floating point numbers on C vs. Perl

10. Handling floating point number input in Perl CGI scripts

11. Question about floating point math w/perl

12. Floating point Perl

 

 
Powered by phpBB® Forum Software