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.

AI-generated posters don’t have to be horrible

https://john.hartnup.uk/2026/06/07/ai-event-posters.html
61•ereiamjh•1h ago•31 comments

Human brain is two separate organs, Stanford Medicine-led research finds

https://med.stanford.edu/news/all-news/2026/09/two-separate-brains.html
239•emigre•4h ago•103 comments

If math is more than proof, we need to better celebrate the rest of it

https://terrytao.wordpress.com/2026/09/18/if-math-is-more-than-proof-we-need-to-better-celebrate-...
102•num42•4h ago•71 comments

GPT-6 Astra Solves a WWI German Radio Cipher

https://www.prinzai.com/p/gpt-6-astra-solves-a-wwi-german-radio
120•nsoonhui•3h ago•60 comments

Android 17 is the first since 3.x to add new APIs without releasing to the AOSP

https://grapheneos.social/@GrapheneOS/117282080803799576
849•theanonymousone•15h ago•417 comments

San Francisco Onion Futures Company

https://onionfutures.com/
195•z-mach9•6h ago•69 comments

"The Secret Life of Circuits" is here

https://blog.coredump.cx/p/the-secret-life-of-circuits-is-here
28•surprisetalk•3d ago•14 comments

Apple M6 Pro Achieves the Highest Single-Core CPU Score in Geekbench 7

https://browser.geekbench.com/v7/cpu/389219
56•gainsurier•4h ago•41 comments

Cloudflare Quick Tunnels

https://try.cloudflare.com/
710•jcbhmr•20h ago•285 comments

SDCC – Small Device C Compiler

https://sdcc.sourceforge.net/
84•lioeters•7h ago•18 comments

Science Is Open Software

https://jepedersen.dk/blog/202505_research/
100•jegp•8h ago•38 comments

How to Write with an LLM

https://sockpuppet.org/blog/2026/09/17/how-to-write-with-an-llm/
495•joeriddles•1d ago•334 comments

Saving another 100TB of RAM

https://blog.cloudflare.com/saving-100-tb-of-ram-with-math/
364•f311a•15h ago•79 comments

You can run Git on object storage if you re-make packfiles

https://www.tigrisdata.com/blog/objgit-packfiles/
48•evacchi•2d ago•15 comments

How OpenAI Used Its Own LLMs to Design Its Jalapeño Chip

https://spectrum.ieee.org/llms-for-chip-design
125•maxall4•11h ago•88 comments

Why building a Rust LSP is hard

https://rust-glancer.github.io/blog/why-lsp-is-hard/
84•agluszak•2d ago•32 comments

Typesafe-computer-use drives a Mac toward a goal for 1/50th of a cent per step

https://github.com/awlevin/typesafe-computer-use
81•rahimnathwani•2d ago•52 comments

Ctenophores: Wonders of Biology

https://www.quantamagazine.org/ctenophores-arent-just-beautiful-theyre-biological-wonders-20260916/
26•randomImmigrant•2d ago•6 comments

Communication by means of modulated Johnson noise

https://www.pnas.org/doi/10.1073/pnas.2201337119
3•austinallegro•2d ago•0 comments

NASA-IBM Lunar Foundation open-Source Geospatial AI Model

https://newsroom.usra.edu/usra-contributes-planetary-science-expertise-to-nasa-ibm-lunar-foundati...
28•noobplus•5h ago•2 comments

The first new cat species discovered in 100 years

https://www.nationalgeographic.com/animals/article/meet-the-first-new-cat-species-discovered-in-1...
275•ohjeez•1d ago•105 comments

OpenJev

https://openjev.com/
630•ilreb•1d ago•267 comments

Goroutine Leak Profiles

https://go.dev/blog/goroutine-leak-profiles
34•torutofu•2d ago•4 comments

Show HN: Cactus Needle 3: 8-29MB automation models can match DeepSeek V4 Flash

https://cactuscompute.com/needle
198•HenryNdubuaku•1d ago•89 comments

Photon-Emission-Guided Laser Fault Injection Enables RP2350 Secure Debug

https://donjon.ledger.com/blog/rp2350-secure-debug-laser-fault-injection/
194•synack•17h ago•70 comments

Cache-to-Cache: Direct Semantic Communication Between LLMs (2025)

https://arxiv.org/abs/2510.03215
92•rochansinha•15h ago•13 comments

Warez: The Infrastructure and Aesthetics of Piracy (2021)

https://archive.org/details/b904a8eb-9c98-4bb1-bf25-3cb9d075b157
168•succinct_ideas•1d ago•71 comments

Veronese's Dogs

https://publicdomainreview.org/essay/veroneses-dogs
7•prismatic•2d ago•1 comments

Suppress vulnerabilities applying Kubernetes context to scans

https://github.com/alegrey91/vex8s
6•alegrey91•1d ago•2 comments

Inside ZCode: Silently uploading your Git history to the cloud

https://blog.ferstar.org/en/posts/zcode-silent-workspace-snapshot-upload/
301•csmantle•1d ago•100 comments