[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: Result not zero

**From**: |
Paul Zimmermann |

**Subject**: |
Re: Result not zero |

**Date**: |
Mon, 3 Sep 2001 14:07:54 +0200 |

I was looking at GMP to alleviate some situations where ieee floating
point's inherent problems are unacceptable, accountancy for example. That
is, certain decimal numbers can't be represented correctly in ieee--a BCD
number class is required.
Here's my sample code: subtract 0.01 from .1, 10 times. You should have zero
at the end. However, the result claims to be 0.1469367938527859385e-37.
Is there a fix for this? Am I using the right mp?_ functions? Is the GMP
library appropriate?
I would appreciate any input you can provide.
Thanks,
Michael Mann
President
Ware4 Technology
You may try the mpfr library (www.mpfr.org), which is like mpf, but with
rounding modes in addition. Just adding the following two lines in your
program:
#include <mpfr.h>
#include <mpf2mpfr.h>
and controlling the default rounding mode with the following instruction:
__gmp_default_rounding_mode = GMP_RNDU;
you'll get the following results:
rounding mode:
GMP_RNDD (down) Final account balance: -3.46944695195362e-17
GMP_RNDN (nearest) Final account balance: 1.04083408558608e-17
GMP_RNDU (up) Final account balance: 3.81639164714898e-17
Of course using a decimal library is preferable. See the
Standard Decimal Arithmetic page from Mike Cowlishaw
(http://www2.hursley.ibm.com/decimal/).
Paul Zimmermann