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.

Attention Kmart shoppers – music played over the store PA

https://www.attentionshoppers.net/
270•ChrisArchitect•6h ago•71 comments

FP64 Is All You Want, INT8 Is All You Need, FP4/6/8 Is All You Have

https://arxiv.org/abs/2609.37693
48•peter_d_sherman•2h ago•9 comments

Beating Floating Point at Its Own Game: Posit Arithmetic [pdf]

http://www.johngustafson.net/pdfs/BeatingFloatingPoint.pdf
47•xenophonf•3h ago•9 comments

Optimizing Rustc's next trait solver

https://donsz.nl/blog/optimizing-rustcs-next-trait-solver
109•g0xA52A2A•3d ago•8 comments

The M-Vave FM-1 FM Synthesizer and the Success of Open Source

https://www.hashbangcode.com/article/m-vave-fm-1-fm-synthesizer-and-success-open-source
31•speckx•4d ago•7 comments

Show HN: I made a tiny live replica of SFO

https://tinysfo.com/
463•eiiot•12h ago•136 comments

Elephant in the Server Room: >50% of WordPress Sites Run PHP That's EOL by Jan

https://adamgreenough.net/blog/wordpress-php-versions/
103•xadz•8h ago•38 comments

PocketBase has moved off GitHub

https://github.com/pocketbase/pocketbase/issues/7872
308•p2detar•11h ago•139 comments

People who refuse to let their gadgets die

https://www.theatlantic.com/technology/2026/10/meet-the-upgrade-holdouts/688874/
65•FinnLobsien•2d ago•54 comments

The importance of understanding what the fuck is going on

https://blog.jsbarretto.com/post/understanding
374•notfried•11h ago•239 comments

Guitar Hero, but for Hand Bells

https://dingdonghero.com
34•devbytes•3d ago•5 comments

Gmail "Send as" ends January 2027

https://emailfa.st/blog/gmail-send-as-ending-cloudflare-email-routing
419•tombakerbuilds•13h ago•227 comments

Show HN: RK3588 QEMU emulator running unmodified Armbian images

https://github.com/petabyt/clanker-rkqemu
28•petabyt•4h ago•7 comments

The Rise of Low Precision Floating Point

https://arxiv.org/abs/2611.00301
36•matt_d•4d ago•5 comments

Silently degrading the model's capabilities is sabotage

https://twitter.com/TimSweeneyEpic/status/2126682647862862063
824•tosh•17h ago•334 comments

Computer Disease (2020)

https://blog.adafruit.com/2020/01/13/richard-feynman-on-computer-disease/
238•jvanderbot•16h ago•91 comments

Audible removes download option for purchased audiobooks

https://consumerrights.wiki/w/Audible_removes_download_option_for_purchased_audiobooks
1322•DeepLogin•21h ago•488 comments

Hacker Laws

https://github.com/dwmkerr/hacker-laws
35•Olshansky•5h ago•5 comments

Lua as a Query Language

https://remy.wang/blog/lua-ql.html
71•remywang•9h ago•17 comments

The Engineering of Smartphone Cameras

https://www.pencilofrays.com/smartphone-lens/
44•nbernard•3d ago•21 comments

Doug Lea's Malloc

https://gee.cs.oswego.edu/dl/html/malloc.html
34•grep_it•3d ago•16 comments

Show HN: Archive95, an independent archive of the early web

https://archive95.net/
108•WumboSpasm•12h ago•14 comments

Cryptography Incident Review Group

https://cirg.org/
67•heavensteeth•8h ago•31 comments

Conductive Dyes Could Turn Hair into EEG Electrodes

https://spectrum.ieee.org/hair-dye-eeg-electroencephalography
54•dverlaeckt80•2d ago•14 comments

Tilapia are multi-jawed monsters

https://signoregalilei.com/2026/11/01/tilapia-are-multi-jawed-monsters/
88•surprisetalk•3d ago•26 comments

What Changes When You Upgrade from PostgreSQL 18 to 19

https://rishigoomar.com/postgresql-19-what-changes-when-you-upgrade-from-18
105•rgoomar•2d ago•27 comments

M 5.0 earthquake - 5 km SW of Manhattan Beach, CA

https://earthquake.usgs.gov/earthquakes/eventpage/ci41581136/executive
196•dboreham•15h ago•122 comments

Rez Dreamcast, Its Prototypes: When Psychedelic Gaming Came to Consoles

https://www.sega-dreamcast-info.com/en/rez-dreamcast-development-history-prototypes-minidv-archive
110•zdw•4d ago•40 comments

Show HN: Peat, a fork of Neon's serverless Postgres, on Postgres 18 and 19

https://github.com/Zabaca/peat
81•uptownhr•10h ago•13 comments

An Update from Lapham's Quarterly

https://laphamsquarterly.substack.com/p/three-years-on-help-us-with-the-next
39•m-hodges•4h ago•1 comments