TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Subtracting large floating point numbers in different languages

35 pointsby wamattover 8 years ago

10 comments

kovrikover 8 years ago
Isn&#x27;t it an expected behaviour (at least for programmers) because of IEEE 754?<p>For example, in Racket this also gives 2.0, but if you are using exact numbers (#e), then it gives exact and correct answer 1.0.<p>Same for Java (and any other language): if you are using Float&#x2F;Double numbers, then you are loosing precision. Use BigDecimal (or something similar for other languages) if you want exact calculations.
评论 #12899793 未加载
评论 #12897211 未加载
评论 #12897308 未加载
ubernostrumover 8 years ago
Alternate title: &quot;How to get IEEE 754 right&quot;.
评论 #12897317 未加载
Lazareover 8 years ago
Kind confused what the point is. Is this a complaint that most languages use IEEE 754 for non-integer numbers and he thinks they shouldn&#x27;t, or a veiled dig about how many programmers don&#x27;t know this, or...?<p>The color coding of results suggests the author thinks that 2 is wrong and 1 is right, but he&#x27;s going out of his way to specify floating point numbers, and when subtracting those two floating point numbers the correct answer is 1 and NOT 2.<p>Eg, Ruby thinks 9999999999999999.0 - 9999999999999998.0 = 2, but 9999999999999999 - 9999999999999998 = 1. Which is...correct. Right? Unless you don&#x27;t think IEE 754 should be the default?<p>I feel like the author is trying to make a clever point, but if so, I&#x27;m not getting it.
ruricolistover 8 years ago
Note that the Common Lisp example could be abbreviated to:<p>(- 9999999999999999.0l0 9999999999999998.0l0)<p>However, this will only work (either way) in Lisps where long floats are arbitrary precision, like CLISP. In most Lisps (e.g. SBCL, CCL), long floats are just double floats.
lightlyusedover 8 years ago
For java: BigDecimal b1 = new BigDecimal(&quot;9999999999999999.0&quot;);<p><pre><code> BigDecimal b2 = new BigDecimal(&quot;9999999999999998.0&quot;); System.out.println(b1.subtract(b2));</code></pre> prints 1.0
评论 #12897190 未加载
aftbitover 8 years ago
How does Google get 0 out of that? Both my high school math teacher and IEEE 754 think that&#x27;s wrong.
评论 #12899839 未加载
评论 #12899309 未加载
ivan_ahover 8 years ago
SymPy, being Python, gets it wrong... but if you use S = sympify to transform the long numbers into SymPy objects it works:<p><pre><code> S(&#x27;9999999999999999.0&#x27;) - S(&#x27;9999999999999998.0&#x27;) 1.0</code></pre>
评论 #12897511 未加载
rurbanover 8 years ago
The perl5 solution is<p><pre><code> perl -Mbignum -e &#x27;print 9999999999999999.0-9999999999999998.0;print &quot;\n&quot;;&#x27; 1</code></pre>
oulu2006over 8 years ago
MacOS search bar gives the correct result 1.<p>Because it evaluates the expression using the Calculator App.
CalChrisover 8 years ago
Wolfram Alpha gives 1. Bing gives 1.
评论 #12897821 未加载