bug-gmp
[Top][All Lists]
Advanced

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

Result not zero


From: Michael Mann
Subject: Result not zero
Date: Fri, 31 Aug 2001 16:28:12 -0400

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

Attachment: Michael Mann.vcf
Description: Vcard


reply via email to

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