本文實例講述了go語言實現sqrt的方法。分享給大家供大家參考。具體實現方法如下:
import "math"
func InvSqrt(x float32) float32 {
var xhalf float32 = 0.5*x // get bits for floating VALUE
i := math.Float32bits(x) // gives initial guess y0
i = 0x5f375a86 - (i>>1) // convert bits BACK to float
x = math.Float32frombits(i) // Newton step, repeating increases accuracy
x = x*(1.5-xhalf*x*x)
x = x*(1.5-xhalf*x*x)
x = x*(1.5-xhalf*x*x)
return 1/x
}
希望本文所述對大家的Go語言程序設計有所幫助。