Skip to content

Instantly share code, notes, and snippets.

@cpmech
Last active January 30, 2023 02:38
Show Gist options
  • Save cpmech/cd56421ba3d2b34f7d420a6a1bca2df5 to your computer and use it in GitHub Desktop.
Save cpmech/cd56421ba3d2b34f7d420a6a1bca2df5 to your computer and use it in GitHub Desktop.
Float point numbers are evil. The answer should be 2; but we get 0
Evil Floating-Point Computations (using Mathematica 13.1)
Analytical
In[1]:= a={32 10^7,1,-1,8 10^7}
Out[1]= {320000000,1,-1,80000000}
In[2]:= b={4 10^7,1,-1,-16 10^7}
Out[2]= {40000000,1,-1,-160000000}
In[3]:= a b
Out[3]= {12800000000000000,1,1,-12800000000000000}
In[4]:= Total[a b]
Out[4]= 2
Numerical
In[5]:= a={32.0 10^7,1.0,-1.0,8.0 10^7}
Out[5]= {3.2*10^8,1.,-1.,8.*10^7}
In[6]:= b={4.0 10^7,1.0,-1.0,-16.0 10^7}
Out[6]= {4.*10^7,1.,-1.,-1.6*10^8}
In[7]:= a b
Out[7]= {1.28*10^16,1.,1.,-1.28*10^16}
In[8]:= Total[a b]
Out[8]= 0.
@cpmech
Copy link
Author

cpmech commented Jan 30, 2023

evil-floating-point-computations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment