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.

Fable 5.1 Solves the Cyphral Distich, a 370-year-old cipher

https://www.vals.ai/blogs/fable-solves-cyphral-distich
417•u1hcw9nx•4h ago•165 comments

Why is Google still serving dodgy ads?

https://www.atomic14.com/2026/09/13/why-is-google-still-serving-dodgy-ads
561•iamflimflam1•7h ago•266 comments

Registration without a phone number on Signal will use zero-knowledge proofs

https://community.signalusers.org/t/registration-without-a-phone-number/2222?page=10
58•Cider9986•3h ago•26 comments

Julia 1.13 highlights

https://julialang.org/blog/2026/09/julia-1.13-highlights/
147•eigenspace•3d ago•11 comments

Astra and Fable still hack on simple variants of alignment evals from 2025

https://www.lesswrong.com/posts/munJKF7iWMsWJLAH2/astra-and-fable-still-hack-on-simple-variants-o...
368•Levitating•10h ago•174 comments

Data collected by cars and sold to third parties

https://www.theverge.com/column/994172/your-car-is-selling-your-data
294•bookofjoe•11h ago•154 comments

Flawed routers flood University of Wisconsin internet time server (2003)

https://pages.cs.wisc.edu/~plonka/netgear-sntp/
50•walrus01•4h ago•6 comments

The GDR and Vietnam: From Fake Coffee to Coffee Empire

https://www.katjahoyer.uk/p/the-gdr-and-vietnam-from-fake-coffee
22•NaOH•3d ago•5 comments

Ask HN: What are you working on? (September 2026)

76•david927•7h ago•146 comments

Mullenweg has returned as CEO after attempted board ouster

https://techcrunch.com/2026/09/12/automattic-confirms-mullenweg-has-returned-as-ceo-after-attempt...
29•ilamont•4h ago•67 comments

The Coming War on General Computation (2011)

https://en.wikisource.org/wiki/The_Coming_War_on_General_Computation
30•gregsadetsky•1h ago•3 comments

Open-Source AI and Open Models Reading List

https://www.interconnects.ai/p/open-source-ai-reading-list
4•simonpure•46m ago•0 comments

Making Startups Powerful

https://paulgraham.com/powerful.html
152•tosh•10h ago•71 comments

Reverse engineering my e-scooter and rewriting the firmware in Rust

https://bensimms.moe/reverse-engineering-scooter/
368•vinhnx•3d ago•88 comments

I build a mechanical watch face: a real gear train for a watch with no gears

https://myday24.com/blog/how-a-mechanical-watch-face-is-built/
10•tomasslavicek•3d ago•4 comments

I added a non-wi-fi Mitsubishi AC to Home Assistant

https://medium.com/@ivangomezarnedo/how-i-added-a-non-wi-fi-mitsubishi-ac-to-home-assistant-22770...
170•ichacas•3d ago•84 comments

Why is the x86 undefined instruction called ud2? Why 2?

https://devblogs.microsoft.com/oldnewthing/20260910-00/?p=112689
198•ibobev•12h ago•47 comments

The contagion of fear

https://bcantrill.dtrace.org/2026/09/13/the-contagion-of-fear/
122•elffjs•2h ago•84 comments

Why is privacy so hard?

https://cacm.acm.org/blogcacm/why-is-privacy-so-hard/
19•andsoitis•3h ago•13 comments

CUDA for AMD on Windows

https://github.com/Speedstu/CUDA-for-AMD-Windows
135•chiassedu80•10h ago•69 comments

Necker, 1832: "An optical phænomenon on viewing a figure of a geometrical solid"

https://zenodo.org/records/1430991
3•akkartik•1d ago•1 comments

Reverse-Engineering Claude Web's MicroVM: Uncovering Anthropic's Hidden Antspace

https://aprilnea.me/en/blog/reverse-engineering-claude-code-antspace
54•rzk•2d ago•12 comments

Sean Carroll explains the biggest ideas in the universe – Full Interview [video] (2025)

https://www.youtube.com/watch?v=_TBNJyztai0
73•binyu•3d ago•17 comments

Bad Code Is Kudzu

https://vickiboykis.com/2026/09/01/bad-code-is-kudzu/
35•surprisetalk•3d ago•11 comments

Garry Tan wants US open-weight AI labs to 'distill' frontier models, too

https://techcrunch.com/2026/09/11/y-combinators-garry-tan-wants-u-s-open-weight-ai-labs-to-distil...
349•TheJCDenton•9h ago•185 comments

Mark Zuckerberg: "Cambridge Analytica" (2017)

https://twitter.com/TechEmails/status/2099214399840059428
270•mfiguiere•5h ago•113 comments

Base84 deserves a place in file names

https://00f.net/2026/09/09/base84/
58•kevvok•2d ago•50 comments

JetKVM Mini

https://jetkvm.com/blog/introducing-jetkvm-mini
525•taubek•17h ago•212 comments

David Sacks: OpenAI and Anthropic Don't Need Regulations to Pace Frontier Models

https://twitter.com/DavidSacks/status/2098973625252708460
241•kolanos•8h ago•184 comments

Device Drivers lab exercise – COSC562

https://web.eecs.utk.edu/~smarz1/courses/cosc562/drivers.html
29•azhenley•7h ago•2 comments