bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Result not zero


From: David T. Ashley
Subject: RE: Result not zero
Date: Fri, 31 Aug 2001 17:13:13 -0400

Hi Mr. Mann,

First thought:  You don't truly need a ***BCD*** number class.  This is like
saying that you need  a gun to kill somebody.  You don't.  A knife, a
shovel, or a piece of piano wire will do the trick.  In other words, you
have presupposed a solution.

What you need is any system of representation where "5" as a prime factor is
accomodated.  The reason that binary math doesn't work right is that no
number whose reduced rational representation includes "5" as a prime factor
in the denominator will be a finite binary number.

BCD will cure it, but that is one of many solutions.

Second, some solutions that come to mind are:

a)Just represent dollar amounts as integral numbers of pennies (i.e. use
integers only).

b)Use rational arithmetic, where you keep everything as a rational number.

BCD is sufficient but not necessarily necessary.

Third, you have not told us very much about your problem.

Best regards, Dave.


> -----Original Message-----
> From: address@hidden [mailto:address@hidden Behalf Of
> Michael Mann
> Sent: Friday, August 31, 2001 4:28 PM
> To: address@hidden
> Subject: Result not zero
>
>
> Hello,
>
> 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
>
>
> #include <stdio.h>
> #include <gmp.h>
> int main (int argc, char **argv)
> {
>     mpf_t account; mpf_clear(account);
>     mpf_t penny;   mpf_clear(penny);
>     mpf_init_set_str(account, ".10", 10);
>     mpf_init_set_str(penny , "0.01", 10);
>     for (int i=0; i<10; ++i) {
>         mpf_out_str(stdout, 10, 0, account);
>         printf(" - ");
>         mpf_out_str(stdout, 10, 0, penny);
>         printf(" = ");
>         mpf_sub(account, account, penny);
>         mpf_out_str(stdout, 10, 0, account);
>         printf("\n");
>     }
>     printf("Final account balance: ");
>     mpf_out_str(stdout, 10, 0, account);
>     printf("\n");
>
>     return 0;
> }
>
> uname -a
> SunOS xxxx 5.7 Generic_106541-15 sun4u sparc SUNW,Ultra-4
>
> gcc -g -I. test.cpp .libs/libgmp.a
>
> 0.1e0 - 0.1e-1 = 0.9e-1
> 0.9e-1 - 0.1e-1 = 0.8e-1
> 0.8e-1 - 0.1e-1 = 0.7e-1
> 0.7e-1 - 0.1e-1 = 0.6e-1
> 0.6e-1 - 0.1e-1 = 0.5e-1
> 0.5e-1 - 0.1e-1 = 0.4e-1
> 0.4e-1 - 0.1e-1 = 0.3e-1
> 0.3e-1 - 0.1e-1 = 0.2e-1
> 0.2e-1 - 0.1e-1 = 0.1e-1
> 0.1e-1 - 0.1e-1 = 0.1469367938527859385e-37
> Final account balance: 0.1469367938527859385e-37
>
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]