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.

Watch Los Angeles get built, one building at a time (1880–2026)

https://lax-skyline.parcelscope.net/
149•rustywasm•3h ago•59 comments

Trusting-Trust Attack against an Entire Linux Distribution

https://arxiv.org/abs/2607.24888
91•signa11•2d ago•20 comments

WeatherNext 3

https://deepmind.google/science/weathernext/
163•matthieu_bl•4d ago•23 comments

Finding a bug in Dummit and Foote's Abstract Algebra

https://kallus.org/blog/dummit_and_foote.html
49•evakhoury•3d ago•22 comments

Scientists observe Einstein's gravity in the quantum world

https://www.ox.ac.uk/news/2026-08-28-scientists-observe-einsteins-gravity-in-the-quantum-world
76•mudil•3d ago•20 comments

Show HN: Interactive Tree of Life

https://ptree.org/
25•Lucent•3d ago•11 comments

The smallest edge AI device for local LLMs

https://tiiny.ai/
15•alex-moon•1h ago•5 comments

Macbeth and His Problems

https://porticoquarterly.com/essay/macbeth-and-his-problems/
22•apophatic•1h ago•6 comments

Methods for Random Gradients (2024)

https://justinjay.wang/methods-for-random-gradients/
39•nickswalker•4d ago•4 comments

216M Spy TVs – The LG Smart TV Problem [video]

https://www.youtube.com/watch?v=6IFVTcM28KA
381•treve•22h ago•661 comments

Decapitating a MacBook (2025)

https://mm-dev.rocks/series/decapitating-macbook-an-odyssey/
43•luu•1d ago•30 comments

Decoding the NEC V20 Microcode

https://martypc.blogspot.com/2026/09/decoding-nec-v20-microcode.html
98•mariuz•3d ago•7 comments

Caltech Mathathon – first hackathon ever devoted to research level mathematics

https://mathathonchallenge.com/index.html
209•astroanax•13h ago•68 comments

Simple Is Not Small

https://jyn.dev/simple-is-not-the-same-as-small/
165•zdw•3d ago•55 comments

Icy Moons Are Ocean Worlds

https://mceglowski.substack.com/p/icy-moons-are-ocean-worlds
105•worldvoyageur•1d ago•10 comments

This Month in Ladybird – August 2026

https://ladybird.org/newsletter/2026-08-31/
120•exploraz•2d ago•13 comments

The Dataflow Model Revisited

https://www.vldb.org/pvldb/volumes/19/paper/The%20Dataflow%20Model%20Revisited
75•scott_s•1d ago•13 comments

My practical approach to surfing the web safely

https://molily.de/safe-websurfing/
50•ethanhawksley•5d ago•18 comments

bzip3

https://github.com/iczelia/bzip3
354•tosh•8h ago•99 comments

Show HN: I built an aesthetically pleasing puzzle

https://jigsawhaiku.com/
4•windowshopping•3d ago•0 comments

Replaceable but Employed: Automation and the Meaning of Work

https://www.nber.org/papers/w35559
43•mooreds•3h ago•12 comments

The Education of a Doomer

https://borretti.me/article/the-education-of-a-doomer
25•f311a•1h ago•9 comments

Keep Our Servers Running

https://blog.archive.org/2026/09/01/keep-our-servers-running-your-recurring-donation-goes-3x-this...
923•sonicrocketman•18h ago•225 comments

Working on Economics with Fable 5

https://wilsoniumite.com/2026/08/03/working-on-economics-with-fable-5/
5•Wilsoniumite•1h ago•2 comments

A Tesla ran a stop sign and killed a man, Full Self-Driving/Autopilot was on

https://electrek.co/2026/09/07/tesla-driver-assist-stop-sign-buena-vista/
89•FabHK•2h ago•45 comments

How to bring up the Linux Kernel on a new platform

https://werwolv.net/posts/linux_bringup/
59•WerWolv•4d ago•4 comments

Whistle Synth Mac App

https://www.jefftk.com/p/whistle-synth-mac-app
55•luu•3d ago•13 comments

She Also Found It at the Movies

https://hedgehogreview.com/web-features/thr/posts/she-also-found-it-at-the-movies
14•prismatic•3d ago•0 comments

Schemy Lisp En DOS

https://sled.neocities.org/
25•AlexeyBrin•1d ago•3 comments

Live map of public transport in Belgium

https://openbaarvervoerbelgie.be/
175•coinfused•13h ago•73 comments