Live data from Hacker News

0x5f3759df and the fast inverse square root (2012)

h14s.p5r.org

11–20 of 27 posts

Re: 0x5f3759df and the fast inverse square root (2012)

#11
post #8
post #2

I may be going on rampage here but " It does contain a fair bit of math " really grinds my gears! Of course it does, it is about square roots and inversions. In most of the cases, in order to optimize an algorithm or solve a problem, mathematics will do it for you, and that ranges from simple arithmetic properties to base changing theorems in linear algebra. You just can not tackle such subjects without expecting it…

Seriously, computer science itself is a branch of mathematics. Or is mathematics a branch of computer science...? It's not always weird bit-level arithmetic, but if you aren't using math at some level, you're not programming; you're just futzing around with a text editor.

Well, at some level almost everything can be described as using math. However, loads of software used by millions and netting millions can be and is written by people who most would describe as barely mathematically literate, from most sorts of business/accounting software (unless arithmetic is math) to device drivers to dating sites (where you could and will use heavyweight machine learning to match people but it kinda seems that having actual people on the site delivers 99.99% of the value regardless of said machine learning.)

There are counter-examples in every area, of course, it's just that the majority of programmers out there aren't great at math and are very much employable. And more often than not, people who're really great at math go to great lengths to avoid programming even when their work revolves around computers.

CS is a branch of math, of course, but this fact happily co-exists with all of the above.

Re: 0x5f3759df and the fast inverse square root (2012)

#13
post #2

I may be going on rampage here but " It does contain a fair bit of math " really grinds my gears! Of course it does, it is about square roots and inversions. In most of the cases, in order to optimize an algorithm or solve a problem, mathematics will do it for you, and that ranges from simple arithmetic properties to base changing theorems in linear algebra. You just can not tackle such subjects without expecting it…

All I do is make buttons blink with jquery and I'm supposedly an engineer.

Re: 0x5f3759df and the fast inverse square root (2012)

#14
Tl;DR :

The fast inverse square root is based on the fact that the integer representation of a floating point number is a rough approximation of its logarithm.

So convert floating point to its integer representation. So now you have its approximate logarithm. Now take half of that and improve that with some Newton raphson.

Re: 0x5f3759df and the fast inverse square root (2012)

#15
I'm really impressed by the generalization to other powers, including the regular square root. It's the first time I've seen that hack (actually even the author mentions he found nothing on google). I think I could definitely have used that when I needed to compute square roots of fixedpoint numbers with no HW support, it looks very significantly faster than the iterative "by digit" method.

Re: 0x5f3759df and the fast inverse square root (2012)

#16

Tl;DR : The fast inverse square root is based on the fact that the integer representation of a floating point number is a rough approximation of its logarithm. So convert floating point to its integer representation. So now you have its approximate logarithm. Now take half of that and improve that with some Newton raphson.

So convert floating point to its integer representation.

Would something dirty like this be feasible in rust?

Re: 0x5f3759df and the fast inverse square root (2012)

#17

If anyone wants a fast atan2 that I wrote a while ago (2007), for making a microcontroller navigate, it's at http://robots-everywhere.com/portfolio/math/

Did you compare its speed to built in atan2 function?

I counted the cycles for you method and it comes to around 50 plus a conditional. Built in function should be around the same.

Re: 0x5f3759df and the fast inverse square root (2012)

#19
post #16

Tl;DR : The fast inverse square root is based on the fact that the integer representation of a floating point number is a rough approximation of its logarithm. So convert floating point to its integer representation. So now you have its approximate logarithm. Now take half of that and improve that with some Newton raphson.

So convert floating point to its integer representation. Would something dirty like this be feasible in rust?

AFAICT transmute would work: http://rustbyexample.com/staging/unsafe.html

That's what's great about Rust, you can write unsafe code when you need it and it's isolated in `unsafe` blocks for easy auditing.

EDIT: Didn't test it too much, but looks like it works.

    fn fast_inv_sqrt(x: f32) -> f32 {
        let f: f32 = unsafe {
            let i: i32 = std::mem::transmute(x);
            std::mem::transmute(0x5f3759df - (i >> 1))
        };
        f * (1.5 - 0.5 * x * f * f)
    }

    println!("{}", fast_inv_sqrt(1.0));
      //=> 0.998307
    println!("{}", fast_inv_sqrt(255.0));
      //=> 0.062517
EDIT2: Shorter, more readable version.

Re: 0x5f3759df and the fast inverse square root (2012)

#20
post #15

I'm really impressed by the generalization to other powers, including the regular square root. It's the first time I've seen that hack (actually even the author mentions he found nothing on google). I think I could definitely have used that when I needed to compute square roots of fixedpoint numbers with no HW support, it looks very significantly faster than the iterative "by digit" method.

[deleted]
Post reply on HN