frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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•6mo ago

Comments

LegionMammal978•6mo 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•6mo 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•6mo 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.

Superman copy found in mum's attic is most valuable comic ever at $9.12M

https://www.bbc.com/news/articles/c8e9rp0knj6o
219•1659447091•5h ago•89 comments

Roblox CEO Makes a Fool of Himself in Car-Crash Interview

https://kotaku.com/roblox-new-york-times-interview-baszucki-2000646174
24•tobr•1h ago•7 comments

Personal blogs are back, should niche blogs be next?

https://disassociated.com/personal-blogs-back-niche-blogs-next/
331•gnabgib•12h ago•209 comments

Kodak Ran a Secret Nuclear Device in Its Basement for Decades

https://www.popularmechanics.com/science/energy/a69147321/kodak-film-nuclear-reactor/
69•cainxinth•6d ago•7 comments

Helping Valve to power up Steam devices

https://www.igalia.com/2025/11/helpingvalve.html
615•TingPing•17h ago•198 comments

The Connectivity Standards Alliance Announces Zigbee 4.0 and Suzi

https://csa-iot.org/newsroom/the-connectivity-standards-alliance-announces-zigbee-4-0-and-suzi-em...
49•paulatreides•3d ago•25 comments

Samsung's 60% DRAM price hike signals a new phase of global memory tightening

https://www.buysellram.com/blog/samsungs-memory-price-surge-sends-shockwaves-through-the-global-d...
283•redohmy•1w ago•230 comments

Show HN: Wealthfolio 2.0- Open source investment tracker. Now Mobile and Docker

https://wealthfolio.app/?v=2.0
530•a-fadil•18h ago•174 comments

Moss Survives 9 Months in Space Vacuum

https://scienceclock.com/moss-survives-9-months-in-space-vacuum/
77•ashishgupta2209•7h ago•33 comments

Single-Celled Marine Organisms Resulted in an Influential Illustrated Book

https://lithub.com/how-the-discovery-of-single-celled-marine-organisms-resulted-in-one-of-the-mos...
7•PaulHoule•1w ago•0 comments

How I learned Vulkan and wrote a small game engine with it (2024)

https://edw.is/learning-vulkan/
102•jakogut•11h ago•53 comments

Sharper MRI scans may be on horizon thanks to new physics-based model

https://news.rice.edu/news/2025/sharper-mri-scans-may-be-horizon-thanks-new-physics-based-model
70•hhs•10h ago•18 comments

We should all be using dependency cooldowns

https://blog.yossarian.net/2025/11/21/We-should-all-be-using-dependency-cooldowns
364•todsacerdoti•20h ago•215 comments

Event Sourcing in Go: From Zero to Production

https://skoredin.pro/blog/golang/event-sourcing-go
17•tdom•4d ago•3 comments

Discontinuation of ARM Notebook with Snapdragon X Elite SoC

https://www.tuxedocomputers.com/en/Discontinuation-of-ARM-notebooks-with-Snapdragon-X-Elite-SoC.t...
132•Venn1•15h ago•57 comments

Self-hosting a NAT Gateway

https://www.awsistoohard.com/blog/self-hosting-nat-gateway
129•veryrealsid•4d ago•75 comments

You can make PS2 games in JavaScript

https://jslegenddev.substack.com/p/you-can-now-make-ps2-games-in-javascript
268•tosh•18h ago•66 comments

LAPD helicopter tracker with real-time operating costs

https://lapdhelicoptertracker.com/
158•polalavik•12h ago•179 comments

Childhood Friends, Not Moms, Shape Attachment Styles Most

https://nautil.us/childhood-friends-not-moms-shape-attachment-styles-most-1247316/
203•dnetesn•1w ago•71 comments

Pixar: The Early Days A never-before-seen 1996 interview

https://stevejobsarchive.com/stories/pixar-early-days
122•sanj•14h ago•9 comments

Automating rootless Docker host updates with Ansible

https://du.nkel.dev/blog/2025-11-15_docker-rootless-ansible/
26•Helmut10001•1w ago•0 comments

Concrete Shipbuilding – Argentina

https://thecretefleet.com/blog/f/concrete-shipbuilding-–-argentina
12•surprisetalk•5d ago•3 comments

Shop Sans is a typeface for curved text paths

https://www.futurefonts.com/hex/shop-sans
167•tobr•1w ago•41 comments

Make product worse, get money

https://dynomight.net/worse/
103•zdw•19h ago•105 comments

Solving Fizz Buzz with Cosines

https://susam.net/fizz-buzz-with-cosines.html
161•hprotagonist•17h ago•49 comments

Building a Durable Execution Engine with SQLite

https://www.morling.dev/blog/building-durable-execution-engine-with-sqlite/
140•ingve•1d ago•45 comments

Jack Ma's family shifted wealth to UK after years-long 'disappearance'

https://www.source-material.org/jack-ma-bought-uk-home-after-years-long-disappearance/
4•robtherobber•47m ago•0 comments

XBMC 4.0 for the Original Xbox

https://www.xbox-scene.info/articles/announcing-xbmc-40-for-the-original-xbox-r64/
160•zdw•19h ago•83 comments

Arduino Terms of Service and Privacy Policy update: setting the record straight

https://blog.arduino.cc/2025/11/21/the-arduino-terms-of-service-and-privacy-policy-update-setting...
59•manchoz•13h ago•47 comments

Making a Small RPG

https://jslegenddev.substack.com/p/making-a-small-rpg
195•ibobev•21h ago•35 comments