# Addition and subtraction don't give exact zero?

Posted 2 months ago
1429 Views
|
12 Replies
|
4 Total Likes
|
 Hi, I just want to make very simple operations, and it doesn't work. The result of the attached operation (in the last line i) should be simply zero, and I have 9. 10 -13 instead of zero ! .... Please help. a = 5306.47 b = 4 c = 1182.54 d = 50.26 e = 0 f = 377.11 g = c + d + e + f h = 3700.56 i = a + b - g - h 
12 Replies
Sort By:
Posted 2 months ago
 Machine precision numbers are not exact so there will always be rounding errors. This is not something specific to Wolfram language. If you want exact results, use exact numbers. a = Rationalize@5306.47 b = 4 c = Rationalize@1182.54 d = Rationalize@50.26 e = 0 f = Rationalize@377.11 g = c + d + e + f h = Rationalize@3700.56 i = a + b - g - h (* 0 *) For details, see this.
Posted 2 months ago
 Thank you very much! So does it means that for financial computations, it is recommended to always enclose the inputs numbers with the Rationalize[] function?
Posted 2 months ago
 Here is a shorter way:
Posted 2 months ago
 Thanks!
 It would depend on what "financial computations" are being performed. Machine precision is about 16 decimal digits which should be sufficient. As Marvin showed, you can always use Chop to convert small values to 0. $MachinePrecision (* 15.9546 *)  Answer Posted 2 months ago  Don't mix integers with reals, Rationalize[] is an ovekill because there is AccountingForm[], a formatter, with other words Clear[a, b, c, d, e, f, h] a = 5306.47; b = 4.; c = 1182.54; d = 50.26; e = 0.; f = 377.11; h = 3700.56; In[51]:= c + d + e + f // AccountingForm Out[51]//AccountingForm= 1609.91 In[52]:= a + b - g - h // AccountingForm Out[52]//AccountingForm= 0.  Answer Posted 2 months ago  ..? Thanks for the proposal, but this following code doesn't give the expected result (0) on my side ... Clear[a, b, c, d, e, f, h] a = 5306.47 b = 4. c = 1182.54 d = 50.26 e = 0. f = 377.11 g = c + d + e + f // AccountingForm h = 3700.56 i = a + b - g - h // AccountingForm  Answer Posted 2 months ago  That is because AccountingForm is not computable (like MatrixForm, TableForm...). It is for display purposes only. It can only be used on the final result. Answer Posted 2 months ago  In[122]:= Clear[a, b, c, d, e, f, h, g] a = 5306.47; b = 4.; c = 1182.54; d = 50.26; e = 0.; f = 377.11; h = 3700.56; g = c + d + e + f Out[130]= 1609.91 In[133]:= N[SetAccuracy[a + b - g - h, 2]] Out[133]= 0. In[134]:= N[a + b - g - h, 2] Out[134]= 9.09495*10^-13  Answer Posted 2 months ago  Accuracy can be nailed on input, still output needs to be stripped by a formatter Clear[a, b, c, d, e, f, h, g] a = 5306.472; b = 4.2; c = 1182.542; d = 50.262; e = 0.2; f = 377.112; h = 3700.562; g = c + d + e + f Out[9]= 1609.9 In[14]:= a + b - g - h Out[14]= 0.*10^-2 In[12]:= N[a + b - g - h] Out[12]= 0. In[13]:= AccountingForm[a + b - g - h] Out[13]//AccountingForm= 0.  Answer Posted 2 months ago  Quantity can do what you expect if we add an extra definition as follows: Quantity[1,"Dollars"]; Unprotect[Quantity]; Quantity[x_,"Dollars"]+Quantity[y_,"Dollars"]^:=With[{s=x+y},( Quantity[0.01(Round[100 x]+Round[100 y]),"Dollars"])/;Head[s]===Real]; UpValues[Quantity]=RotateRight[UpValues[Quantity],1]; Then we can do this: a=Quantity[5306.47,"Dollars"]; b=Quantity[4,"Dollars"]; c=Quantity[1182.54,"Dollars"]; d=Quantity[50.26,"Dollars"]; e=Quantity[0,"Dollars"]; f=Quantity[377.11,"Dollars"]; g=c+d+e+f; h=Quantity[3700.56,"Dollars"]; i=a+b-g-h (*$0.00 *) However, if you evaluate b, it will be displayed as an integer number of dollars. If you want to see \$4.00 instead, add the following definition: Quantity[x_Integer,"Dollars"]:=Quantity[N@x,"Dollars"]; `