Skip to content
HomeVideosBooksBlogs
AllTop postsTopics
Podcasts
HistoryListsWatch laterLiked videosFollowing

The videos, books, blogs and podcasts Hacker News keeps recommending, all in one place.

VideosBooksBlogsPodcastsAboutPrivacySettings

Data from Hacker News, HackerTube, HackerBooks, HackerBlogs and Open Library. Credits · Not affiliated with Y Combinator.

HomeVideosBooksBlogsPodcasts
Ctrl K
    H

    Hiroshi Hayashi

    h14s.p5r.org · Personal blog · on HN since 2014

    Hiroshi Hayashi writes technical posts on low-level math and programming, including the fast inverse square root, IEEE 754, parsing, and varints.

    Visit blogRSSHN submissions
    MathSystems & PerformanceLanguages & Compilers
    What HN says

    Commenters point to his 2012 explanation of 0x5f3759df as the best they have seen. One reader also praises his Forth-based parser work.

    Stats for nerds
    20HN people shared it
    16linked in comments
    655story points
    Aug 2025last shared
    2015people per year2025

    Top posts on HN

    1. 1
      Understanding the math behind 0x5f3759df and the fast inverse square root (2012)
      13 people · 454 points · first shared 2014 · 25 comments on HN
    2. 2
      Why I find IEEE 754 frustrating
      1 person · 140 points · first shared 2014 · 58 comments on HN
    3. 3
      0x5f3759df
      2 people · 34 points · first shared 2017 · 1 comments on HN
    4. 4
      Shift/Reduce Expression Parsing
      2 people · 21 points · first shared 2014 · 2 comments on HN
    5. 5
      Bitwise Division
      1 person · 4 points · first shared 2023 · Discussion on HN
    6. 6
      Bigger Better Varints
      1 person · 2 points · first shared 2024 · 1 comments on HN
    7. 7
      Z quads strings
      1 person · first shared 2014
    8. 8
      0x5f3759df appendix
      1 person · first shared 2017
    9. 9
      Punched cards
      1 person · first shared 2018
    10. 10
      Bernoulli
      1 person · first shared 2016

    What HN people wrote when they linked it

    • nwallinAug 2025

      1/sqrt(x) is complicated. Imagine instead of computing 1/sqrt(x), imagine instead that you wanted to compute exp_2(-.5 log_2(x)). Also imagine you have an ultra fast way to compute exp_2 and log_2. If you have an ultra fast way to compute exp_2 and log_2, then exp_2(-.5 log_2(x)) is gonna be fast to compute.

      It turns out you do have an ultra fast way to compute log_2: you bitcast a float to an integer, and then twiddle some bits. The first 8 bits (after the sign bit, which is obviously zero because we're assuming our input is positive) or whatever are the exponent, and the trailing 23 bits are a linear interpolation between 2^n and 2^(n+1) or whatever. exp_2 is the same but in reverse.

      You can simply convert the integer to floating point, multiply by -.5, then convert back to integer. But multiplying -.5 by x can be applied to a floating point operating directly on its bits, but it's more complicated. You'll need to do some arithmetic, and some magic numbers.

      So you're bitcasting to an integer, twiddling some bits, twiddling some bits, twiddling some bits, twiddling some bits, and bitcasting to a float. It turns out that all the bit twiddling simplifies if you do all the legwork, but that's beyond the scope of this post.

      So there you go. You've computed exp_2(-.5 log_2 x). You're done. Now you need to figure out how to apply that knowledge to the inverse square root.

      It just so happens that 1/sqrt(x) and exp(-.5 log x) are the same function. exp(-.5 log x) =…

    • plesnerAug 2016

      > L1. There are gaps out there.

      I had a similar experience. At one point I decided that I wanted to understand, step by step, how Ada Lovelace's Bernoulli program works[1]. There was a few steps that didn't seem right to me but I was sure if there were any bugs in there they would be well-known and mentioned somewhere I could find. This being one of the most iconic and historically significant programs ever written. Indeed I found more than one person claiming that the program was known to be correct.

      It took a long time before I could believe that it wasn't just me not getting it, there were indeed a few things out of place and apparently there was just no mention of it anywhere on the web or in any of the research into the analytical engine I could get my hands on.

      [1]: http://h14s.p5r.org/2012/12/bernoulli.html

    • trealiraMay 2025

      Thanks for sharing that. I've actually been learning some Forth recently and had been wondering how a parser or a compiler might actually look using Forth, and you actually already made one, which is pretty cool. I was wondering this because deterministic parsers are often already stack based (LL and LR parsers can both be implemented as imperative programs that manipulate a stack based on the input tokens, or just recursive descent).

      I still can't really understand everything just from reading the code, but I had assumed it would be some kind of explicitly stack-based state machine precedence parser, like this article describes: https://h14s.p5r.org/2014/10/shiftreduce-expression-parsing-by-douglas-gregor.html

      Kind of looks like you did that in expr.fs? Not completely sure, though.

    • forgotpwtomainMar 2016

      Carmack also started a rocket company at a time when it was much more difficult to do so (there were far fewer government dollars available via contracts), Bezos also has far more capital to put in than Carmack.

      Not undeservingly Carmack is something of a folk hero here (for example: http://h14s.p5r.org/2012/09/0x5f3759df.html) but I don't think being a 10X programmer necessarily translates into great rocket company CEO and in terms of entrepreneurial and organizational skill Bezos excels (at least based on Amazon's success).

    • lemoncucumberAug 2017

      Be sure to read the appendix, which is an easy-to-miss link at the end: http://h14s.p5r.org/2012/09/0x5f3759df-appendix.html

      The appendix was by far the most interesting part to me, after reading it I felt like I really got the intuition behind how the trick works.

    • Greek0Nov 2020

      Here's another article (from 2012) describing the same calculation:

      http://h14s.p5r.org/2012/09/0x5f3759df.html

    Related

    Xena Projectxenaproject.wordpress.com74 people

    Fermat's Last Theorem – how it’s going

    Xena Project
    Personal · last shared 2026
    Jeremy Kunjeremykun.com194 people

    A Programmer's Introduction to Mathematics

    Jeremy Kun
    Personal · last shared 2026
    Niko Matsakissmallcultfollowing.com221 people

    Towards a Rust Foundation

    Niko Matsakis
    Personal · last shared 2026
    Paul Khuongpvk.ca106 people

    SBCL: The Ultimate Assembly Code Breadboard

    Paul Khuong
    Personal · last shared 2026
    Hillel Waynehillelwayne.com294 people

    1/0 = 0

    Hillel Wayne
    Personal · last shared 2026
    James Sinclairjrsinclair.com48 people

    What’s so great about functional programming anyway?

    James Sinclair
    Personal · last shared 2026
    Random ASCIIrandomascii.wordpress.com274 people

    There are only four billion floats, so test them all (2014)

    Random ASCII
    Personal · last shared 2026
    LispCastlispcast.com108 people

    React: Another Level of Indirection

    LispCast
    Personal · last shared 2022