Re:Fast Inverse Square Root
by timhh on Sunday November 01, 2020 @08:04AM
(#24959202)
This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.
[ Parent
| on HN ]
Thanks for this. I have one nit. You show `q = 1598029824 - u/2;` as being identical to `q = 0x5F400000 - u >> 1;`, but every language I know of uses a different order of operations, giving different results. It might be clearest to provide parentheses in the second case.
Interestingly enough it seems to do the shift first in Swift
All well-known programming languages will do the shift first.
Re:Fast Inverse Square Root
by thehobgoblin on Monday November 02, 2020 @10:24PM
(#24976003)
I looked at about 20 different "popular" languages and I believe that only 2 (Go and Swift) of them have shifts at a higher precedence than addition.
I think I'm missing a reference/joke =/
Or this might just be another confident but baseless HN commenter assertion.
Re:Fast Inverse Square Root
by copascetic on Tuesday November 03, 2020 @01:13AM
(#24976871)
C, C++, Java, and Rust all have shift as lower precedence than addition/subtraction. Edit: and Javascript, Python.
You can add C# as well.
Re:Fast Inverse Square Root
by beached_whale on Monday November 02, 2020 @10:04PM
(#24975923)
Use brackets to be explicit about your expected order of operations.
Oops! I added brackets, thanks!
You mention the wiki page is badly written, and I have the same feeling whenever I read about anything mathematics related on it. But... it's written by volunteers, so I'll take what I can get. Since you obviously have the talent for explaining things, do you think the wiki page could be edited and improved for clarity?
I have thought about it, but I always worry about making large edits to Wikipedia pages that I'll put a lot of work into it and then someone will come along and just revert it. Maybe that fear is unfounded - I'll put it on my (very long) todo list!
Very nice piece! Also, minuend and subtrahend are the standard terms.
This is a great post! The setup could be clearer: I found it a little difficult to follow (during “a real number x … this value which I will denote f … treat the 31-bit value as an unsigned integer u”) what each of the named variables was intended to refer to, and formatting the 31-bit number as 32 bits threw me off too. Nothing insurmountable but I could feel my brain stumbling before I got to the clever part.
Computers do not have _real_ number systems, only _rational_ number systems and rational approximations of real numbers.
Re:Fast Inverse Square Root
by jackpirate on Tuesday November 03, 2020 @01:47AM
(#24977066)
There are plenty of libraries for computable numbers [1], which sit between the rationals and reals
Re:Fast Inverse Square Root
by lifthrasiir on Tuesday November 03, 2020 @02:06AM
(#24977170)
...which (naturally) can't determine if some number x is equal to y or not; that alone makes using constructive real numbers challenging and the usage is virtually non-existent. I believe the most-known software using them is the Android calculator since 6.0 and Hans Boehm (of the gc fame) documented various issues encountered [1].
Re:Fast Inverse Square Root
by longemen3000 on Monday November 02, 2020 @11:04PM
(#24976176)
Nice, i was looking at this, but for sqrt (basically the same integer trick)