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.

Sudoku's Hidden Rule

https://cyrusjavan.com/posts/solving-with-uniqueness/
229•multifasciatus•6h ago•102 comments

The state of Rust allocators in 2026: stabilised

https://cetra3.github.io/blog/state-of-allocators-2026-finally-stabilised/
92•fanf2•2d ago•18 comments

Post-quantum encryption adoption

https://radar.cloudflare.com/post-quantum
269•mfiguiere•10h ago•128 comments

Claude and I found a dwarf planet candidate

https://www.joshuawolk.com/writing/claude-and-i-found-a-dwarf-planet-candidate
364•nhatcher•10h ago•145 comments

If you want failover to work well, use it often

https://bencane.com/posts/2026-11-25-failover-often/
409•madflojo•16h ago•101 comments

Self-hosted software development factory

https://kudmitry.com/articles/self-hosted-software-development-factory/
188•ingve•10h ago•77 comments

So, how fast is numerical computation in Lean?

https://robertboy18.github.io/TorchLean-Verified-Examples/week-09-floatlib-benchmarks/
19•matt_d•2d ago•0 comments

Ozempic indirectly boosts fertility, causing surprise pregnancies

https://health.clevelandclinic.org/ozempic-babies
230•illidan1000•10h ago•195 comments

Cyanobacterium can adapt to simulated Martian conditions

https://bioengineer.org/simulated-martian-conditions-reshape-cyanobacterial-growth-and-lipid-meta...
97•hackthemack•10h ago•29 comments

Nearly 2B credentials exposed in a publicly accessible database

https://www.expressvpn.com/research/2billion-credentials-exposed
244•basilgohar•16h ago•131 comments

TrojanMaxxing: One-shotting VLM backdoors with weight-space programming

https://arxiv.org/abs/2611.27425
231•hvp1621•16h ago•73 comments

Banned from Google?

https://bannedfromgoogle.com/
569•ondas•10h ago•289 comments

Emulator for Symbolics XL-1200 Lisp machine

https://unya.github.io/xl1200-emu/
35•p_l•5h ago•4 comments

Happy 60th Birthday to PDP-10, running ITS and Zork

https://hyperties.org/machine/games/zork/
47•DonHopkins•2d ago•3 comments

Historic Great Lakes Bulker Departs for Boneyard

https://maritime-executive.com/article/historic-great-lakes-bulker-set-to-depart-for-boneyard
20•hermitdev•2d ago•5 comments

Lessons from Charlie Munger's Harvard School Commencement Speech

https://www.umr.io/blog/charlie-munger-harvard-speech
81•umerf•8h ago•27 comments

AWS Launches Quick Docs, Sheets and Slides

https://www.aboutamazon.com/news/aws/aws-quick-ai-for-work
14•onza•54m ago•3 comments

Anthropic has made a major breakthrough in theoretical cryptanalysis

https://twitter.com/AndrewCurran_/status/2127127873802911864
442•bifftastic•14h ago•388 comments

New York Ban AI Chatbot Toys for 5 Years

https://www.governor.ny.gov/news/consumers-begin-their-holiday-shopping-season-governor-hochul-si...
154•DeepLogin•5d ago•110 comments

So, I rewrote my game in C3

https://www.madrigalgames.com/blog/so-i-rewrote-my-game-in-c3/
124•lerno•2d ago•25 comments

Why I can't sell you a €1 email inbox

https://www.jannis.io/why-i-cant-sell-you-a-eur1-email-inbox/
840•amaccuish•15h ago•370 comments

How to Brew Solar Powered Coffee

https://solar.lowtechmagazine.com/2025/11/how-to-brew-solar-powered-coffee/
70•Sir_Twist•2d ago•42 comments

Underrated reasons to be thankful VI

https://dynomight.net/thanks-6/
189•Curiositry•15h ago•62 comments

Mass Destruction of Weapons

https://computer.rip/2026-11-22-mass-destruction-of-weapons.html
78•Sniffnoy•6d ago•36 comments

"This message has been on the screen for almost two hours now."

https://unsung.aresluna.org/this-message-has-been-on-the-screen-for-almost-two-hours-now/
278•firloop•1d ago•169 comments

Nvidia reportedly cuts gaming GPU supply by another 20-30% for Q4

https://videocardz.com/newz/nvidia-reportedly-cuts-gaming-gpu-supply-by-another-20-30-for-q4
190•extasetoi12•9h ago•124 comments

Ask HN: Apple suspended my dev account 69 days ago and no human will respond

4•spichakovda•28m ago•0 comments

Looped Models are Fast Multi-Token Predictors

https://arxiv.org/abs/2611.33896
101•ComputedCamp995•8h ago•15 comments

The Math Behind Counter-Strike Surfing

https://healeycodes.com/the-math-behind-counter-strike-surfing
377•v3gas•2d ago•62 comments

Borland's DOS compilers, rebuilt from their own binaries

https://github.com/abedegno/borland-reborn
235•frostbyte7•21h ago•84 comments