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.

Steam Machine launches today

https://store.steampowered.com/news/group/45479024/view/685257114654870245
882•theschwa•4h ago•760 comments

British Columbia, Time Zones, and Postgres

https://www.crunchydata.com/blog/british-columbia-and-time-zone-changes
55•sprawl_•2h ago•9 comments

Optocam Zero: a Pi Zero based digital camera made using off the shelf components

https://github.com/dorukkumkumoglu/optocamzero
42•iamnothere•2h ago•4 comments

Canyon HUD helmet for road riding

https://media-centre.canyon.com/en-INT/266866-new-canyon-heads-up-display-helmet-could-be-a-safet...
40•zh3•2d ago•33 comments

My Mathematical Regression

https://blog.dahl.dev/posts/my-mathematical-regression/
141•aleda145•3d ago•45 comments

Japanese symbols that speak without words

https://arun.is/blog/japan-symbols/
38•msephton•2h ago•7 comments

Kyber (YC W23) Is Hiring a Head of Engineering

https://www.ycombinator.com/companies/kyber/jobs/FGmI8mx-head-of-engineering
1•asontha•39m ago

Moebius: 0.2B image inpainting model with 10B-level performance

https://hustvl.github.io/Moebius/
186•DSemba•7h ago•55 comments

Show HN: Oak – Git alternative designed for agents

https://oak.space/oak/oak
114•zdgeier•6h ago•117 comments

Canada is looking to build up to 10 new nuclear reactors over the next 15 years

https://www.cbc.ca/news/politics/federal-nuclear-strategy-9.7244509
113•geox•2h ago•29 comments

Codex logging bug may write TBs to local SSDs

https://github.com/openai/codex/issues/28224
435•vantareed•14h ago•235 comments

Linux and Secure Boot certificate expiration (2025)

https://lwn.net/Articles/1029767/
71•weaksauce•3h ago•36 comments

Nintendo Wii U games running from a 1980's Bernoulli disk [video]

https://www.youtube.com/watch?v=8GZDOpV2OXk
76•zdw•1d ago•28 comments

Flock-Powered Police Chiefs Stalking Women Shows Why Warrants Are Needed

https://ipvm.com/reports/police-chiefs-track
132•jhonovich•2h ago•18 comments

GLM 5.2 vs. Opus

https://techstackups.com/comparisons/glm-5.2-vs-opus/
452•ritzaco•14h ago•307 comments

Prompt Injection as Role Confusion

https://role-confusion.github.io
117•x312•5h ago•60 comments

Blogger defeats photographer's copyright claim

https://blog.ericgoldman.org/archives/2026/06/blogger-defeats-photographers-copyright-claim-sokol...
72•speckx•4h ago•42 comments

PivCo-Huffman "Merge" Operations

https://fgiesen.wordpress.com/2026/06/21/pivco-huffman-merge-operations/
8•luu•22h ago•0 comments

Pledging another $400k to the Zig software foundation

https://mitchellh.com/writing/zig-donation-2026
667•tosh•7h ago•220 comments

Finding the Best Dog Treat with Statistics

https://www.wespiser.com/posts/2026-06-19-best-dog-treat.html
53•wespiser_2018•3h ago•11 comments

Walt Disney Company is the most successful at monetizing human nostalgia [audio]

https://www.acquired.fm/episodes/the-walt-disney-company
35•speckx•1h ago•20 comments

Die analysis of the 8087 math coprocessor's fast bit shifter (2020)

https://www.righto.com/2020/05/die-analysis-of-8087-math-coprocessors.html
68•Jimmc414•8h ago•13 comments

DisplayMate

https://www.displaymate.com/
64•skibz•4h ago•21 comments

The text in Claude Code’s “Extended Thinking” output

https://patrickmccanna.net/the-text-in-claude-codes-extended-thinking-output-is-not-authentic/
239•0o_MrPatrick_o0•7h ago•175 comments

Jobs and Software Is Fucked

https://urflow.bearblog.dev/jobs-and-software-is-fucked/
215•speckx•1h ago•172 comments

Memory crisis is getting so bad that even retro RAM prices are going to the Moon

https://www.theregister.com/personal-tech/2026/06/22/the-memory-crisis-is-getting-so-bad-that-eve...
51•speckx•2h ago•8 comments

Deno Desktop

https://docs.deno.com/runtime/desktop/
979•GeneralMaximus•16h ago•362 comments

Chevron signs 20-year power agreement with Microsoft for West Texas data center

https://www.chevron.com/newsroom/2026/q2/chevron-signs-20-year-power-agreement-with-microsoft-for...
94•cdrnsf•7h ago•94 comments

Help I accidentally a wigglegram

https://lmao.center/blog/wiggle-accidents/
458•gregsadetsky•2d ago•117 comments

Show HN: Got sick of ads, so I made my own logic puzzle site

https://puzzlelair.com/
104•HaxleRose•9h ago•81 comments