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.

mitmproxy adds --emudhra option

https://github.com/mitmproxy/mitmproxy/commit/169234499088fc2c414644a67efaed3d64ae2e3b
194•ahlCVA•2h ago•50 comments

Railway Security Disclosure

https://blog.railway.com/p/security-disclosure-wednesday-october-21st
317•passenger•4h ago•156 comments

There isn't an easy exploit for this PHP vulnerability

https://eliastaylor.com/noexploit/
160•zkldi•2h ago•72 comments

Today's PHP security release, and how we're strengthening security

https://thephp.foundation/blog/2026/10/21/todays-php-security-release-and-how-were-strengthening-...
682•ejohnso49•10h ago•336 comments

GitLab Critical Patch Release

https://docs.gitlab.com/releases/patches/patch-release-gitlab-19-5-1-released/
254•artooro•7h ago•63 comments

Google's Rust Style Guide

https://google.github.io/styleguide/rustguide.html
211•kaycebasques•6h ago•119 comments

Compromised LiteLLM 1.107.0 on PyPI Installs a Credential Stealer and Backdoor

https://safedep.io/litellm-pypi-compromise-credential-stealer-backdoor/
440•abhisek•12h ago•146 comments

No More Icons in Every Menu by Default

https://blog.jim-nielsen.com/2026/icons-in-every-menu/
182•riffraff•1d ago•128 comments

Meta ordered to pay $25B penalty in New Mexico Cambridge Analytica case

https://www.cnbc.com/2026/10/21/meta-new-mexico-cambridge-analytica-penalty.html
386•cbg0•8h ago•237 comments

Tiny possum and glider thought extinct for 6000 years found in remote West Papua

https://www.bbc.com/news/articles/cp814l80pw2o
159•bryanrasmussen•1d ago•25 comments

The era of speedball capitalism has dawned

https://www.economist.com/business/2026/09/30/the-era-of-speedball-capitalism-has-dawned
19•mikhael•1h ago•3 comments

Chain of Thought for Humans

https://www.boristhebrave.com/2026/10/18/chain-of-thought-for-humans/
28•ibobev•2d ago•11 comments

Beyond the &

https://lwn.net/Articles/1096028/
9•PaulHoule•1d ago•0 comments

OpenAI models are coming to Google Cloud

https://openai.com/index/openai-models-google-cloud/
242•armcat•13h ago•104 comments

Patch the Planet: global response coordination as agents uncover vulnerabilities

https://openai.com/index/coordinated-disclosure-of-critical-vulnerabilities/
236•nr378•12h ago•162 comments

DuckDB 2.0.0 LTS “Cinnamon Teal”

https://duckdb.org/2026/10/21/announcing-duckdb-200
366•nnx•12h ago•45 comments

Why Windows 11 still ships with Windows 95's memory manager hidden inside it

https://www.windowslatest.com/2026/10/19/why-windows-11-still-ships-with-windows-95s-memory-manag...
138•khalidkhair•2d ago•95 comments

The Rise and Fall of the Plasma Screen

https://www.construction-physics.com/p/the-rise-and-fall-of-the-plasma-screen
36•surprisetalk•2d ago•22 comments

Airbnb bans making guests do dishes and take out the trash

https://www.theverge.com/tech/1015844/airbnb-chores-house-rules-update
107•babelfish•2h ago•83 comments

EA is shutting down BioWare

https://www.theverge.com/games/1015647/ea-is-shutting-down-bioware
534•aaronbrethorst•8h ago•321 comments

When Do We Become the Person We Ought to Be?

https://www.graphsaboutreligion.com/p/when-do-we-become-the-person-we-ought
16•toomuchtodo•2h ago•11 comments

Oregon lawmakers approve money to buy an iconic waterfall listed on Redfin

https://apnews.com/article/oregon-waterfall-for-sale-lawmakers-996de63462d49ec3ad9bee1629fb9488
172•surprisetalk•10h ago•83 comments

Launch HN: Alter (YC S25) – Access control for multi-user agents

https://alterauth.com/
52•simfinii•19h ago•22 comments

PHP 8.2.35

https://github.com/php/php-src/releases/tag/php-8.2.35
973•tuyuri•12h ago•535 comments

Κ = 0.003 for integer multiplication crossed

https://github.com/CrocSwap/integer-mult-bounds/pull/4441
26•aleph_minus_one•2h ago•15 comments

Claude Duet

https://claude.com/product/claude-duet
318•kuberwastaken•6h ago•278 comments

Code is clay now

https://www.albertodenia.com/writing/code-is-clay-now
94•albertodenia•2d ago•53 comments

New plastic pollution discovered: 'Plasticorals' coral rubble fused with plastic

https://www.oist.jp/news-center/news/2026/9/18/new-form-plastic-pollution-discovered-shores-vulne...
101•giuliomagnifico•9h ago•42 comments

Using thousands of AI agents to uncover new leads in biological data

https://openai.com/index/scaling-ai-research-for-complex-biological-data/
135•raahelb•12h ago•85 comments

PHP web runtimes (CVE-2026-109618 and CVE-2026-109619)

https://aws.amazon.com/security/security-bulletins/2026-143-aws/
110•hayd•4h ago•26 comments