Created
January 1, 2021 00:46
-
-
Save shawnsmithdev/2c3e15512bc43287e8cc2632339f2c87 to your computer and use it in GitHub Desktop.
Go port of Quake III Fast Approx Inverse Square Root algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the Quake III Q_rsqrt Fast Approx Inverse Square Root algorithm | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
const threehalfs float32 = 1.5 | |
func qrsqrt(number float32) float32 { | |
i := math.Float32bits(number) | |
i = 0x5f3759df - (i >> 1) | |
y := math.Float32frombits(i) | |
x2 := number * 0.5 | |
return y * (threehalfs - (x2 * y * y)) | |
} | |
func main() { | |
fmt.Println(qrsqrt(27)) // prints 0.19215362 for 0.154% error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment