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.

How do you compute the midpoint of an interval? (2014) [pdf]

70 pointsby tzhenghaoabout 6 years ago

4 comments

jacobolusabout 6 years ago
Summary: to compute (a + b) &#x2F; 2 and get the best answer for arbitrary inputs, you want:<p>0 if a == –b<p>±realmax if a or b is ±∞<p>otherwise compute (a - a&#x2F;2) + b&#x2F;2 at higher precision, then round to the nearest float
评论 #19519880 未加载
fharsabout 6 years ago
Similar things are relevant for calculations with integers (like array indexes), if you haven‘t yet, read this classic post about it: <a href="https:&#x2F;&#x2F;ai.googleblog.com&#x2F;2006&#x2F;06&#x2F;extra-extra-read-all-about-it-nearly.html?m=1" rel="nofollow">https:&#x2F;&#x2F;ai.googleblog.com&#x2F;2006&#x2F;06&#x2F;extra-extra-read-all-about...</a> (reader mode makes it accessible on iPhones, why don‘t people test their layouts with common browsers?).
评论 #19521846 未加载
abhinaiabout 6 years ago
This post is a great reminder of just how much we need to respect the people who write or have written system libraries. Even simple things can be so complicated when you have to factor for all eventualities. A rocket could blow up because the library function you wrote didn&#x27;t account for floating point arithmetic correctly.
评论 #19520240 未加载
sovaabout 6 years ago
Take an average in a range! Neat. Floating point uses exponential indexing and gets blurry. Taking a chunk of an exponential range and averaging that seems to solve the inaccuracy (!)