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.

Omarchy: Any User Process Can Escalate to Root

https://0xcc.io/posts/omarchy-root-creds/
106•trap0xcc•1h ago•58 comments

Creepy Crawlies

https://people.kernel.org/monsieuricon/creepy-crawlies
302•zdw•23h ago•137 comments

Haiku R1/beta6 has been released

https://www.haiku-os.org/news/2026-08-26_haiku_r1_beta6
59•metrofun•1h ago•9 comments

Hacking IKEA Furniture

https://greenlightning.eu/diy/hacking-ikea-furniture/
161•greenlightning•5h ago•82 comments

Coordination Headwind: How Organizations Are Like Slime Molds

https://komoroske.com/slime-mold/
14•rzk•1h ago•4 comments

Artie (YC S23) Is Hiring Technical AES

https://www.artie.com/careers?ashby_jid=e87b84d2-78b3-41a3-937a-47e83643cdf1
1•j-cheong•7m ago

Electric rain can eat through metal

https://www.scientificamerican.com/article/electric-rain-can-eat-through-metal/
16•sohkamyung•3d ago•4 comments

European Commission Revives Push for Encryption Backdoors in ProtectEU Strategy

https://reclaimthenet.org/eu-protecteu-strategy-encryption-backdoor-law-enforcement
106•nickslaughter02•1h ago•22 comments

Casey Muratori – The Root of the Root of All Evil – BSC 2026 [video]

https://www.youtube.com/watch?v=hpj6r6CjJf8
193•surprisetalk•3d ago•92 comments

Arbitrary code execution in QubesOS via copy-to-VM error reporting backchannel

https://www.qubes-os.org/news/2026/08/29/qsb-118/
152•vntok•8h ago•63 comments

Longest Straight Line Paths on Water or Land on the Earth (2018)

https://arxiv.org/abs/1804.07389
164•joebig•8h ago•48 comments

Europe's summer drought is so extreme that desertification is a growing threat

https://fortune.com/2026/08/29/europe-summer-drought-desertification-threat-rivers-fish/
92•Brajeshwar•2h ago•80 comments

Zig: Pointer Stability for ArrayLists

https://ziglang.org/devlog/2026/#2026-08-27
15•tosh•2h ago•0 comments

Dad's Custom Atari Peripherals – By Jim Trageser

https://www.goto10retro.com/p/dads-custom-atari-peripherals
29•rbanffy•2d ago•6 comments

When fruit is scarce, these monkeys hunt animals

https://www.smithsonianmag.com/smart-news/when-fruit-is-scarce-these-monkeys-hunt-animals-the-beh...
31•cisc•1d ago•6 comments

METR and Redwood Offer Holy %^ Postmortem of the HuggingFace Hack

https://thezvi.wordpress.com/2026/08/29/metr-and-redwood-offer-holy-postmortem-of-the-huggingface...
10•catbird•3h ago•2 comments

An implementation of Conway's Game of Life for Windows 3.1x and later

https://www.muppetlabs.com/~breadbox/software/windows.html
36•Bluestein•5h ago•5 comments

What my dad taught me about AI coding in the 90s

https://askmike.org/articles/ai-coding-lessons-in-the-90s-from-my-dad/
80•askmike•6d ago•41 comments

RISC-V is now officially supported by CPython

https://blog.python.org/2026/08/riscv-now-officially-supported/
264•lumpa•5d ago•45 comments

Bug Blindness

https://danluu.com/bug-blind/
342•davidmckenna•16h ago•219 comments

Claude Session URL appended to commit messages and PR descriptions by default

https://github.com/anthropics/claude-code/issues/66504
111•sparsesignal•4h ago•137 comments

Automating Immersive Reading

https://smoores.dev/post/automating_immersive_reading/
49•smoores•5h ago•17 comments

Building my own network stack

https://blog.lyc8503.net/en/post/dn42-2-dnet/
67•uneven9434•7h ago•61 comments

monty-go: Pure-Go wrapper for Pydantic's Monty Python Interpreter

https://github.com/fugue-labs/monty-go
32•networked•5h ago•5 comments

Hy4 preview

https://www.tencent.com/tencent-releases-and-open-sources-tencent-hy4-preview/
362•shenli3514•21h ago•225 comments

JupyterGIS 0.16: a grammar of graphics for maps, and collaborative story maps

https://blog.jupyter.org/jupytergis-0-16-new-visualization-capabilities-collaborative-story-maps-...
82•arjxn-py•5d ago•9 comments

Orbs

https://ampcode.com/notes/orbs-explained
72•tosh•5d ago•28 comments

One Nix flake to rule them all

https://fzakaria.com/2026/08/28/one-flake-to-rule-them-all
59•ingve•5h ago•5 comments

The Einstein-Szilard Refrigerator (2020)

https://invention.si.edu/invention-stories/einstein-szilard-refrigerator
90•EndXA•3d ago•39 comments

California lawmakers unanimously pass Linux exemption from age-verification law

https://www.tomshardware.com/software/linux/california-lawmakers-unanimously-pass-linux-exemption...
586•shscs911•13h ago•275 comments