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.

Magnitude 7.7 Earthquake – 68 km NNW of Ende, Indonesia

https://earthquake.usgs.gov/earthquakes/eventpage/us6000tkt2/executive
44•Bender•1h ago•3 comments

Qwen 3.8 27B

https://huggingface.co/Qwen/Qwen3.8-27B-FP8
916•erdaltoprak•11h ago•598 comments

Going Dark, and the era of law enforcement hacking

https://blog.cryptographyengineering.com/2026/08/14/everything-is-about-to-go-dark/
215•vslira•5h ago•124 comments

The Ploopy A+ Trackball Is Here

https://blog.ploopy.co/the-aplus-is-finally-here-499
23•big_toast•1h ago•10 comments

Why does Opus 5 feel worse to work with?

https://mun-logadan.github.io/why-does-opus-5-feel-worse/
796•numeri•16h ago•731 comments

Simplifying and Refactoring Introductory Calculus

https://arxiv.org/abs/1811.03459
20•E-Reverance•1h ago•0 comments

The case for overhauling American science

https://www.economist.com/by-invitation/2026/08/13/the-case-for-overhauling-american-science
44•andsoitis•3h ago•33 comments

Google is making private AI practical with homomorphic encryption

https://blog.google/security/how-google-is-making-private-ai-practical-with-homomorphic-encryption/
287•u1hcw9nx•10h ago•171 comments

RustDesk now supports true unattended remote access on Wayland

https://rustdesk.com/blog/unattended-remote-access-wayland/
227•rustdesk•10h ago•96 comments

Hi-Fi Tape Recorder Changed Radio Forever

https://spectrum.ieee.org/magnetophon-laugh-track
9•Jimmc414•3d ago•0 comments

Super Mario Derivations

https://fzakaria.com/2026/08/05/super-mario-derivations
58•domenkozar•1w ago•10 comments

eigendrum

https://eigendrum.com/#p=circle
36•bookofjoe•3h ago•8 comments

Corgi kills short-lived website that ranked its female employees

https://sf.gazetteer.co/corgi-kills-short-lived-website-that-ranked-its-female-employees
19•randycupertino•2h ago•6 comments

AI by Hand

https://www.byhand.ai/
211•sans_souse•10h ago•18 comments

Firefox is now the last major browser that still supports uBlock Origin

https://www.pcworld.com/article/3212428/firefox-is-now-the-last-major-browser-that-still-supports...
436•DemiGuru•7h ago•169 comments

Introducing Toast 1

https://www.mixedbread.com/blog/toast-1
181•mplappert•11h ago•58 comments

RISC-V: They should have known better

https://dmitry.gr/?r=06.%20Thoughts&proj=12.%20RV
128•kaycebasques•3h ago•99 comments

Stop sending me huge PRs; a rant

https://getsmall.xyz/post/cmstjfl9l000if70ljmpzr4va
79•trezm•3h ago•47 comments

I turned my RSS feeds into an e-ink newspaper to stop reading on my phone

https://heyjonny.dev/posts/rss-to-eink-newspaper/
147•speckx•11h ago•61 comments

Ultraviolet Bird Photography

https://uvbirds.com/
111•EndXA•1w ago•22 comments

Maximizing the value of your Claude Code sessions

https://claude.com/blog/maximizing-the-value-of-your-claude-code-sessions
135•twapi•9h ago•92 comments

Turbo Pascal on CP/M, MSX-DOS and MS-DOS

http://pascal.hansotten.com/delphi/turbo-pascal-on-cpm-msx-dos-and-ms-dos/
73•rbanffy•2d ago•24 comments

NSA and IETF, Part 9

https://blog.cr.yp.to/20260814-update.html
7•libroot•1h ago•0 comments

Show HN: Mole – Deep research agent for your terminal

https://github.com/lajosdeme/mole
50•lajosdeme•7h ago•8 comments

GLM-5.3: Frontier coding with emergent cyber capabilities

https://z.ai/blog/glm-5.3
1034•pella•20h ago•515 comments

New Lower and Upper Bounds for the Grothendieck Constant

https://arxiv.org/abs/2608.11158
37•surprisetalk•6h ago•7 comments

Seven books I keep close because I love them

https://blog.plover.com/2026/08/02/
299•surprisetalk•11h ago•133 comments

Don't classify, hallucinate

https://softwaredoug.com/blog/2026/08/10/hypothetical-classifications
219•softwaredoug•4d ago•90 comments

Show HN: Ember – Redshift safe color palettes

https://github.com/carpdiem/ember
63•carpdiem•5d ago•14 comments

Show HN: LuaCAD – Parametric CAD Scripted in Lua

https://luacad.ad-si.com
73•adius•9h ago•14 comments