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.

Git for the US Code

https://lexifina.com/blog/git-for-the-us-code
249•alansaber•7h ago•121 comments

Genie 4

https://deepmind.google/blog/genie-4-a-new-generation-of-world-simulation/
880•acossta•17h ago•506 comments

Ghostty 1.4

https://ghostty.org/docs/install/release-notes/1-4-0
998•deaux•16h ago•317 comments

Ink droplets in motion snag top prize in Nikon Small World contest

https://arstechnica.com/science/2026/10/meet-the-2026-winner-of-nikons-small-world-photomicrograp...
222•_Microft•2d ago•36 comments

Survival LLM: AI assistant and emergency library that runs on your own computer

https://www.brangus.io/survival-llm
337•Luc•13h ago•139 comments

MacOS Extensions Pack for the Apple Network Server v1.0

http://oldvcr.blogspot.com/2026/10/macos-extensions-pack-for-apple-network.html
34•classichasclass•3d ago•0 comments

Tailscale SSH: Investigation and remediation update

https://tailscale.com/ssh-investigation-update
279•figmert•13h ago•144 comments

Early progress on accelerating drug development with Claude

https://www.anthropic.com/research/early-progress-drug-development
221•dataking•12h ago•207 comments

Closing the Sea: Lessons from Ancient Mediterranean Blockades

https://maritime-executive.com/editorials/closing-the-sea-lessons-from-ancient-mediterranean-bloc...
7•gumby•2d ago•0 comments

Raspberry Pi Zero 3W and Pi 4 Model A spotted in device trees

https://bret.dk/raspberry-pi-zero-3-w-pi-4-model-a/
164•sthlmb•12h ago•84 comments

SS United States to be sunk as largest artificial reef

https://divemagazine.com/scuba-diving-news/ss-united-states-to-be-sunk-as-worlds-largest-artifici...
142•geox•2d ago•107 comments

Python's experimental JIT to be removed

https://lwn.net/SubscriberLink/1102102/0c00e4d550f25f8c/
498•f311a•21h ago•275 comments

New 14‑inch MacBook Pro with M6

https://www.apple.com/in/newsroom/2026/10/apple-introduces-the-new-14-inch-macbook-pro-with-m6/
359•beex•17h ago•475 comments

Code-execution flaw in UEFI firmware threatens most servers on planet Earth

https://arstechnica.com/security/2026/10/code-execution-flaw-in-uefi-firmware-threatens-most-serv...
134•DamnInteresting•12h ago•65 comments

What Happened to the Neanderthals?

https://www.nytimes.com/2026/10/26/science/what-happened-to-the-neanderthals.html
34•Hooke•1d ago•34 comments

A Love Letter to the Super Dimension Fortress

https://brennan.day/a-love-letter-to-the-super-dimension-fortress/
53•jruohonen•4d ago•9 comments

Decompiling Fast Food Tycoon 2: Drawing the World

https://healeycodes.com/decompiling-fast-food-tycoon-2-drawing-the-world
25•healeycodes•3d ago•5 comments

New iMac with M6

https://www.apple.com/imac/
333•notfried•20h ago•423 comments

Jokes for the ZX81

https://spelk.press/gb/products/81-jokes-for-the-zx81
85•robin_reala•1d ago•7 comments

Classification of Finite Simple Groups Formalized in Lean

https://cfsg.axiommath.ai/
207•pfdietz•16h ago•58 comments

Four Months of Zig

https://gracefulliberty.com/articles/four-months-of-zig/
122•ibobev•13h ago•76 comments

An examination of lost firefly species of the US and Canada

https://journaloflostspecies.org/index.php/jls/article/view/37/33
120•bryanrasmussen•3d ago•38 comments

Hunger beats tenure: Advice for junior engineers

https://felixrieseberg.com/hunger-beats-tenure-advice-for-junior-engineers/
62•akavi•10h ago•27 comments

Assembly Nights (2021)

https://ratfactor.com/assembly-nights
50•andsoitis•2d ago•10 comments

US Government is hiding a secret court ruling about how it spies on Americans

https://www.wired.com/story/the-us-government-is-hiding-a-secret-court-ruling-about-how-it-spies-...
924•helterskelter2•19h ago•340 comments

Moxnox 1.0: NT-Like, POSIX-Compatible OS Running Atop Btrfs

https://github.com/maharmstone/moxnox
67•SockThief•9h ago•10 comments

State of ARM Support for Puget Bench

https://www.pugetsystems.com/blog/2026/10/22/state-of-arm-support-for-puget-bench/
6•zdw•5d ago•0 comments

Why Micron Is Suddenly Ditching 4F² DRAM as Rivals Double Down

https://hothardware.com/news/why-micron-is-suddenly-ditching-4f-dram
166•rbanffy•1d ago•26 comments

Radio-tagged Monarch butterflies

https://news.vt.edu/articles/2026/10/cmi-monarchs.html
56•rdmuser•15h ago•18 comments

Artificial Analysis Intelligence Index v5

https://artificialanalysis.ai/articles/artificial-analysis-intelligence-index-v5
43•hn-aarvin•6h ago•52 comments