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.

Kimi K3.1 Pro and K3.1 Pro Highspeed

https://www.kimi.ai/blog/kimi-k3-1-pro
808•Jowsey•12h ago•485 comments

Twelve years of distributed SQLite

https://philipotoole.com/twelve-years-of-distributed-sqlite/
93•otoolep•3h ago•9 comments

Apple Music 404 Not Found

https://music.apple.com/this-url-doesnt-exist
672•frizlab•16h ago•212 comments

How close can pure functional programming get to C?

https://rtfeldman.com/fp-vs-c
14•stmonty•1h ago•1 comments

Local Wikipedia: Wikipedia, but only for things a half hour walk from you

https://edjefferson.com/localwiki/
1316•sohkamyung•1d ago•228 comments

Dessa: Conversation Model trained only on speech

https://block.xyz/dessa
310•dsr12•10h ago•116 comments

CVE Tracker

https://cve.0xosprey.com/
136•Group_B•7h ago•55 comments

Today, we hacked what we'd set out to hack: the iPhone

https://blog.calif.io/p/iphone
800•pavel_lishin•12h ago•526 comments

Bringing uv and ty together

https://astral.sh/blog/uv-ty-integration
73•Ravencentric•3d ago•19 comments

The Quake Fan who built the map editor everybody uses

https://funcarena.com/articles/the-quake-fan-who-built-the-map-editor-everybody-uses
40•klaussilveira•1d ago•2 comments

The Real Kevin Bacon

https://pudding.cool/2026/10/kevin-bacon/
272•xnx•3d ago•75 comments

Canadians are running out of time to oppose Bill C-22

https://www.eff.org/deeplinks/2026/10/canadians-are-running-out-time-oppose-dangerous-c-22-survei...
377•hn_acker•1d ago•204 comments

One Man's Pan Am 747 Replica [video]

https://www.youtube.com/watch?v=vAFwHGGfPz4
10•Teever•7h ago•0 comments

C vs. C#: racing VLC's Gaussian blur

https://martinfinkel.com/blog/gaussian-blur-c-vs-csharp/
46•martz•2d ago•2 comments

Show HN: Corgi, a helpful {agent, terminal} multiplexer

https://corgi.computer/
61•eiopa•9h ago•14 comments

The past, present, and future share a show floor at Pinball Expo 2026

https://arstechnica.com/gaming/2026/10/the-past-present-and-future-share-a-show-floor-at-pinball-...
20•civicpivot•2d ago•3 comments

Air Purifiers as White Noise Machines

https://www.jefftk.com/p/air-purifiers-as-white-noise-machines
91•luu•11h ago•103 comments

UUIDv7: Timestamps aren’t time-ordered and other oddities

https://alexsci.com/blog/uuidv7-is-weird/
121•ibobev•12h ago•103 comments

Evolving Threat: http2shell is an RCE in Node, Python, and more

https://nitter.click/AikidoSecurity/status/2113759110625906950?__galadriel=lPmlGl9id4oAd5UeBwMhlY...
129•cowlevel•5h ago•70 comments

A Bicycle Journey Across Brazil to Find a Vanishing Savanna

https://e360.yale.edu/features/2026-film-contest-second-place-cerrado
37•throwaway2037•2d ago•9 comments

Alzheimer's breakthrough: 2 hrs of sleep restored without clearing brain plaques

https://www.sciencedaily.com/releases/2026/10/261018084421.htm
56•amichail•3h ago•13 comments

A practical guide to AI image generation, from diffusion to agents

https://danielball.com/notes/ai_image_generation.html
10•westsmith•3d ago•4 comments

Rune 1.3: adds TypeScript, a Helix editor, Kitty graphics and OSC-7501

https://rune.build/blog/rune-1-3-release
46•ernestrc•9h ago•11 comments

Rise of collagen-eating microbes may help explain gaps in the fossil record

https://www.cam.ac.uk/research/news/rise-of-collagen-eating-microbes-may-help-explain-gaps-in-the...
23•gmays•3d ago•0 comments

How is music possible

https://www.henrikkarlsson.xyz/p/music
95•rafaelc•12h ago•72 comments

Is this the new cyber normal?

https://drew.silcock.dev/blog/is-this-the-new-cyber-normal/
151•drewsberry•18h ago•168 comments

A whirlwind tour of systemd-nspawn containers (2025)

https://quantum5.ca/2025/03/22/whirlwind-tour-of-systemd-nspawn-containers/
39•ibobev•8h ago•7 comments

Animations for System Builders

https://brooker.co.za/blog/2026/10/23/animations.html
51•KraftyOne•12h ago•3 comments

DNS: A Replacement for Finger

https://altchan.org/sh/42
34•Bender•10h ago•26 comments

Password, Please: Border Searches of Laptops, Phones Up 32% This Year

https://gizmodo.com/password-please-border-searches-of-laptops-phones-up-32-this-year-2000830213
21•gumby•3h ago•2 comments