go语言实现sqrt的方法

时间:2022-05-05
本文章向大家介绍go语言实现sqrt的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package mymath
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
}