当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > go语言实现sqrt的方法

go语言实现sqrt的方法

2017年12月08日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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
}

希望本文所述对大家的go语言程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网