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.

How I use LLMs to learn complex topics

https://laurentiugabriel.github.io/blog/articles/how-i-use-llms-to-learn/
277•laurentiurad•3h ago•154 comments

The Hacker's Renaissance

https://phrack.org/issues/72/19#article
63•yu3zhou4•2h ago•44 comments

New Zealand lost its music media, and what we're building to replace it

https://propelmusic.co.nz/articles/the-sound-went-quiet-nz-music-media
39•berghoffer•2h ago•17 comments

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

141•david927•5h ago•530 comments

Mea Culpa – Dark Hours

https://blog.terrygodier.com/2026/08/09/mea-culpa-dark-hours.html
521•satvikpendem•9h ago•239 comments

The tragedy of the commons, AI edition

https://www.economist.com/britain/2026/08/06/the-tragedy-of-the-commons-ai-edition
53•simonpure•3h ago•21 comments

Tuxedo No. 2 – Cocktail recipes

https://tuxedono2.com
19•smartmic•2h ago•4 comments

Cool URIs Don't Change (1998)

https://www.w3.org/Provider/Style/URI
158•Klaster_1•8h ago•29 comments

Taxi drivers rarely die of Alzheimer's

https://theconversation.com/taxi-drivers-rarely-die-of-alzheimers-how-complex-mental-maps-and-spa...
135•jader201•7h ago•112 comments

OpenChamber: An Agentic Development Environment

https://openchamber.dev/
89•hexomancer•5h ago•48 comments

Andrew Wiles on proving Fermat’s Last Theorem (1995) [video]

https://www.youtube.com/watch?v=GS7CxAtV5Ks
18•jackdoe•3d ago•11 comments

I made tinnitus my friend, then it disappeared [video]

https://mynoise.net/vlog.php?ep=20260803
37•gregsadetsky•4h ago•14 comments

The main way I've seen people turn ideologically crazy (2025)

https://blog.andymasley.com/p/the-main-way-ive-seen-people-turn
68•mef•2h ago•46 comments

How Golden Is Silence, Actually?

https://www.newyorker.com/magazine/2026/08/10/silence-kate-mcloughlin-book-review
27•tintinnabula•3d ago•9 comments

Everything you do is being recorded

https://www.theatlantic.com/technology/2026/05/ai-wearable-surveillance-countermeasures/687203/
167•ike_usawa•11h ago•139 comments

When Compilers Disagree About UTF‑8

https://nemanjatrifunovic.substack.com/p/when-compilers-disagree-about-utf8
25•rbanffy•4d ago•5 comments

Touring the Consensus, Six Months In

https://theconsensus.dev/blog/2026/08/05/touring-the-consensus-six-months-in.html
4•tosh•3d ago•0 comments

Georgia police officers fired after Flock camera misuse

https://www.wtoc.com/2026/08/07/savannah-police-department-fires-6-employees-over-flock-safety-sy...
25•chirau•1h ago•4 comments

John C. Lilly on solid state intelligence and the elimination of man (1978)

https://kibotronics.net/unlisted/lilly-machines/
112•Kiboneu•9h ago•71 comments

Windows 11's built-in Weather app wastes more than 1 GB of RAM

https://www.notebookcheck.net/Windows-11-s-built-in-Weather-app-wastes-more-than-1-GB-of-RAM.1364...
307•akyuu•7h ago•258 comments

Show HN: Alphabet Soup, a multiplayer game, build the longest word to win

https://alphabetsoup.club
17•johnchinjew•2h ago•10 comments

Show HN: A Project Oberon System version running on RISC-V instead of RISC-5

https://github.com/rochus-keller/OberonSystem/tree/op2-rv32
99•Rochus•10h ago•16 comments

The climate benefits of retiring an operational combustion engine vehicle

https://www.science.org/doi/10.1126/science.adv5441
11•toomuchtodo•2h ago•12 comments

Two wheels, a few tradeoffs, and gas prices

https://blog.raed.dev/posts/motocycle/
15•Raed667•3h ago•14 comments

Crickets as pets

https://en.wikipedia.org/wiki/Crickets_as_pets
71•Michelangelo11•4d ago•33 comments

Reviving a four year old reMarkable 2

https://oskrim.github.io/hardware/2026/08/09/remarkable-over-ssh.html
109•tremguy•11h ago•83 comments

There Are Magic Hexagons of Every Order

https://gukov.dev/math/2026/08/02/new-magic-hexagons.html
176•gukoff•15h ago•28 comments

To Recruit Teachers, School Districts Are Building Homes

https://www.nytimes.com/2026/07/28/business/affordable-housing-schools-teachers.html
10•mooreds•1h ago•0 comments

Human vs. AI – Diff-based line-level provenance for text under agentic editing

https://github.com/eighttrigrams/us-vs-them
40•eighttrigrams•7h ago•9 comments

Microsoft Word for Windows 1.1a, Native X64 Port

https://github.com/jmarshall23/msword
161•BruceEel•17h ago•66 comments