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.

Nobel Prize in Physics goes to Francis Halzen

https://www.nobelprize.org/prizes/physics/2026/
163•solarist•2h ago•41 comments

Gleam doesn't compile to Erlang source anymore

https://gleam.run/news/gleam-doesnt-compile-to-erlang-source-anymore/
65•ingve•4h ago•11 comments

Beam: Reflection's 501B open-weight model

https://reflection.ai/blog/introducing-beam
478•Philpax•17h ago•154 comments

Find the flattest route between any two points in SF

https://flattensf.com/
228•ishan0102•14h ago•79 comments

Dust: Pretraining Transformers Without Backpropagation

https://qlabs.sh/research/dust
220•E-Reverance•15h ago•56 comments

Friendship ended with Deno, now Node is my best friend

https://dbushell.com/2026/10/03/deno-to-node/
205•ibobev•13h ago•121 comments

Accountability mechanisms can be joyful (2024)

https://liquidbrain.net/blog/accountability-and-joy/
48•luu•2d ago•14 comments

ASOS app users receive push notifications apparently sent by hackers

https://www.bbc.co.uk/news/articles/cj62ylzpr6d3o
35•helsinkiandrew•2h ago•14 comments

Example.com just launched the biggest redesign in decades

https://www.debugbear.com/blog/example-dot-com-redesign-history
253•jgx0•13h ago•159 comments

Web Search API

https://developers.cloudflare.com/changelog/post/2026-10-02-introducing-web-search-api/
562•tosh•1d ago•258 comments

Resurrecting iChat Audio and Video Conferencing

https://blog.pipetogrep.org/2026/09/11/resurrecting-ichat-audio-and-video-conferencing/
66•thepipetogrep•8h ago•14 comments

Opus 5.5 agents discover two room-temperature magnetic semiconductor candidates

https://www.vals.ai/blogs/room-temperature-magnetic-semiconductors
384•outlier99•15h ago•261 comments

Testing 12 different Zigbee temperature/humidity sensors

https://smarthomescene.com/reviews/best-selling-zigbee-temperature-sensors-tested/
139•walrus01•2d ago•75 comments

Direct retinal projection display for smart glasses using a meta-optic mirror

https://www.tdk.com/en/news_center/press/20261002_01.html
45•bookofjoe•2d ago•10 comments

Competitive Programmer's Handbook (2018) [pdf]

https://cses.fi/book/book.pdf
228•vinhnx•2d ago•55 comments

Using A.I. and machine learning to decode communication of sperm whales

https://blue-continuum.com/the-man-who-listens-to-whales
38•dnetesn•2d ago•24 comments

An algorithmic failure beneath the secret ballot

https://blog.citp.princeton.edu/2026/08/03/an-algorithmic-failure-beneath-the-secret-ballot/
92•leecoursey•2d ago•68 comments

The complement of true is true, except when it's false

https://dryperspective.github.io/posts/complement-of-true/
29•aw1621107•1d ago•5 comments

Global Solar Atlas: summary of solar power potential globally

https://globalsolaratlas.info/
67•stratts•13h ago•45 comments

Show HN: Entombed in a Raycaster

https://www.stelabouras.com/blog/entombed-raycaster/
30•stelabouras•3d ago•6 comments

The lamps in my house

https://arslan.io/2026/10/05/the-lamps-in-my-house/
234•farslan•22h ago•102 comments

World's First enhanced geothermal power plant completed in just 23 months

https://techcrunch.com/2026/10/01/worlds-first-enhanced-geothermal-power-plant-completed-in-just-...
5•hochmartinez•42m ago•1 comments

Why Common Lisp is now the best programming language

https://www.vivienhenz.com/common-lisp
217•misterchocolat•9h ago•271 comments

DEDA – Tracking Dots Extraction, Decoding and Anonymisation Toolkit

https://github.com/dfd-tud/deda
72•greyface-•2d ago•8 comments

Ephemeral Testing

https://lemire.me/blog/2026/10/05/ephemeral-testing/
69•ibobev•13h ago•22 comments

Samon: Designing a Zen Garden Raking Puzzle

https://gwern.net/doc/design/2026-10-03-gwern-samon.html
47•networked•12h ago•23 comments

Apple and a hacker's future

https://stratechery.com/2026/apple-and-a-hackers-future/
273•maguay•1d ago•230 comments

Making a GTK application in Haskell, part 1

https://floreal.tech/blog/2026/making-a-gtk-app-in-haskell-part-1/
164•Vosporos•21h ago•46 comments

Nature's capacity to 'bounce back' when species are lost is vastly overestimated

https://phys.org/news/2026-10-nature-capacity-species-lost-vastly.html
5•pseudolus•1h ago•0 comments

Qualcomm licenses patents on Huawei’s LogicFolding chip tech

https://www.bloomberg.com/news/articles/2026-10-05/qualcomm-licenses-patents-on-huawei-s-logicfol...
198•0xedb•1d ago•126 comments