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.

Escape IntelliJ: Scala and Kotlin LSPs on Emacs Eglot

https://jointhefreeworld.org/blog/articles/emacs/emacs-eglot-scala-kotlin/index.html
51•jjba23•1d ago•9 comments

Terence Tao's ChatGPT conversation about the Jacobian Conjecture counterexample

https://chatgpt.com/share/6a5fdc7a-d6f8-83e8-bbea-8deb42cfed56
860•gmays•15h ago•498 comments

git's –end-of-options Flag

https://nesbitt.io/2026/07/21/end-of-options.html
137•Erenay09•1d ago•70 comments

Cruller: Bun's Zig Runtime, Continued on Zig 0.16

https://ziggit.dev/t/cruller-buns-zig-runtime-continued-on-zig-0-16/16734
40•Erenay09•3h ago•12 comments

Quality non-fiction books are the antithesis of AI slop

https://resobscura.substack.com/p/quality-non-fiction-books-are-the
352•benbreen•18h ago•118 comments

ANSI escape injection in MCP servers: Hidden from humans, visible to AI

https://brightsec.com/research/detecting-ansi-escape-sequence-injection-in-mcp-servers-with-dast/
10•xgpyc2qp•2d ago•1 comments

GigaToken: ~1000x faster Language model tokenization

https://github.com/marcelroed/gigatoken/
500•syrusakbary•15h ago•102 comments

Show HN: Bento - An entire PowerPoint in one HTML file (edit+view+data+collab)

https://bento.page/slides/
824•starfallg•17h ago•182 comments

Everyone should know SIMD

https://mitchellh.com/writing/everyone-should-know-simd
416•WadeGrimridge•15h ago•152 comments

Amiga 1000: Ten years ahead of its time

https://dfarq.homeip.net/amiga-1000-ten-years-ahead-of-its-time/
70•giuliomagnifico•3h ago•37 comments

Are AI labs pelicanmaxxing?

https://dylancastillo.co/posts/pelicanmaxxing.html
521•dcastm•15h ago•205 comments

So Reddit has decided that plain HTML is unsafe

https://www.cole-k.com/2026/07/21/reddit/
451•montroser•20h ago•456 comments

Making ASCII Art in Vim

https://alexyang.dev/vim-ascii-art/
62•evakhoury•2d ago•6 comments

Show HN: Cactus Hybrid: We taught Gemma 4 to know when it's wrong

https://github.com/cactus-compute/cactus-hybrid
129•HenryNdubuaku•15h ago•24 comments

Why malloc always does more than I asked for?

https://ssenthilnathan3.github.io/blog/malloc/
30•nathaah3•2d ago•20 comments

Medici family mystery may be solved after more than 400 years

https://www.cnn.com/2026/07/15/science/medici-family-mystery-dna-malaria
119•effects•11h ago•31 comments

Making

https://beej.us/blog/data/ai-making/
353•erikschoster•17h ago•141 comments

The startup's Postgres survival guide

https://hatchet.run/blog/postgres-survival-guide
404•abelanger•20h ago•189 comments

John C. Dvorak has died

https://twitter.com/na_announce/status/2079952538040672302
741•coleca•13h ago•243 comments

Computing Camera Rays

https://momentsingraphics.de/CameraRays.html
3•ibobev•1w ago•0 comments

Malleable Computing, Emacs, and You

http://yummymelon.com/devnull/malleable-computing-emacs-and-you.html
105•kickingvegas•11h ago•30 comments

ascdraw: Editor for ASCII/UTF-8 diagrams (in 144FPS)

https://github.com/exlee/ascdraw
43•xlii•2d ago•5 comments

Fairphone 6 wide camera experimental Linux support

https://nondescriptpointer.com/articles/fairphone-6-wide-camera-linux/
120•helonaut•12h ago•31 comments

Protecting our FLOSS commons from LLMs

https://blog.codeberg.org/protecting-our-floss-commons-from-llms.html
42•acmnrs•7h ago•3 comments

Businesses with ugly AI menu redesigns

https://blog.fiddery.com/businesses-with-ugly-ai-menu-redesigns/
283•speckx•20h ago•188 comments

Restructuring GitHub's bug bounty program

https://github.blog/security/next-chapter-restructuring-githubs-bug-bounty-program/
45•soheilpro•6h ago•16 comments

Nobody knows what a used GPU cluster is worth

https://ciphertalk.substack.com/p/nobody-knows-what-a-used-gpu-cluster
228•rbanffy•1w ago•202 comments

Back to Kagi

https://blog.melashri.net/micro/back-to-kagi/
266•speckx•20h ago•197 comments

All 253 Patterns from Christopher Alexander's a Pattern Language Summarized

https://claytondorge.com/patterns-list
72•toomuchtodo•11h ago•13 comments

Ghost Cut – Or why Cut and Paste is broken everywhere

https://ishmael.textualize.io/blog/ghost-cut/
165•willm•18h ago•111 comments