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.

Why don't more developers “use the platform”?

https://nolanlawson.com/2026/10/03/why-dont-more-developers-use-the-platform/
105•vinhnx•4h ago•70 comments

Tell HN: Bob Cringely has died

386•paveworld•7h ago•77 comments

So you think you could be an electrician?

https://asteriskmag.com/issues/15/so-you-think-you-could-be-an-electrician
185•zdw•3d ago•119 comments

The work by Valve's Timur Kristóf on improving old AMD GPUs on Linux

https://www.phoronix.com/news/XDC-2026-Valve-Timur-AMDGPU
251•speckx•13h ago•36 comments

Treachery in the Rodin Museum 3D scan verdict

https://cosmowenman.substack.com/p/rodin-museum-3d-scan-verdict
174•CosmoWenman•14h ago•81 comments

We're going to need default hard budget caps on pretty much everything

https://simonwillison.net/2026/Oct/3/default-hard-budget-caps/
398•elffjs•8h ago•201 comments

Agents don't need memory, they need documentation

https://liao.gg/blog/agents-dont-need-memory
148•kmeh•15h ago•89 comments

Hole Punch: Sling your spaceship around gravitational fields

https://notoriousbfg.com/hole-punch/
279•trwhite•14h ago•66 comments

We're working on a new RuneScape MMO

https://play.runescape.com/4
48•droidjj•7h ago•16 comments

Celebrating the 100th birthday of the kidney donated to him as a teenager

https://www.whec.com/top-news/webster-man-celebrating-the-100th-birthday-of-the-kidney-his-mom-do...
192•gscott•2d ago•46 comments

Religious scholars met with Anthropic

https://www.nytimes.com/2026/09/29/us/anthropic-claude-morals-ai.html
65•bookofjoe•5h ago•108 comments

Reasons I didn't become an EMT, ranked

https://ben.stolovitz.com/posts/reasons-not-emt-ranked/
143•citelao•11h ago•70 comments

Math's pedagogical curse – Grant Sanderson [video]

https://www.youtube.com/watch?v=UOuxo6SA8Uc
39•bobajeff•1d ago•15 comments

LeCun has "zero concerns" about AI wiping out humanity, recent "rogue" incidents

https://fortune.com/2026/10/01/ai-godfather-yann-lecun-has-zero-concerns-about-human-extinction-s...
102•Anon84•14h ago•129 comments

We want you to build the next Git platform on Cloudflare

https://blog.cloudflare.com/next-git-platform-on-cloudflare/
118•geoffbp•12h ago•114 comments

Three AI agents, two countries, and one uneven world wide web

https://royapakzad.substack.com/p/multilingual-ai-agents
29•effects•1d ago•1 comments

Your body of work thinks back at you

https://photoni.st/index.php/2026/09/25/your-body-of-work-thinks-back-at-you/
54•surprisetalk•3d ago•4 comments

How to hack time, with C2PA

https://www.da.vidbuchanan.co.uk/blog/hacking-time.html
33•Retr0id•13h ago•4 comments

FTL: A new operating system for clouds

https://ftl-os.org/
172•romac•17h ago•67 comments

Dirty Optimization Secrets (C for Playdate)

https://devforum.play.date/t/dirty-optimization-secrets-c-for-playdate/23011
5•ibobev•1d ago•0 comments

Former SR-71 engineer talks NASA's Blackbird revival program

https://www.twz.com/air/former-sr-71-engineer-talks-nasas-blackbird-revival-program
52•rkagerer•1d ago•33 comments

I quit OpenAI because its culture is broken

https://www.theatlantic.com/technology/2026/10/openai-safety-team-resignation/688881/?gift=v5U_Uz...
202•Brajeshwar•18h ago•465 comments

Show HN: Thoreau BASIC – What if BASIC hadn't gone out of fashion?

https://thoreaubasic.com/
54•Gorsefound•1d ago•29 comments

What Meta got right with Muse

https://metedata.substack.com/p/what-meta-got-right-with-muse
42•young_mete•13h ago•53 comments

City building games have a Soul Problem pt.2

https://www.radical-elements.com/minor-epiphanies/city-building-games-have-a-soul-problem-pt2
129•lexx•16h ago•126 comments

Show HN: Pi pod – Run your pi coding agent in sandboxes on your own server

https://pipod.dev/
94•edverma2•1d ago•39 comments

Surely you have ultra-wideband radios on your bins too?

https://sjg.io/writing/binrange-have-you-actually-put-the-bins-out/
71•simonjgreen•11h ago•37 comments

Amazon introduces a redesigned Kindle family

https://www.aboutamazon.com/news/devices/new-kindle-lineup-2026
30•seam_carver•2d ago•49 comments

Woking Electrical Control Room (2016)

http://www.darbiansphotography.com/woking-electrical-control-room-urbex
141•NaOH•1d ago•27 comments

Docker has always used microVMs (well since 2016)

https://dave.recoil.org/docker-has-always-used-microvms/
36•avsm•16h ago•29 comments