Why not FP for Money?

Carlos Ribeiro carribeiro at gmail.com
Tue Sep 21 14:56:38 CEST 2004

On 21 Sep 2004 00:25:25 -0700, Chris Barker <barkmann at gmail.com> wrote:
> If you were to use FP, you could have your IEEE 754 hardware do the
> rounding for you, and get better performance to boot. If you are
> rounding to cents at the end anyway, I can't see that the errors you'd
> get with 64 bit FP numbers would ever be an issue.

I'm not an expert either, but the best way to handle errors of any
kind is not to have them first place. Although what you're talking
about could possibly work, it would require extra care from the
programmer to make sure that all values would be thorougly checked and
rounded at the end of each and every operation. I'm not sure if this
can be made to work on large scale. The option would be to encapsulate
into a special purpose rounded-floating-point class. But then, I'm not
sure if the possible gains that you get from the use of IEEE754
accelerated hardware are worth the extra layer; in the end,
performance may be end up being similar to the one obtained with a
fixed point library.

There is another potential problem: I think that the biggest benefits
of the IEEE754 hardware would come with complex operations that are
directly supported by the hardware. For simple tasks that make the
bulk of money-handling code -- sums and multiplications -- integer
based code is probably as fast or faster, even with all the checks and
the scaling.

Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com

More information about the Python-list mailing list