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.

Felony Bench

https://www.felonybench.com/
671•colinprince•16h ago•268 comments

Rust Glancer: Rust LSP using 100x less RAM

https://rust-glancer.github.io/blog/hello-world/
148•matklad•12h ago•34 comments

Kobo can run apps now

https://bandarlabs.github.io/Cobalt/
521•thepoet•15h ago•182 comments

There's no reason for software to be slow anymore

https://danluu.com/perf-opt/
351•Jach•6h ago•251 comments

Optimizing meshoptimizer to process billions of triangles in minutes (2025)

https://zeux.io/2025/09/30/billions-of-triangles-in-minutes/
16•corysama•14h ago•0 comments

Felony charges for citizen deleting phone data at US Border

https://www.nytimes.com/2026/08/21/us/politics/samuel-tunick-deleted-phone-felony.html
742•floathub•19h ago•868 comments

Three important steps in my maturation process

https://thomasdullien.github.io/posts/2026-08-21-three-important-steps-in-my-maturation-process/
126•tdullien•9h ago•52 comments

Kagi added a setting for removing paywalled links from search results

https://kagi.com/changelog#11296
1101•speckx•18h ago•359 comments

I accidentally logged hundreds of thousands of phone calls to military bases

https://lina.sh/blog/hijacking-e164-arpa
526•gavide•18h ago•59 comments

OTel isn’t going well

https://matduggan.com/otel-isnt-going-well-and-i-made-a-spreadsheet-about-it/
100•hn_acker•14h ago•38 comments

Zig’s io.threaded is neat

https://matklad.github.io/2026/08/06/neat-io-threaded.html
54•chilipepperhott•17h ago•16 comments

Scientists release biggest 2D map of the universe

https://newscenter.lbl.gov/2026/08/10/scientists-release-biggest-2d-map-of-the-universe/
192•NKosmatos•13h ago•55 comments

Canada will match US tariffs 'dollar for dollar' as trade talks break down

https://www.bbc.com/news/articles/cvgvyy4x2mvo
40•tartoran•1h ago•7 comments

AI boosted homework scores, then exam scores dropped: study

https://www.economist.com/graphic-detail/2026/08/18/does-ai-stop-children-from-learning
295•dash2•3d ago•311 comments

Stop Making TUIs

https://sockpuppet.org/blog/2026/08/20/stop-making-tuis/
106•underdeserver•1d ago•169 comments

People of ACM – Russ Cox

https://www.acm.org/articles/people-of-acm/2026/russ-cox
129•signa11•5d ago•13 comments

Initial focus for our partnership with Motorola is a regular non-folding device

https://grapheneos.social/@GrapheneOS/117136278553665985
103•Cider9986•6h ago•33 comments

A revisit of remote Spectre attacks on Cloudflare Workers

https://blog.cloudflare.com/revisiting-spectre-attacks-on-workers/
47•albertpedersen•2d ago•0 comments

Claudette: Make Claude stop talking like a BuzzFeed article

https://github.com/adnanakil/nobuzz/blob/main/README.md
253•aakil•17h ago•175 comments

Early-life stress leaves a 'scar' inside brain cells in mice

https://medicine.washu.edu/news/how-early-life-stress-leaves-a-scar-inside-brain-cells/
83•gmays•1d ago•31 comments

Show HN: OzBrain, a shared brain for knowledge between agents and your team

https://ozbrain.com
66•dariusmonsef•8h ago•37 comments

I'm becoming AI-blind

https://cymerys.com/w/im-becoming-ai-blind
346•rcymerys•20h ago•349 comments

New Worlds: We are living in the future of J.G. Ballard or William Gibson

https://precastreinforced.co.uk/2026/08/16/new-worlds/
232•speckx•18h ago•165 comments

How we made a text-to-speech model respond in sub-50 ms

https://nari-labs.com/blog/qwen3-tts-speed-cost-frontier/
139•toebee•16h ago•33 comments

HN: The Good Parts (2016)

https://danluu.com/hn-comments/
41•adletbalzhanov•8h ago•8 comments

Everyone says assembly is untyped—everyone is wrong

https://www.gingerbill.org/article/2026/08/20/designing-odins-inline-asm/
69•adamrezich•1d ago•25 comments

A look under our trunk: what's in our compute

https://waymo.com/blog/2026/08/look-under-our-trunk/
121•ra7•1d ago•67 comments

SalesPatriot (YC W25) Is Hiring Forward Deployed Engineers

https://www.ycombinator.com/companies/salespatriot/jobs/M46X6YX-forward-deployed-engineer
1•maciejSz•11h ago

The coolest anti-surveillance tools at Defcon [video]

https://www.youtube.com/watch?v=-2uAsJ5EPAw
192•neom•3d ago•28 comments

Ozone: The fault is not in our trees, but in ourselves

https://www.science.org/content/blog-post/fault-not-our-trees-ourselves
24•gumby•1d ago•1 comments