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•11mo ago

Comments

LegionMammal978•11mo 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•11mo 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•11mo 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.

Flipdiscs

https://flipdisc.io
196•skogstokig•3d ago•35 comments

I bought Friendster for $30k – Here's what I'm doing with it

https://ca98am79.medium.com/i-bought-friendster-for-30k-heres-what-i-m-doing-with-it-d5e8ddb3991d
732•ca98am79•11h ago•384 comments

TurboQuant: A first-principles walkthrough

https://arkaung.github.io/interactive-turboquant/
121•kweezar•6h ago•15 comments

Self-updating screenshots

https://interblah.net/self-updating-screenshots
258•bjhess•1d ago•38 comments

AI should elevate your thinking, not replace it

https://www.koshyjohn.com/blog/ai-should-elevate-your-thinking-not-replace-it/
449•koshyjohn•12h ago•330 comments

The Prompt API

https://developer.chrome.com/docs/ai/prompt-api
88•gslin•5h ago•61 comments

Three constraints before I build anything

https://jordanlord.co.uk/blog/3-constraints/
184•nervous_north•1d ago•28 comments

When the cheap one is the cool one

https://arun.is/blog/cheap-cool/
102•ddrmaxgt37•1d ago•44 comments

Fast16: High-precision software sabotage 5 years before Stuxnet

https://www.sentinelone.com/labs/fast16-mystery-shadowbrokers-reference-reveals-high-precision-so...
229•dd23•11h ago•51 comments

A Guide to CubeSat Mission and Bus Design

https://pressbooks-dev.oer.hawaii.edu/epet302/
20•o4c•1d ago•2 comments

Box to save memory in Rust

https://dystroy.org/blog/box-to-save-memory/
115•emschwartz•3d ago•27 comments

Mystery Cpuid Bit

http://www.os2museum.com/wp/mystery-cpuid-bit/
9•userbinator•2d ago•1 comments

FreeBSD Device Drivers Book

https://github.com/ebrandi/FDD-book
78•myth_drannon•9h ago•13 comments

Sawe becomes first athlete to run a sub-two-hour marathon in a competitive race

https://www.bbc.com/sport/athletics/articles/crm1m7e0zwzo
367•berkeleyjunk•11h ago•251 comments

SWE-bench Verified no longer measures frontier coding capabilities

https://openai.com/index/why-we-no-longer-evaluate-swe-bench-verified/
292•kmdupree•18h ago•161 comments

The Mushroom That Makes People Have the Exact Same Hallucination

https://www.vice.com/en/article/meet-the-mushroom-that-make-people-have-the-exact-same-hallucinat...
13•thunderbong•44m ago•3 comments

The Military Rockets That Launched the Space Age (2023)

https://airandspace.si.edu/stories/editorial/military-rockets-launched-space-age
7•radeeyate•1d ago•0 comments

Revocation of X.509 Certificates

https://blog.apnic.net/2026/04/24/revocation-of-x-509-certificates/
33•jandeboevrie•1d ago•4 comments

Butterflies are in decline across North America, a look at the Western Monarch

https://www.smithsonianmag.com/science-nature/butterflies-are-in-dramatic-decline-across-north-am...
198•1659447091•10h ago•59 comments

Quirks of Human Anatomy

https://www.sdbonline.org/sites/fly/lewheldquirk/figlegq6.htm
129•gurjeet•2d ago•70 comments

Running Bare-Metal Rust Alongside ESP-IDF on the ESP32-S3's Second Core

https://tingouw.com/blog/embedded/esp32/run_rust_on_app_core
65•MrBuddyCasino•3d ago•10 comments

EvanFlow – A TDD driven feedback loop for Claude Code

https://github.com/evanklem/evanflow
59•evanklem2004•6h ago•25 comments

Chernobyl wildlife forty years on

https://www.bbc.com/future/article/20260424-chernobyl-wildlife-forty-years-on
95•reconnecting•12h ago•19 comments

Magic: The Gathering took me from N2 to Japanese fluency

https://www.tokyodev.com/articles/how-magic-the-gathering-took-me-from-n2-to-japanese-fluency
125•pwim•3d ago•51 comments

An AI agent deleted our production database. The agent's confession is below

https://twitter.com/lifeof_jer/status/2048103471019434248
638•jeremyccrane•15h ago•794 comments

Clay PCB Tutorial

https://feministhackerspaces.cargo.site/Clay-PCB-Tutorial
219•j0r0b0•16h ago•129 comments

MoQ Boy

https://moq.dev/blog/moq-boy/
58•mmcclure•11h ago•7 comments

Show HN: Free textbook on engineering thermodynamics

https://thermodynamicsbook.com/
136•2DcAf•16h ago•34 comments

The Visible Zorker: Zork 1

https://eblong.com/infocom/visi/zork1/
126•PLenz•15h ago•25 comments

Lessons from building multiplayer browsers

https://www.alejandro.pe/writing/sail-muddy-lessons
27•alejandrohacks•17h ago•11 comments