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.

CellGemma – Model built to help accelerate discovery of new cancer therapies

https://deepmind.google/models/gemma/cellgemma/
106•estebandalelr•1h ago•21 comments

Amazon Ergo: A fast, cost-effective open-weights model

https://www.aboutamazon.com/amazon-ergo
1491•mtw14•16h ago•597 comments

Code-driven AI illustrations are more useful than images from image models

https://simonwillison.net/2026/Nov/28/code-driven-ai-illustrations/
225•gmays•8h ago•119 comments

TinyGo 0.45.0

https://github.com/tinygo-org/tinygo/releases/tag/v0.45.0
164•iaaan•6h ago•14 comments

Minimal dark mode styles for Hacker News

https://antonz.org/hn-dark-mode/
42•ingve•3h ago•28 comments

OpenMono: The open-source mono font

https://openmono.com/
182•hn-aarvin•7h ago•57 comments

Advent of Code 2026

https://adventofcode.com/
539•saisundar•1d ago•592 comments

Paprika Powers This Village in Serbia

https://www.nytimes.com/interactive/2026/11/28/dining/paprika-pepper-serbia.html
57•mooreds•2d ago•15 comments

Bacteria linked to inflammatory bowel disease identified with help from gamers

https://today.ucsd.edu/story/bacteria-linked-to-inflammatory-bowel-disease-identified-with-help-f...
271•gmays•13h ago•96 comments

Ty 1.0: Fast Python type checker and language server

https://astral.sh/blog/ty-1.0
596•burntsushi•17h ago•138 comments

Michael Levin's "Platonic Space Hypothesis" aims to explain emergent behavior

https://www.quantamagazine.org/does-behavior-emerge-from-beyond-the-physical-world-20261130/
201•ivell•16h ago•137 comments

ATProto's private data protocol is now in beta

https://atproto.com/blog/the-spaces-public-beta-is-now-live
328•pfraze•15h ago•144 comments

Cryptopocalypse

https://www.argmin.net/p/cryptopocalypse
617•_delirium•18h ago•323 comments

Microcontrôland: A fictional country simulated by 7 ESP32 microcontrollers

https://www.microcontroland.org/en/projet.html
64•dverlaeckt80•12h ago•21 comments

Repository Is Maintained by the New Official Heretic Community

https://github.com/Vinay-Umrethe/heretic
162•jacquesm•7h ago•14 comments

Writing a primitive Forth in JavaScript (2024)

https://www.onirom.fr/wiki/blog/16-11-2024_writing_a_primitive_forth_in_javascript/
15•dverlaeckt80•12h ago•2 comments

Finding your ISP router's credentials

https://xetera.dev/article/finding-your-isp-routers-credentials
128•8organicbits•10h ago•48 comments

CellGemma: Predicting cell responses to help accelerate cancer research

https://blog.google/innovation-and-ai/models-and-research/google-deepmind/cellgemma/
249•carlesonielfa•17h ago•71 comments

Caveling-2-Mini (post-trained/fine-tuned Inkling-2-Mini)

https://caveman.so/news/introducing-caveling-2-mini
126•cobychapple•13h ago•18 comments

Micromorts – visualizing your chances of dying doing everyday activities

https://hugo0.com/micromorts
367•montenegrohugo•20h ago•189 comments

Claude 5-minute prompt cache vs GPT 30-minute cache: the cost of pauses

https://www.requesty.ai/blog/claude-5-minute-prompt-cache
405•danvdb•17h ago•175 comments

Show HN: Gully Cricket

https://varun.ca/gully-cricket/
94•winkerVSbecks•2d ago•28 comments

Can we stop with the vulnerability fear, uncertainty and doubt, please

https://shubs.io/can-we-stop-with-the-vulnerability-fear-uncertainty-and-doubt-please/
265•infosecau•9h ago•200 comments

Warren Buffett has died

https://www.cnbc.com/2026/12/01/warren-buffett-dies-at-age-96.html
1847•United857•11h ago•694 comments

TCP/IP over the air with LinHT

https://m17project.org/2026/11/27/tcp-ip-over-the-air-with-linht/
169•otto-mate•2d ago•25 comments

The cost of a story, and how it’s gone since we removed the paywall

https://www.sltrib.com/annual-report-2026/
119•danso•13h ago•43 comments

Amazon S3 now supports reading parts of multipart uploads in progress

https://aws.amazon.com/about-aws/whats-new/2026/11/amazon-s3-supports-reading-parts-multipart-upl...
210•dabinat•2d ago•45 comments

The Moat Is Sinking

https://sharamp.com/personal/moat/
103•BrokenMirror•12h ago•105 comments

Show HN: Learn Japanese kanji by understanding their parts

https://p.migdal.pl/kanly/
90•stared•2d ago•42 comments

So you just bought a $450M Leonardo da Vinci painting. Now what? (2017)

https://news.artnet.com/market/so-you-just-bought-a-450-million-da-vinci-now-what-1156268
86•NaOH•2d ago•53 comments