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.

Organic Maps

https://organicmaps.app/
232•tosh•2h ago•54 comments

Introduction to Compilers and Language Design

https://dthain.github.io/books/compiler/
136•AlexeyBrin•4h ago•16 comments

Run Windows 2000 on a DEC Alpha with a new es40 fork

https://raymii.org/s/blog/Run_Windows_2000_for_Dec_Alpha_on_a_new_es40_fork.html
28•jandeboevrie•2h ago•5 comments

Airplane Boneyards List and Map

https://airplaneboneyards.com/airplane-boneyards-list-and-map.htm
32•hyperific•1d ago•3 comments

Medieval-style fortifications are back in the Sahel

https://www.economist.com/middle-east-and-africa/2026/06/25/medieval-style-fortifications-are-bac...
41•andsoitis•4d ago•25 comments

Shadcn/UI now defaults to Base UI instead of Radix

https://ui.shadcn.com/docs/changelog
226•dabinat•11h ago•118 comments

Rayfish, Peer-to-peer mesh VPN with no server to trust

https://rayfish.xyz/blog/01-introducing-rayfish
22•captain_dfx•4d ago•8 comments

Pandoc Lua Filters

https://pandoc.org/lua-filters.html
113•ankitg12•2d ago•10 comments

Phosh 0.56.0

https://phosh.mobi/releases/rel-0.56.0/
117•edward•2h ago•34 comments

Solar rail could become common in Europe after successful trial in Switzerland

https://www.euronews.com/2026/07/05/italy-could-be-the-next-country-to-build-a-solar-railway-afte...
24•neilfrndes•1h ago•11 comments

The GNU Emacs Architecture: Unlocking the Core [pdf]

https://www.diva-portal.org/smash/get/diva2:2052282/FULLTEXT01.pdf
127•cenazoic•4d ago•8 comments

Cannabis Users Face Substantially Higher Risk of Heart Attack (2025)

https://www.acc.org/about-acc/press-releases/2025/03/17/15/35/cannabis-users-face-substantially-h...
98•RickJWagner•4h ago•120 comments

Show HN: KiCad in the Browser

https://demo.pcbjam.com/
46•ViktorEE•4h ago•20 comments

Trust your compiler: Modern C++

https://categorica.io/blog/2026.06.29_trust_your_compiler/
51•foxhill•4d ago•31 comments

Fast Software, the Best Software (2019)

https://craigmod.com/essays/fast_software/
91•ustad•8h ago•49 comments

If you're a button, you have one job

https://unsung.aresluna.org/if-youre-a-button-you-have-one-job/
426•nozzlegear•14h ago•212 comments

Knowledge Should Not Be Gated

https://www.formaly.io/blog/knowledge-should-not-be-gated
65•nezhar•8h ago•43 comments

Megawatts by Microwave

https://computer.rip/2026-07-04-microwave-and-power.html
58•eternauta3k•10h ago•5 comments

Moby Dick Workout (2022)

https://www.hogbaysoftware.com/posts/moby-dick-workout/
83•helloplanets•11h ago•27 comments

EU Council forces Chat Control via fast-track

https://www.heise.de/en/news/Chat-Control-1-0-EU-Council-forces-messenger-scans-via-fast-track-11...
99•stavros•4h ago•26 comments

Command and Conquer Generals natively ported to macOS, iPhone, iPad using Fable

https://github.com/ammaarreshi/Generals-Mac-iOS-iPad/tree/main
620•asronline•20h ago•261 comments

Artful Cats: Feline-Inspired Art and Artifacts

https://www.si.edu/spotlight/art-cats
72•jruohonen•3d ago•5 comments

Meta's Un-Stable Signature

https://hackerfactor.com/blog/index.php?/archives/1098-Metas-Un-Stable-Signature.html
124•ementally•3d ago•20 comments

Atomic Force Microscope [video]

https://www.youtube.com/watch?v=DyIQkqBXhS0
106•mhb•2d ago•15 comments

The Log is the Agent

https://arxiv.org/abs/2605.21997
82•iacguy•13h ago•30 comments

Functional Programming in hica

https://www.hica.dev/docs/functional-programming/
38•cladamski79•3d ago•9 comments

GPT-5.5 Codex reasoning-token clustering may be leading to degraded performance

https://github.com/openai/codex/issues/30364
332•maille•18h ago•134 comments

About the Digital Art

https://www.tricivenola.com/about-the-digital-art/
28•NaOH•3d ago•11 comments

Autonomous flying umbrella follows and shields users from rain and sunlight

https://www.designboom.com/technology/autonomous-flying-umbrella-follows-users-rain-sunlight-i-bu...
3•amichail•30m ago•1 comments

Return of the Nigerian Prince Redux: Beware Book Club and Book Review Scams (2025)

https://writerbeware.blog/2025/09/19/return-of-the-nigerian-prince-redux-beware-book-club-and-boo...
69•Anon84•15h ago•24 comments