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.

Jerry's Map

http://www.jerrysmap.com/the-map
263•turtleyacht•3h ago•33 comments

United Wizards of the Coast recognized by NLRB

https://unitedwizardsofthecoast.com/news/2026-06-23-we-have-a-union
30•d4mi3n•40m ago•3 comments

Don't verify email addresses by sending spam to them

https://milek7.pl/mailverifyspam/
95•garaetjjte•2h ago•20 comments

California AB 2047 makes 3d printers off-limits to students, educators, business

https://www.the3dprintingnerd.com/ab2047
10•Buildstarted•25m ago•0 comments

Swift Package Index joins Apple

https://swiftpackageindex.com/blog/swift-package-index-joins-apple
145•JDevlieghere•4h ago•46 comments

FUTO Swipe – A new swipe typing model

https://swipe.futo.tech/
164•futohq•4h ago•54 comments

Printing Gaussian Splats

https://www.patreon.com/DanyBittel/posts/printing-splats-161333338
97•ilnmtlbnm•2d ago•6 comments

The worthlessness of Vitamin D is mildly exaggerated

https://dynomight.net/vitamin-d/
146•surprisetalk•6h ago•108 comments

Show HN: TikZ Editor – WYSIWYG editor for figures in LaTeX

https://tikz.dev/editor/
291•DominikPeters•8h ago•58 comments

AI's Affordability Crisis

https://blog.dshr.org/2026/06/ais-affordability-crisis.html
202•ilreb•7h ago•257 comments

F3

https://github.com/future-file-format/f3
575•tosh•5h ago•126 comments

Trains halted across Germany because of communication system problem

https://apnews.com/article/germany-trains-halted-communications-radio-problem-deutsche-bahn-e8fd9...
99•sva_•1h ago•89 comments

Unlimited OCR: One-shot long-horizon parsing

https://github.com/baidu/Unlimited-OCR
419•ingve•11h ago•96 comments

The deadly rise of giant trucks and SUVs

https://www.nytimes.com/interactive/2026/06/21/us/trucks-suv-pedestrian-crashes.html
333•xnx•1d ago•541 comments

Algorithmic Monocultures in Hiring

https://hai.stanford.edu/news/ai-hiring-tools-can-yield-racial-bias-and-systemic-rejection
116•sizzle•3h ago•112 comments

F* file system – file search that reads SSD directly bypassing OS kernel

https://github.com/dmtrKovalenko/ffs
13•neogoose•2d ago•16 comments

Five monitors on a Commodore 128 [video]

https://www.youtube.com/watch?v=ul5hC3PY1Yg
93•EvanAnderson•1d ago•17 comments

The Coming Loop

https://lucumr.pocoo.org/2026/6/23/the-coming-loop/
271•ingve•11h ago•212 comments

Lift4D: Harmonizing Single-View 3D Estimation for 4D Reconstruction In-the-Wild

https://lift4d.github.io/
100•ilreb•7h ago•9 comments

Samsung demonstrates 3D stacked FETs with triple nanosheet channels at 42nm

https://semiconductor.samsung.com/news-events/tech-blog/from-gaa-to-3d-stacked-fet-expanding-the-...
78•its_ajseven•4d ago•23 comments

San Diego photologs from the 1970s

https://www.beautifulpublicdata.com/san-diego-photologs-from-the-1970s/
135•jonathanmkeegan•5h ago•46 comments

On the Skin-Furrows of the Hand (1880) [pdf]

https://galton.org/fingerprints/faulds-1880-nature-furrows.pdf
4•pncnmnp•1d ago•1 comments

Plotnine

https://plotnine.org/
246•tosh•4d ago•74 comments

ATProto Permissioned Data Proposal Draft

https://github.com/bluesky-social/proposals/pull/94
7•danabramov•1h ago•1 comments

Claude Tag

https://www.anthropic.com/news/introducing-claude-tag
217•adocomplete•5h ago•139 comments

Digital euro clears key hurdle as EU seeks to break free from U.S. credit cards

https://finance.yahoo.com/markets/currencies/articles/ecb-secures-key-parliamentary-backing-10271...
149•madars•6h ago•223 comments

Fired by Google for creating the Google workspace CLI

https://twitter.com/JPoehnelt/status/2069482265953087602
164•justinwp•4h ago•108 comments

Mistral OCR 4

https://mistral.ai/news/ocr-4/
412•meetpateltech•8h ago•106 comments

In memory of the man who put red and green squiggles under words

https://devblogs.microsoft.com/oldnewthing/20260622-00/?p=112451
21•saikatsg•4h ago•0 comments

Performance Improvements in Libffi

https://atgreen.github.io/repl-yell/posts/libffi-plan-cache/
35•atgreen•2d ago•6 comments