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.

Owed a billion dollars in Nvidia stock

https://colo.to/nvidia-stock-narrative.html
310•Eric_Gullichsen•3h ago•153 comments

Thinking Fast and Slow in AI: The Role of Metacognition

https://arxiv.org/abs/2110.01834
27•teleforce•1h ago•2 comments

Nissan's third generation e-POWER powertrain

https://www.nissan-global.com/EN/INNOVATION/TECHNOLOGY/ARCHIVE/E_POWER_GEN3/
28•mroche•2h ago•29 comments

Ember-1

https://fireworks.ai/blog/ember-1
395•gmays•11h ago•197 comments

When did Google get so weird?

https://sancho.bearblog.dev/google-weird/
980•sancho-panza•8h ago•529 comments

Alan Kay's answer to “Did the ENIAC have a BIOS”?

https://www.quora.com/Did-the-ENIAC-have-a-BIOS/answer/Alan-Kay-11
96•midnightfish•9h ago•33 comments

There is more to code review than (automatable) detection

https://www.adaptivecapacitylabs.com/2026/08/24/there-is-more-to-code-review-than-automatable-det...
95•utiiiD•1d ago•53 comments

The state of SIMD in Rust in 2026

https://shnatsel.github.io/state-of-simd-rust-2026/
112•verdagon•2d ago•23 comments

Malleable software: Restoring user agency in a world of locked-down apps (2025)

https://www.inkandswitch.com/essay/malleable-software/
22•evakhoury•10h ago•5 comments

Don't couple your Go code to GitHub

https://iain.rocks/blog/dont-couple-your-go-code-to-github
185•birdculture•12h ago•90 comments

Self-Hosting on the Dark Web

https://david.alvarezrosa.com/posts/self-hosting-on-the-dark-web/
104•mooreds•9h ago•34 comments

Goodbye to the Hard Parts That Never Mattered

https://jakegoldsborough.com/blog/2026/goodbye-to-the-hard-parts-that-never-mattered/
7•JumpCrisscross•3h ago•1 comments

Microsoft drops Copilot+ branding from its new laptops

https://www.tomshardware.com/tablets/microsoft-surface/microsoft-quietly-drops-copilot-branding-f...
32•anthuswilliams•2h ago•14 comments

Show HN: Lofi Cities – Pixel-art city nights with browser-generated lofi

https://loficities.com/
201•safaelmali•10h ago•88 comments

Guitar amp and effects pedal built on the Waveshare ESP32-S3-Touch-AMOLED-2.06

https://github.com/dashersw/coyopedal
38•arbayi•2d ago•16 comments

What I did at Recurse Center

https://thill.me/2026/09/11/what-i-did-at-rc.html
86•bingden•10h ago•25 comments

Deterministic Concurrency [video]

https://www.youtube.com/watch?v=25x0UuSCKuU
5•surprisetalk•2d ago•0 comments

Imp is a full port of DSPy to the BEAM

https://github.com/deepfates/imp
61•mpweiher•9h ago•6 comments

In an $80 motel room, a discovery to shed light on the origins of life

https://www.nytimes.com/2026/09/26/science/motel-science-discovery.html
225•danso•14h ago•84 comments

Lunar Terminator Paradox

https://notes.secretsauce.net/notes/2026/09/27_lunar-terminator-paradox.html
56•dima55•8h ago•40 comments

Was Silent Reading Unusual During Augustine's Time?

https://www.historyofinformation.com/detail.php?entryid=4341
14•ryanscio•3h ago•0 comments

Oral history of John Chowning, inventor of FM synthesis [video]

https://www.youtube.com/watch?v=e1Xn3030IvM
62•Rochus•11h ago•15 comments

TabPFN and TabICL vs. tuned XGBoost: the model that doesn't train won 14/14

https://efraingaray.com/en/blog/tabpfn-vs-xgboost/
7•EfrainGaray•2h ago•3 comments

Previously unheard recordings of John Coltrane, captured by Frank Tiberi

https://www.jazzwise.com/content/news/john-coltrane-centenary-celebrations-see-impulse-records-re...
78•gregsadetsky•2d ago•28 comments

Replacing the old battery on rechargeable bike lights

https://jvns.ca/blog/2026/09/27/replacing-the-old-battery-on-rechargeable-bike-lights/
150•surprisetalk•15h ago•83 comments

Writing Efficient C++ Code (2013)

https://asawicki.info/articles/writing_efficient_cpp_code.php
141•ibobev•2d ago•87 comments

Research finds 485 chemicals in US pesticide products linked to breast cancer

https://www.theguardian.com/us-news/2026/sep/26/breast-cancer-us-pesticide-products
71•Teever•3h ago•21 comments

As A.I. makes law firms more efficient, clients ask: 'Where's my discount?'

https://www.nytimes.com/2026/09/26/business/dealbook/ai-law-discount-billable-hour.html
92•mooreds•3h ago•81 comments

A New Experiment Meta-Strategy

https://chillphysicsenjoyer.substack.com/p/a-new-experiment-meta-strategy
18•surprisetalk•2d ago•0 comments

The Cartesian Hand: In-Hand Manipulation with All-Linear Fingers

https://generalroboticslab.com/cartesian_handv1
74•AareyBaba•1d ago•11 comments