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.

It's time to talk about my writerdeck

https://veronicaexplains.net/my-first-writerdeck/
171•hggh•3h ago•98 comments

.NET (OK, C#) gets union types

https://andrewlock.net/exploring-the-dotnet-11-preview-2-dotnet-gets-union-types/
73•ingve•1d ago•54 comments

My two-part desk setup (2025)

https://arslan.io/2025/11/18/my-two-part-desk-setup/
155•James72689•3d ago•101 comments

On The <dl> (2021)

https://benmyers.dev/blog/on-the-dl/
319•ravenical•9h ago•102 comments

Texas woman arrested for Facebook post about town water quality

https://reclaimthenet.org/texas-woman-arrested-for-facebook-post-about-town-water-quality
500•abawany•4h ago•227 comments

Markdown Is Not LaTeX

https://www.iconoclasts.blog/joe/markdown-is-not-latex
6•joesuf4•11m ago•0 comments

Green card seekers must leave U.S. to apply, Trump administration says

https://www.nytimes.com/2026/05/22/us/politics/green-card-changes-trump.html
331•tlhunter•1d ago•673 comments

Hengefinder: Finding When the Sun Aligns with Your Street

https://victoriaritvo.com/blog/hengefinder/
85•evakhoury•1d ago•20 comments

Reverse engineering circuitry in a Spacelab computer from 1980

https://www.righto.com/2026/05/reverse-engineering-spacelab-computer.html
74•elpocko•5h ago•6 comments

z386: An Open-Source 80386 Built Around Original Microcode

https://nand2mario.github.io/posts/2026/z386/
107•wicket•7h ago•21 comments

80386 Microcode Disassembled

https://www.reenigne.org/blog/80386-microcode-disassembled/
201•nand2mario•10h ago•39 comments

SpaceX launches Starship v3 rocket

https://www.space.com/space-exploration/launches-spacecraft/spacex-starship-v3-megarocket-first-t...
307•busymom0•22h ago•215 comments

PHP's Oddities

https://flowtwo.io/post/php%27s-oddities
83•thejoeflow•4d ago•86 comments

New map reveals lost roads of the Roman Empire

https://www.scientificamerican.com/article/new-high-resolution-map-transforms-what-we-know-about-...
6•sohkamyung•3d ago•0 comments

The Art of Money Getting

https://kk.org/cooltools/book-freak-210-the-art-of-money-getting/
156•dxs•9h ago•109 comments

Making Deep Learning Go Brrrr from First Principles (2022)

https://horace.io/brrr_intro.html
137•tosh•10h ago•54 comments

Italy moves to Airbus A330 tankers

https://www.euronews.com/my-europe/2026/05/21/italy-moves-to-airbus-a330-tankers-in-major-nato-al...
213•embedding-shape•6h ago•67 comments

-​-dangerously-skip-reading-code

https://olano.dev/blog/dangerously-skip/
72•fagnerbrack•12h ago•88 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-...
90•cf100clunk•4d ago•94 comments

sp.h: Fixing C by giving it a high quality, ultra portable standard library

https://spader.zone/sp/
166•dboon•3d ago•154 comments

Rubish: A Unix shell written in pure Ruby

https://github.com/amatsuda/rubish
161•winebarrel•15h ago•95 comments

Oura says it gets government demands for user data

https://this.weekinsecurity.com/oura-says-it-gets-government-demands-for-user-data-will-it-share-...
239•donohoe•8h ago•139 comments

Highest Random Weight in Elixir

https://jola.dev/posts/highest-random-weight-in-elixir
55•shintoist•2d ago•2 comments

Lisp in Vim (2019)

https://susam.net/lisp-in-vim.html
44•whent•7h ago•6 comments

Evaluating Spec CPU2026

https://chipsandcheese.com/p/evaluating-spec-cpu2026
20•zdw•6h ago•3 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/
92•gslin•15h ago•79 comments

Improving C# Memory Safety

https://devblogs.microsoft.com/dotnet/improving-csharp-memory-safety/
131•soheilpro•2d ago•28 comments

Electrobun 2.0 will be decoupled from Bun due to the Rust rewrite

https://twitter.com/YoavCodes/status/2058064720553222567
103•bundie•10h ago•113 comments

Microsoft starts canceling Claude Code licenses

https://www.theverge.com/tech/930447/microsoft-claude-code-discontinued-notepad
440•robertkarl•1d ago•427 comments

Solving the “Zork” Mystery

https://www.dpolakovic.space/blogs/zork-part2
50•dpola•4d ago•18 comments