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.

GPT-5.6

https://openai.com/index/gpt-5-6/
723•logickkk1•3h ago•518 comments

GLM 5.2 is nearly as accurate as a human book keeper

https://toot-books.pages.dev/blog/glm-5-2-vat-benchmark
111•adamkurkiewicz•1h ago•59 comments

ChatGPT Work

https://openai.com/index/chatgpt-for-your-most-ambitious-work/
267•Tiberium•3h ago•111 comments

Show HN: 18 Words

https://18words.com/
679•pompomsheep•7h ago•254 comments

EU Parliament greenlights Chat Control 1.0

https://www.patrick-breyer.de/en/eu-parliament-greenlights-chat-control-1-0-breyer-our-children-l...
737•rapnie•9h ago•373 comments

Hy3

https://hy.tencent.com/research/hy3
254•andai•4h ago•63 comments

How to Start a Ruby Meetup

https://guides.rubyevents.org/meetups/
34•mooreds•2h ago•8 comments

Buried Apple Feature Turns an iPhone into the Perfect Kids' Dumb Phone

https://www.wired.com/story/this-buried-apple-feature-turns-an-iphone-into-the-perfect-kids-dumb-...
129•PotatoNinja•3d ago•93 comments

Train SIM Created by Just One Person Is Being Called the Best Ever Made

https://kotaku.com/a-train-sim-created-by-just-one-person-is-being-called-the-best-ever-made-2000...
59•oumua_don17•4d ago•9 comments

Girls Just Wanna Have Fast MPMC Queues with Bounded Waiting

https://nahla.dev/blog/waitfree_queue/
86•EvgeniyZh•3d ago•16 comments

A possible future for Damn Interesting

https://www.damninteresting.com/a-possible-future/
151•mzur•5h ago•13 comments

TLS certificates for internal services done right

https://tuxnet.dev/posts/tls-for-internal-services/
100•mrl5•5h ago•66 comments

Wildcard (YC W25) Is Hiring a Founding Engineer

https://www.ycombinator.com/companies/wildcard/jobs/ZSLVaaU-founding-engineer
1•kaushikmahorker•3h ago

The glass backbone: Why the Army's logistics will break in the next war

https://mwi.westpoint.edu/the-glass-backbone-why-the-armys-logistics-will-break-in-the-next-war/
216•baud147258•7h ago•277 comments

Muse Spark 1.1

https://ai.meta.com/blog/introducing-muse-spark-meta-model-api/
260•ot•6h ago•151 comments

Why the Next Era of AI Is About Infrastructure, Not Just Models

https://blog.mozilla.ai/the-control-layer-why-the-next-era-of-ai-is-about-infrastructure-not-just...
14•royapakzad•5h ago•3 comments

Show HN: Getting GLM 5.2 running on my slow computer

https://github.com/JustVugg/colibri
13•vforno•12h ago•0 comments

Launch HN: Context.dev (YC S26) – API to get structured data from any website

https://www.context.dev
53•TheYahiaBakour•4h ago•39 comments

Opinionated and Easy Pi.dev Configuration

https://lazypi.org/
78•lwhsiao•5h ago•47 comments

No leap second will be introduced at the end of December 2026

https://datacenter.iers.org/data/latestVersion/bulletinC.txt
181•ChrisArchitect•6h ago•149 comments

Show HN: I mapped 8.5M research papers into an interactive atlas

https://tomesphere.com/atlas
41•leonickson•18h ago•10 comments

Show HN: Analog Watch

https://analog.watch
76•ezekg•5h ago•67 comments

Meta reuses old RAM in new servers with custom bridge chip

https://www.networkworld.com/article/4192827/meta-reuses-old-ram-in-new-servers-with-custom-bridg...
264•ihsw•6d ago•180 comments

Show HN: Pylon Sync, an agent-first full-stack realtime framework

https://www.pylonsync.com
4•ericc59•2h ago•0 comments

Show HN: Abralo – Free, easy way to run several Claude Code agents in one window

https://abralo.com/
16•cwbuilds•1d ago•6 comments

How to Follow a Drummer

https://drummate.app/blog/how-to-follow-a-drummer
20•sashyo•3d ago•16 comments

AI changes the economics of software rewrites

https://thetruthasiseeitnow.com/ai-slop-starts-with-the-codebase-itself/
84•cinooo•14h ago•95 comments

New open access book on history of computers and politics

https://mitpress.mit.edu/9780262053198/simpolitics/
50•mckelveyf•6h ago•4 comments

What the New Executive Order Means for Secure Software Delivery in Government

https://www.rise8.us/resources/ai-executive-order-secure-software-delivery-government
5•mooreds•29m ago•0 comments

Spider venom kills varroa mites without harming honeybees

https://connectsci.au/news/news-parent/9703/Spider-venom-kills-varroa-mites-without-harming
279•Jedd•15h ago•126 comments