The go language implements the methods of sqrt

  • 2020-05-27 05:49:21
  • OfStack

This article illustrates how the go language implements sqrt. Share with you for your reference. The specific implementation method is as follows:

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
}

I hope this article has been helpful to your programming of Go language.


Related articles: