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.

Floats Are Weird

154 pointsby soopurmanover 1 year ago

17 comments

cgadskiover 1 year ago
That&#x27;s a neat trick, &quot;cancelling&quot; out catastrophic cancellation. Of course it doesn&#x27;t work anymore once x is smaller than machine epsilon, whereas expm1(x) &#x2F; x will continue to work.<p>In general herbie is pretty good at suggesting the right functions &#x2F; rearrangements. For this example, it finds expm1: <a href="https:&#x2F;&#x2F;herbie.uwplse.org&#x2F;demo&#x2F;378a0682ec4d3e735c790a58fa97e00f46723916.f54ecc7459e2826747d647a0d98ec9e41c611ae9&#x2F;graph.html" rel="nofollow">https:&#x2F;&#x2F;herbie.uwplse.org&#x2F;demo&#x2F;378a0682ec4d3e735c790a58fa97e...</a>
评论 #39428700 未加载
评论 #39428735 未加载
an1sotropyover 1 year ago
I guess &quot;floats are weird&quot; is a catchier title than &quot;numerical computing is an acquired skill based in part on understanding the various consequences of value representation density being inversely proportional to the absolute value&quot;.
评论 #39431792 未加载
评论 #39427414 未加载
Pinusover 1 year ago
Ages ago, when I was in college, a &quot;numerical methods&quot; course was more or less obligatory... has that changed?<p>Anyway, toying with a slide rule is a fun way of making these things very obvious, because you are essentially working with something like 3-digit floating point. When you find yourself subtracting 1.234 from 1.235, but you know that both the &quot;4&quot; and the &quot;5&quot; are really fuzzy around the edges, it becomes obvious that the resulting 0.001 does not really mean much. Similarly, when you take 1.234e+6 (again with a really fuzzy &quot;4&quot;) and add 2, you realize that 1.234002e+6 is not really a sane answer.
评论 #39433158 未加载
评论 #39436942 未加载
评论 #39433874 未加载
eesmithover 1 year ago
For this specific case, use Python&#x27;s expm1, see <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;math.html#math.expm1" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;math.html#math.expm1</a> and history of expm1 at <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Exponential_function#expm1" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Exponential_function#expm1</a><p><pre><code> def f(x): return math.expm1(x)&#x2F;x </code></pre> The expm1(x) means &quot;exp(x) minus 1&quot;.<p><pre><code> &gt;&gt;&gt; f(1e-15) 1.0000000000000007 </code></pre> The technique described gives<p><pre><code> 1.0000000000000004 </code></pre> which is 1 step smaller than the value computed via expm1():<p><pre><code> &gt;&gt;&gt; math.nextafter(f(1e-15), 0) 1.0000000000000004 </code></pre> Both are within 1 ulp of the more precise value from WolframAlpha:<p><pre><code> 1.0000000000000005000000000000001666666666666667083333333333333416...</code></pre>
评论 #39427471 未加载
rho4over 1 year ago
Re money systems: I have found that problems arise no matter whether floats are used or something else: The system can be too exact.<p>My current approach is that I try to guess (or ask) how a customer understands or checks a statement&#x2F;invoice. The customer usually takes a calculator and enters the rounded numbers they see. I then try to do all programmatic calculations exactly the same way: Operation - round - operation - round etc, with rounding-steps at exactly the same places where these values will be visible to the customer, and with the same number of decimal places (can vary). Sometimes that is not possible of course, which is usually solved with a final row called &#x27;rounding errors in the customer&#x27;s favor&#x27;).<p>For my (smaller) systems I find floats sufficient and easier to work with compared to the alternatives, which tend to make code very verbose and annoying to read.
评论 #39431242 未加载
architectonicover 1 year ago
That&#x27;s called the stability of the algorithm. The first algorithm is not stable whereas the second with the logarithm is.<p>Another example: compare the two algorithms, that are actually pure-mathematically equal:<p><pre><code> def fc(x): return sqrt(x+1) - sqrt(x) def fd(x): return 1&#x2F;(sqrt(x+1) + sqrt(x)) </code></pre> For large values of x (1e16 for example) plot the results and see the difference.<p><pre><code> xs = np.linspace(10\*14,10\*16,10000) plt.figure(figsize=(8, 6), dpi=120) plt.plot(xs,[fc(x) for x in xs]) plt.plot(xs,[fd(x) for x in xs])</code></pre>
doubloonover 1 year ago
i think it is our educational system that is weird.<p>we teach students from arithmetic to differential equations assuming they are working with Real Numbers and never really talking about what that means or what a Field is. Then we present computation as something that can easily be done with Real numbers. Which is a lie because we don&#x27;t even know if Pi plus E is rational or not.<p>Then we present computers as tools of computation, handwaving away the fact that Computer floating point math is not using real numbers, and is not a Field nor anything even remotely like a Field. Floats are not even closed under basic arithmetic operations. And god help someone who learned Geometry on graph paper then tries to apply it to a computer with Floats because Floats cannot even represent a uniform grid in space, they vary in resolution by definition depending on distance from origin.<p>So in the end by trying to gloss over the details of how Real Numbers work, and how Computers work, we have actually misrepresented both Real Numbers and Computers to generations of people, for going on 80 years now. And we keep having to have these articles, or &quot;your calculator is wrong&quot; videos go viral every few months&#x2F;years because people were not taught basic facts about Reals and Computers.
评论 #39433202 未加载
cratermoonover 1 year ago
Here&#x27;s a REALLY weird one: <a href="https:&#x2F;&#x2F;latkin.org&#x2F;blog&#x2F;2014&#x2F;11&#x2F;22&#x2F;mullers-recurrence-roundoff-gone-wrong&#x2F;" rel="nofollow">https:&#x2F;&#x2F;latkin.org&#x2F;blog&#x2F;2014&#x2F;11&#x2F;22&#x2F;mullers-recurrence-roundo...</a>
layer8over 1 year ago
This is a whole scientific field, called numerical analysis: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Numerical_analysis" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Numerical_analysis</a>
skaover 1 year ago
It’s not so much that floats are weird, but that they are not reals. Most of the problems happen due to confusion about this fact.<p>Reals are much weirder&#x2F;more interesting than floats, imo.
评论 #39432884 未加载
the-Black-Dreadover 1 year ago
Then what&#x27;s the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in floating points.
评论 #39427348 未加载
评论 #39427485 未加载
评论 #39428007 未加载
评论 #39431883 未加载
评论 #39427197 未加载
mjcohenover 1 year ago
A number of years back I was programming in ADA, a language I generally liked, and had a case where the compiled version of a floating point constant differed from the value generated when the same string was entered at run time. Evidently, the compiler and the run time used different routines to convert strings to floats.<p>The difference wasn&#x27;t much, but it was there.
098799over 1 year ago
Nothing weird about it. It should be obvious that subtracting two floats that are very close to each other results in a loss of numerical precision:<p>1.000000003456e0 - 1.000000002345e0 = 0.000000001111e0 = 1.111numericalnoise e-9<p>It&#x27;s exactly the same issue here. `math.exp(1e-15)` is `1.000000000000001`. If you subtract 1, you get 1 significant digit and numerical noise.
评论 #39427355 未加载
评论 #39427337 未加载
echoangleover 1 year ago
Kind of OT: Why do so many people say “phenomena” when they want to use the singular case?
gonzusover 1 year ago
I decided years ago that the next time I hear someone suggesting we use floats &#x2F; doubles to represent money amounts, I am going to punch them in the face.
评论 #39427699 未加载
评论 #39427713 未加载
评论 #39429096 未加载
评论 #39428571 未加载
评论 #39427522 未加载
评论 #39452132 未加载
评论 #39433306 未加载
评论 #39427597 未加载
user3939382over 1 year ago
I’ve had weird float bugs that I didn’t have time for and fixed by operating on them as strings. Nowadays I’ve found pretty good libraries in common languages designed to handle the weird edges.
评论 #39428780 未加载
pieratover 1 year ago
Hotter take: Floats are bad and shouldn&#x27;t be used.<p>It&#x27;s clear that even experienced programmers do NOT understand how they work, how they don&#x27;t work, and the pile of edge cases.<p>Use something saner, like binary coded decimal, larger types, and use floats as a last and very approximate resort.
评论 #39429212 未加载
评论 #39428747 未加载