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.

Android May Soon Restrict On-Device ADB

https://kitsumed.github.io/blog/posts/android-may-soon-restrict-on-device-adb/
229•shscs911•3h ago•117 comments

Claude Opus 5

https://www.anthropic.com/news/claude-opus-5
1563•alvis•17h ago•908 comments

Hannah Fry Wins the Leelavati Prize in 2026 for Mathematics Outreach

https://www.maths.cam.ac.uk/features/professor-hannah-fry-wins-leelavati-prize
232•agnishom•8h ago•37 comments

GC and Exceptions in Wasmtime

https://bytecodealliance.org/articles/wasmtime-gc
102•phickey•4d ago•3 comments

Building a Tiny 3D Renderer for a Tiny Handheld

https://saffroncr.itch.io/katavatis/devlog/1534514/building-a-tiny-3d-renderer-for-a-tiny-handheld
37•g0xA52A2A•2d ago•1 comments

NYC Apartment Aquaponics

https://erinmurphy.dev/projects/project-2/
50•mm1119•4d ago•13 comments

ARC-AGI Leaderboard

https://arcprize.org/leaderboard
82•rzk•3h ago•64 comments

UK AISI / Caisi Preliminary Assessment of Kimi K3's Cyber Capabilities

https://www.nist.gov/news-events/news/2026/07/uk-aisi-caisi-preliminary-assessment-kimi-k3s-cyber...
66•walrus01•6h ago•17 comments

Extinct Media Museum Tokyo

https://extinct-media-museum.blog.jp/otemachi/
34•sohkamyung•4h ago•5 comments

Postgres LISTEN/NOTIFY actually scales

https://www.dbos.dev/blog/postgres-listen-notify-scalability
310•KraftyOne•15h ago•59 comments

Opus 5 is currently #1 on Artificial Analysis Intelligence Leaderboard

https://artificialanalysis.ai/models
300•aarondong•14h ago•166 comments

Charles Ross spent 50 yrs building Star Axis naked-eye observatory in New Mexico

https://www.nytimes.com/2026/07/22/arts/design/charles-ross-star-axis-land-art.html
10•ChrisArchitect•2d ago•2 comments

India's first privately-developed rocket reaches orbit on debut launch

https://arstechnica.com/space/2026/07/indias-first-privately-developed-rocket-reaches-orbit-on-dr...
615•sohkamyung•5d ago•175 comments

My security camera shipped a GitHub admin token in its login page

https://hhh.hn/hanwha-github-token/
596•hhh•22h ago•191 comments

Sperm Whales blow bubbles to achieve restful, vertical sleep

https://news.st-andrews.ac.uk/archive/sperm-whales-blow-bubbles-to-achieve-restful-vertical-sleep/
97•hhs•11h ago•20 comments

Show HN: I simulated closing the Strait of Hormuz on real oil trade data

https://globaloilnetwork.staffinganalytics.io/
183•eliotho•1d ago•87 comments

Book Corners: Community map of neighborhood book exchange spots

https://www.bookcorners.org
23•NaOH•2d ago•7 comments

An old patent inspired the new "Y-zipper", a three-sided fastener

https://news.mit.edu/2026/three-sided-y-zipper-design-0504
199•crescit_eundo•2d ago•41 comments

Firefox Containers Preview

https://blog.mozilla.org/en/firefox/firefox-containers-preview/
349•twapi•3d ago•99 comments

Designing an Ethernet Switch ASIC

https://essenceia.github.io/projects/ethernet_switch_asic/
185•random__duck•4d ago•44 comments

PartialString – A finite-difference time-domain physical modelling synthesiser

https://differentinstruments.com/
21•shmeano•3d ago•4 comments

Kimi K3 exploited the latest Redis server

https://twitter.com/fried_rice/status/2080059356322918777
228•Alifatisk•1d ago•64 comments

MouthPad: A Tongue-Controlled Touchpad

https://www.augmental.tech/
8•ZaninAndrea•2h ago•1 comments

Taylor Farms Called White House to Try to Delay Cyclospora Recall

https://www.wsj.com/health/taylor-farms-cyclospora-recall-delay-call-41fef0bc
217•JumpCrisscross•7h ago•103 comments

Half-Life 2 running natively on HaikuOS

https://discuss.haiku-os.org/t/haiku-nvidia-porting-nvidia-driver-for-turing-gpus/16520?page=18
306•m0do1•21h ago•56 comments

IRGC claims it destroyed Amazon's Bahrain data center

https://houseofsaud.com/irgc-claims-destroyed-amazon-bahrain-data-center/
299•thisislife2•1d ago•358 comments

Nvidia, Microsoft, Meta warn against overregulating open-weight models

https://www.cnbc.com/2026/07/24/nvidia-microsoft-meta-open-weight-ai-models.html
613•louiereederson•20h ago•268 comments

Don't Take the Black Pill [video]

https://www.youtube.com/watch?v=zLZwpH5lCD4
189•signa11•17h ago•157 comments

Fil-C: Garbage In, Memory Safety Out [video]

https://www.youtube.com/watch?v=5F-2Y1LPRek
142•Bootvis•1d ago•144 comments

Future euro banknote design proposals

https://www.ecb.europa.eu/euro/banknotes/future_banknotes/html/all-design-proposals.en.html
200•robin_reala•1d ago•187 comments