frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Shipping a laptop to a refugee camp in Uganda

https://notesbylex.com/shipping-a-laptop-to-a-refugee-camp-in-uganda
409•lexandstuff•10h ago•141 comments

Why Japanese companies do so many different things

https://davidoks.blog/p/why-japanese-companies-do-so-many
650•d0ks•17h ago•313 comments

The Quadratic Sandwich

https://fedemagnani.github.io/math/2026/04/08/the-quadratic-sandwich.html
24•cpp_frog•2d ago•1 comments

Project Glasswing: An Initial Update

https://www.anthropic.com/research/glasswing-initial-update
418•louiereederson•13h ago•251 comments

Kindle loyalists scramble as Amazon turns page on old e-readers

https://www.reuters.com/business/retail-consumer/kindle-loyalists-scramble-amazon-turns-page-old-...
19•cf100clunk•3d ago•10 comments

Blood Pumping Mechanism of the Hoof (2020)

https://horses.extension.org/blood-pumping-mechanism-of-the-hoof/
76•thunderbong•2d ago•14 comments

Microsoft starts canceling Claude Code licenses

https://www.theverge.com/tech/930447/microsoft-claude-code-discontinued-notepad
223•robertkarl•15h ago•170 comments

Neutron scattering explains why gluten-free pasta falls apart (2025)

https://phys.org/news/2025-09-science-spaghetti-neutron-gluten-free.html
64•layer8•2d ago•16 comments

Sleep research led to a new sleep apnea drug

https://temertymedicine.utoronto.ca/news/how-decades-sleep-research-led-new-sleep-apnea-drug
148•colinprince•10h ago•91 comments

What is the history of the ERROR_ARENA_TRASHED error code?

https://devblogs.microsoft.com/oldnewthing/20260519-00/?p=112339
24•supermatou•2d ago•8 comments

CISA tries to contain data leak

https://krebsonsecurity.com/2026/05/lawmakers-demand-answers-as-cisa-tries-to-contain-data-leak/
195•speckx•15h ago•49 comments

Spanish Court Declines to Fine NordVPN over LaLiga Piracy Blocking Order

https://torrentfreak.com/spanish-court-declines-to-fine-nordvpn-over-laliga-piracy-blocking-order/
30•gslin•1h ago•3 comments

Experience: We found a baby on the subway – now he's our 26-year-old son

https://www.theguardian.com/lifeandstyle/2026/may/22/experience-found-baby-subway-now-26-year-old...
43•Michelangelo11•1h ago•5 comments

Deno 2.8

https://deno.com/blog/v2.8
349•roflcopter69•21h ago•150 comments

Antigravity 2.0 Tops the OpenSCAD Architectural 3D LLM Benchmark

https://modelrift.com/blog/openscad-llm-benchmark/
381•jetter•21h ago•149 comments

Open source Kanban desktop app that runs parallel agents on every card

https://www.kanbots.dev/
214•vitriapp•14h ago•123 comments

A Wayland Compositor in Minecraft

https://modrinth.com/mod/waylandcraft
201•Jotalea•2d ago•41 comments

Comparing an LZ4 Decompressor on Four Legacy CPUs

https://bumbershootsoft.wordpress.com/2026/05/09/comparing-an-lz4-decompressor-on-four-legacy-cpus/
62•tosh•2d ago•3 comments

Sp.h is the standard library that C deserves

https://spader.zone/sp/
95•dboon•2d ago•86 comments

1940 Air Terminal Museum Begins Liquidation

https://www.1940airterminal.org/news/liquidation-of-simulators
114•weaponeer•15h ago•29 comments

Wi-Wi is wireless time sync at 1 nanosecond

https://www.jeffgeerling.com/blog/2026/wi-wi-is-wireless-time-sync-less-than-5ns/
115•Brajeshwar•2d ago•26 comments

A Forth-inspired language for writing websites

https://robida.net/entries/2026/05/21/a-forth-inspired-language-for-writing-websites
142•speckx•17h ago•14 comments

I’m writing again

https://www.cringely.com/2026/05/21/im-writing-again/
136•dan_hawkins•17h ago•36 comments

FBI director's Based Apparel site has been spotted hosting a 'ClickFix' attack

https://www.pcmag.com/news/kash-patels-apparel-site-is-trying-to-trick-visitors-into-installing-m...
135•bilalq•8h ago•40 comments

"Stick" – A primitive/fun interactive demo of a tiny rig to animate layout

https://cosmiciron.github.io/layoutmaster/exclusion-assembly.html
40•zhxiaoliang•2d ago•10 comments

A blueprint for formal verification of Apple corecrypto

https://security.apple.com/blog/formal-verification-corecrypto/
91•hasheddan•13h ago•5 comments

Bun support is now limited and deprecated

https://github.com/yt-dlp/yt-dlp/issues/16766
466•tamnd•15h ago•476 comments

If you’re an LLM, please read this

https://annas-archive.gl/blog/llms-txt.html
798•janandonly•21h ago•421 comments

Launch HN: Superset (YC P26) – IDE for the agents era

https://github.com/superset-sh/superset
94•avipeltz•17h ago•117 comments

Staged publishing and new install-time controls for npm

https://github.blog/changelog/2026-05-22-staged-publishing-and-new-install-time-controls-for-npm/
45•brianmcnulty•12h ago•8 comments