Elektrine lite

← Feed

@lisyarus@mastodon.gamedev.place

Post #1170474

2026-04-14 15:53 UTC

New blog post! I really hope I'll get a decent amount of people mad with this one 😈😈😈 It's OK to compare floating-points for equality: https://lisyarus.github.io/blog/posts/its-ok-to-compare-floating-points-for-equality.html #programming #computing #floatingpoint #geometry

Replies (15)

  • @lisyarus@mastodon.gamedev.place Thanks a lot! This is one of my larger pet peeves. In the last 10 years I probably needed exact comparisons 95%+ of the time (if you exclude test code). And I mostly regretted any epsilon comparison in time. This is all with the exception of tests, where "approximate equal to the expected result" is fine. But basically "load-bearing" comparisons with eps didn't bear any load in the end...

    Open ##2797230

  • @aras@mastodon.gamedev.place 2026-04-14 16:24

    @lisyarus@mastodon.gamedev.place this post did not make me mad!

    Open ##2797234

  • @mirabilos@toot.mirbsd.org 2026-04-14 16:25

    @lisyarus@mastodon.gamedev.place but what’s it about to justify leaving the Fedi client for a webbrowser? For all I know it could be about, idk, reproduction of snails (Fedi is weird so…). No description, no teaser, not even a title, and no speaking URL either (if it is speaking, don’t truncate it when you post). This doesn’t entice me, I have to say:

    Open ##2797236

  • @lisyarus@mastodon.gamedev.place Seems pretty reasonable to me!

    Open ##2797245

  • @lisyarus@mastodon.gamedev.place > In reality it is a pretty deterministic (modulo compiler options, CPU flags, etc) and then det(v, v) suddenly doesn't return 0.0 anymore because the compiler now targets a newer CPU with FMA and uses regular multiplication for `a.y * b.x` and FMA for `a.x * b.y - prev_result`, and FMA uses infinite precision so `a.x * b.y` can be slightly different from the result of the regular multiplication -_- It annoys me that IEEE754 allows this and compilers (ab)use it

    Open ##2797246

  • @oblomov@sociale.network 2026-04-14 16:55

    @lisyarus@mastodon.gamedev.place fun fact, I agree with the sentiment but some of the examples are horrible, but before commenting further I'd prefer your permission 8-D

    Open ##2797264

  • @lisyarus@mastodon.gamedev.place Not a controversial statement among numerics people at all, FWIW. :)

    Open ##2797278

  • @mbr@mastodon.gamedev.place 2026-04-14 18:21

    @lisyarus@mastodon.gamedev.place It's a perfectly normal thing to do. But then so is approximately equal testing...it might need to test both abs and rel measures though.

    Open ##2797279

  • @lisyarus@mastodon.gamedev.place symbolic algebra knows no floats :-)

    Open ##2797280

  • @lisyarus@mastodon.gamedev.place for some examples, I wondered why floats, at all? A grid is not in need of floats. Neither is the passing of time. 1/4 of an hour is just a representation. 60 / 4 or 3600 / 4 also. I do use floats but as little as possible. In audio land, using floats just increases deps. Like, need fpu all of a sudden. My conventions, where time is concerned, all use the heuristic of Hz/cps and time becomes a counting issue. Probably missunderstood something.

    Open ##2797281

  • @bnut@aus.social 2026-04-14 21:09

    @lisyarus@mastodon.gamedev.place looks interesting, I’ll read it later! I feel like it’s obligatory for me to link https://herbie.uwplse.org/demo/ here. It’s an excellent tool for finding alternative math expressions which are faster and/or have better float precision.

    Open ##2797282

  • @gray17@mastodon.social 2026-04-15 00:24

    @lisyarus@mastodon.gamedev.place I learned "use epsilon" before IEEE-754 existed, when you didn't necessarily know how your floats behaved at boundary conditions. and I never really thought about it much after floats became universally standardized. this feels like a case of a best-practice principle losing its context and getting misapplied in a changed environment

    Open ##2797283

  • @lisyarus@mastodon.gamedev.place thanks for sharing this. What do you think about using double precision for intermediates when inputs are in single precision? For example, in your "vector length" example it's much cheaper to cast x, y, z to double instead of dividing by max(x, y, z) (in context of a CPU target, not GPU) — after the cast extreme exponent values do not pose an issue. This is exactly how hypotf in a modern C math library works — it's the double-precision hypot that needs to be fancier than that.

    Open ##2797284

  • @lisyarus@mastodon.gamedev.place Thank you for this article. I found it enjoyable and useful!

    Open ##2797288

  • @lisyarus@mastodon.gamedev.place Related talk by Rose Peck a.k.a. sixfold-origami at last year's RustWeek (not specific to Rust): https://www.youtube.com/watch?v=JY4nYnLrnt8 Probably no new insights there for you, but maybe for others in this thread, and very entertaining in any case.

    Open ##2797289