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.

Hurried Announcement: Alessandro Masullo GRH

https://math216.wordpress.com/2026/11/20/hurried-announcement-alessandro-masullo-grh/
1675•jasondavies•8h ago•746 comments

Inkling 2 Mini

https://thinkingmachines.ai/news/inkling-2-mini/
748•jasondavies•7h ago•128 comments

Levent Alpöge claims proof of the Generalized Riemann Hypothesis [pdf]

https://alpo.ge/remark.pdf
2416•iamronaldo•13h ago•1630 comments

NINLIVE: The Nine Inch Nails live archive

https://ninlive.com/
149•CharlesW•4h ago•35 comments

We promoted developers into architects without informing them

https://manidoraisamy.com/developer-to-architect.html
329•QueensGambit•9h ago•192 comments

Valkey kills the skiplist, cuts sorted set overhead by up to 43%

https://valkey.io/blog/from-skiplist-to-b+tree/
58•reconditerose•3d ago•27 comments

Unforced Navier-Stokes Singularity and the Generalized Riemann Hypothesis

https://terrytao.wordpress.com/2026/11/20/unforced-euler-singularity-unforced-navier-stokes-singu...
363•archon1410•9h ago•147 comments

Language Models Can Rewrite Their Own Weights

https://arxiv.org/abs/2611.21885
353•lwhsiao•10h ago•113 comments

Sir Brian May adds new dimension to Hera asteroid

https://www.esa.int/articles/about-us/directorates/operations/space-safety/hera/sir-brian-may-add...
46•mooreds•3h ago•11 comments

DreamCoder: Coding harness and IDE from the author of Mastering Emacs

https://www.masteringemacs.org/article/announcing-dreamcoder
128•facundo_olano•7h ago•24 comments

Finite-time blow-up for the Navier–Stokes equations

https://github.com/tristanbuckmaster/navier-stokes/
261•trotro•11h ago•84 comments

And yet, it moves

https://signoregalilei.com/2026/11/20/and-yet-it-moves/
107•notfried•7h ago•52 comments

Restricting AI agents in mobile operating systems platforms from capturing data

https://signal.org/blog/protecting-your-privacy/
523•dotcoma•17h ago•309 comments

Re: Riemann

https://borretti.me/article/re-riemann
152•_dain_•3h ago•139 comments

Vibecoding an inference chip

https://ankitg.me/blog/vibecoding-inference-chip/
119•ankitvgupta•9h ago•43 comments

The stochastic parrots have apparently proved the Riemann Hypothesis

https://scottaaronson.blog/?p=10344
224•stratos123•7h ago•202 comments

How to Bypass Rust's Orphan Rule

https://edvong.com/bypass-orphan-rule/
20•todsacerdoti•2d ago•3 comments

Winedroid – run Android apps without Android (WASM demo)

https://winedroid.soham.sh/demo/
109•sohzm•7h ago•35 comments

Serious cyberattack thought backed by wealthy operators

https://www.ruv.is/english/2026-11-20-serious-cyberattack-thought-backed-by-wealthy-operators-493411
25•Bender•3h ago•10 comments

The Hungry Butler Effect: Concealed Commercial Incentives in LLMs

https://www.researchgate.net/publication/418143113_The_Hungry_Butler_Effect_Concealed_Commercial_...
21•undefinedmethod•5h ago•8 comments

Integrals of Polynomials with Galois Group S_p

https://m-slee.netlify.app/posts/integrals-galois-polynomials
6•richard_chase•2h ago•1 comments

Why the Airbus A321XLR often gets stuck climbing for first few hours of flight

https://simpleflying.com/airbus-a321xlr-often-stuck-climbing-1st-few-hours-flight/
93•edward•6d ago•61 comments

I can’t believe it’s 1!

https://aperiodical.com/2026/11/i-cant-believe-its-1/
47•ColinWright•4d ago•55 comments

Show HN: I made an audio-to-MIDI model 60x more efficient

https://kobi.music/research/dettato
247•hidude562•4d ago•52 comments

US SEC's Nvidia probe is examining potential accounting issues – sources

https://www.marketscreener.com/news/us-sec-s-nvidia-probe-is-examining-potential-accounting-issue...
245•ftchd•6h ago•130 comments

Show HN: OpenComputer – an open source alternative to Devin

https://github.com/diggerhq/opencomputer
72•iacguy•6h ago•22 comments

I'm Peter Roberts, immigration attorney who does work for YC and startups. AMA

99•proberts•10h ago•137 comments

Show HN: An API for word timestamps from speech, 75% within 20ms

https://quebec.yarn.so
69•jasperstory•3d ago•11 comments

Merkle Tree Certificates

https://datatracker.ietf.org/doc/draft-ietf-plants-merkle-tree-certs/
22•mooreds•4h ago•3 comments

Forking TuxGuitar into a 17-EDO tab editor

https://frogwizard.bearblog.dev/tuxguitar-17edo/
6•speckx•2d ago•6 comments