本文实例讲述了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
}
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语言程序设计有所帮助。
最后
以上就是花痴睫毛膏最近收集整理的关于go语言实现sqrt的方法的全部内容,更多相关go语言实现sqrt内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复