-
-
Save alco/3731366 to your computer and use it in GitHub Desktop.
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
package main | |
import "fmt" | |
import "math" | |
func FastInvSqrt(x float32) float32 { | |
xhalf := float32(0.5) * x | |
i := math.Float32bits(x) | |
i = 0x5f3759df - i>>1 | |
x = math.Float32frombits(i) | |
x = x * (1.5 - (xhalf * x * x)) | |
return x | |
} | |
func main() { | |
fmt.Printf("inv sqrt: %f\n", FastInvSqrt(4)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ha. Turns out the implementations of math.Float32bits and math.Float32frombits use the unsafe package (source). Still, this version is more readable.