How to obtain exact results from simple arithmetic calculations?

Asked

Viewed 44 times

0

I tested simple arithmetic calculations in multiplex languages.

I tested some calculations in python console:

>>> 0.1+0.1
0.2
>>> 0.1+0.2
0.30000000000000004
>>> 10*0.67
6.7
>>> 10*0.68
6.800000000000001
>>> 10*0.69
6.8999999999999995

I also tested in javascript:

> 0.1 + 0.1
0.2 // correto
> 0.1 + 0.2
0.30000000000000004 // erro
> 10 * 0.67
6.7 // correto
> 10 * 0.68
6.800000000000001 // erro
> 10 * 0.69
6.8999999999999995 // erro

In Fortran it only got worse:

        real(kind=4)    :: soma, produto, soma1, produto1        
        soma = 0.1+0.1
        print *, '0.1+0.1 = ',soma 
        soma = 0.1+0.2
        print *, '0.1+0.2 = ',soma 

        produto = 10*0.67
        print *, '10*0.67 = ', produto  
        produto = 10*0.68
        print *, '10*0.68 = ', produto
        produto = 10*0.69
        print *, '10*0.69 = ', produto
        print *,'' 

        soma1 = 0.1d0+0.1d0
        print *, '0.1d0+0.2d0 = ',soma1
        soma1 = 0.1d0+0.2d0
        print *, '0.1d0+0.2d0 = ',soma1

        produto1 = 10.d0*0.67d0
        print *, '10.d0*0.67d0 = ', produto1
        produto1 = 10.d0*0.68d0
        print *, '10.d0*0.68d0 = ', produto1
        produto1 = 10.d0*0.69d0
        print *, '10.d0*0.69d0 = ', produto1
        print *,'' 

        soma1 = 0.1_8+0.1_8
        print *, '0.1_8+0.1_8 = ',soma1
        soma1 = 0.1_8+0.2_8
        print *, '0.1_8+0.1_8 = ',soma1

        produto1 = 10_8*0.67_8
        print *, '10_8*0.67_8 = ', produto1
        produto1 = 10_8*0.68_8
        print *, '10_8*0.68_8 = ', produto1
        produto1 = 10_8*0.69_8
        print *, '10_8*0.69_8 = ', produto1

The way out:

 0.1+0.1 =   0.200000003    
 0.1+0.2 =   0.300000012    
 10*0.67 =    6.70000029    
 10*0.68 =    6.80000019    
 10*0.69 =    6.90000010    

 0.1d0+0.2d0 =   0.200000003    
 0.1d0+0.2d0 =   0.300000012    
 10.d0*0.67d0 =    6.69999981    
 10.d0*0.68d0 =    6.80000019    
 10.d0*0.69d0 =    6.90000010    

 0.1_8+0.1_8 =   0.200000003    
 0.1_8+0.1_8 =   0.300000012    
 10_8*0.67_8 =    6.69999981    
 10_8*0.68_8 =    6.80000019    
 10_8*0.69_8 =    6.90000010 

The cause of the problem has already been explained in the question Inaccurate result in broken numbers calculation but because calculators don’t have that problem.

There is a way to get results as good as a market cash calculator with these languages?

  • 1

    Excellent reading Why do computers suck at math? in Coding Horror (English). In short, real numbers often have infinite digits. Computers are incredible, but not infinite. The best they can do is get closer.

  • The computer does nothing wrong, the language also does not, well has language that even does, but is rare, is debatable and is not the case. So it only leaves the programmer even for using the wrong tool. When accuracy is desired, do not use floating point but one decimal, according to this answer for Python: http://answall.com/a/44718/101. also: http://answall.com/a/38140/101

  • Even with the edition that mischaracterizes the original question, it is still duplicated. I am prone to change the original that indicates what to do to solve the problem, but as it is mine I will avoid. Anyway all the information to solve this is linked here.

  • 1

    I read that answer but it doesn’t tell me how to perform an exact calculation on python. It really answers why? It was enriching to read it, but as I do, to calculate, there is a special function to get around this situation. The better I take my tank and calculate in hand...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.