frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Comparing floating-point numbers (2012)

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
26•sph•1y ago

Comments

LegionMammal978•1y ago
I'd argue that any equality comparison of floating-point numbers is asking for trouble, unless you're specifically working with small dyadic fractions (using exact comparison) or testing a purely heuristic 'closeness' condition (using fuzzy comparison).

Of course, inequalities show up in a lot more places, but are similarly fraught with difficulty, since mathematical statements may fail to translate to floating-point inequalities. E.g., in computational geometry, people have written entire papers about optimizing correct orientation predicates [0], since the naive method can easily break at small angles. This sort of thing is what often shows up as tiny seams in 3D video-game geometry.

[0] https://www.cs.cmu.edu/~quake/robust.html

mtklein•1y ago
My preferred way to compare floats as being interchangeably equivalent in unit tests is

    bool equiv(float x, float y) {
        return (x <= y && y <= x)
            || (x != x && y != y);
    }
This handles things like ±0 and NaNs (while NaNs can't be IEEE-754-equal per se, they're almost always interchangeable), and convinces -Wfloat-equal you kinda know what you're doing. Also everything visually lines up real neat and tidy, which I find makes it easy to remember.

Outside unit tests... I haven't really encountered many places where float equality is actually what I want to test. It's usually some < or <= condition instead.

sph•1y ago
I have built a production Javascript library with decent amounts of users that incorporates the following hack to deal with float error (avert your eyes if you're sensitive):

  // 1.2 - 1.0 === 0.19999999999999996
  // fixFloatError(1.2 - 1.0) === 0.2
  var fixFloatError = function (n) {
    return parseFloat(n.toPrecision(12));
  };
It felt correct at the time, but after reading the article, I cringe at how fundamentally broken it is. I got away with it because the library is used to convert betting odds, which are mostly small floating point numbers, so the error is often < 10^-12.

"IT WoRKs BeTter in the App "

https://shkspr.mobi/blog/2026/08/it-works-better-in-the-app/
479•blenderob•3h ago•284 comments

Get your Windows license refund

https://en.refund4freedom.org/
231•smartmic•2h ago•74 comments

Inception-style curved map for turn-by-turn directions

https://www.orbify.eu/demo/
173•smoser•3h ago•59 comments

GLM-5.3 is now open-weight

https://twitter.com/Zai_org/status/2093354097122455713
99•jeudesprits•53m ago•27 comments

Autistici/Inventati's main .org domain goes dark after US terrorism designation

https://mastodon.bida.im/@cavallette/117172989300228024
105•subvol•1h ago•31 comments

Just the rumour of a bug is enough to find an exploit these days

https://anil.recoil.org/notes/rumour-is-the-exploit
6•avsm•15m ago•0 comments

OpenStreetMap State of the Map 2026

https://2026.stateofthemap.org/
57•lode•2h ago•17 comments

OpenAI: Migrating to HTTPX2

https://github.com/openai/openai-python/blob/main/httpx2.md
132•tosh•4h ago•53 comments

The Twelve-Factor App

https://12factor.net/
61•jxmorris12•17h ago•22 comments

Hilariously Fast Volume Computation with the Divergence Theorem

https://alyssarosenzweig.ca/blog/hilariously-fast-volume-computation-with-the-divergence-theorem....
185•luu•7h ago•47 comments

Htmx 4.0.0 has been released

https://four.htmx.org/announcements/2026-08-28-htmx-4.0.0-is-released
40•rmsaksida•2h ago•10 comments

Run Qwen3.8 27B locally: real numbers from my Mac Studio

https://terminalbytes.com/run-qwen-3-8-27b-locally/
6•speckx•49m ago•0 comments

Africa's Wild Dogs Are the Most Hated Carnivores on the Continent

https://www.smithsonianmag.com/science-nature/africa-wild-dogs-most-hated-carnivores-continent-he...
11•speckx•1h ago•2 comments

Luanti removed from Google Play due to baseless AI copyright notice

https://blog.luanti.org/2026/08/27/luanti-dmca-tracer-ai/
237•miniBill•9h ago•73 comments

Are Crows Our Friends?

https://www.audubon.org/magazine/are-crows-really-our-friends
32•speckx•3h ago•20 comments

Pentagon's blacklisting of Anthropic was unlawful, US judge rules

https://www.reuters.com/legal/government/us-judge-blocks-pentagons-anthropic-blacklisting-2026-08...
304•softwaredoug•4h ago•103 comments

GUIs should be fully keyboard-driven

https://ckardaris.com/blog/2026/08/28/keyboard-driven-guis.html
6•ckardaris•56m ago•2 comments

I Used AWS Cognito for a Startup. I Wouldn't Do It Again

https://joshkaramuth.com/blog/aws-cognito-authentication-startup-nightmare/
102•speckx•2h ago•81 comments

Don't use musl if you care about performance

https://blog.brokk.ai/dont-use-musl-if-you-care-about-performance/
4•jbellis•57m ago•0 comments

Verschlimmbesserung: The Word Your Software Updates Need

https://geekyschmidt.com/post/2026-08-25-verschlimmbesserung/
8•speckx•1h ago•1 comments

httpx2

https://github.com/pydantic/httpx2
54•tosh•4h ago•20 comments

How Dactyl Works

https://dactyl.dev/blog/how-dactyl-works/
11•anorak27•31m ago•0 comments

Interactive Warhammer 40k Galaxy Map – Cartographia Imperialis

https://cartographia40k.com/
97•gbxyz•7h ago•23 comments

Autistici/Inventati case sets a new counterterrorism precedent, Irdi says

https://decode39.com/16319/autistici-inventati-case-sets-a-new-counterterrorism-precedent-irdi-says/
12•iamnothere•2h ago•1 comments

Bhartrhari's Paradox

https://www.futilitycloset.com/2026/08/18/bhartrharis-paradox/
27•surprisetalk•4h ago•27 comments

"Weird" is a weird word

https://www.deadlanguagesociety.com/p/weird-is-a-weird-word
21•pseudolus•2h ago•3 comments

Interactive pattern discovery in binaries (FF-16-TUI)

https://github.com/HexLasso/FF-16-TUI
21•HexLasso•3h ago•2 comments

Lake formed after ice-rock avalanche remains at a high level and is overflowing

https://kathmandupost.com/national/2026/08/28/barrier-lake-continues-to-pose-flood-risk-china-warns
5•r721•52m ago•0 comments

ALMA Reveals Long-Lived Hotspots on Betelgeuse's Bubbling Surface

https://www.almaobservatory.org/en/audiences/alma-reveals-long-lived-hotspots-on-betelgeuses-bubb...
27•layer8•4h ago•3 comments

GoGoGrandparent (YC S16) is hiring back end engineers

https://www.ycombinator.com/companies/gogograndparent/jobs/2vbzAw8-backend-engineer
1•davidchl•12h ago