简体   繁体   English

如何在 Go 中取立方根?

[英]How to take a cube root in Go?

I'm writing a cube root function in Google Go using Newton's method.我正在使用牛顿法在 Google Go 中编写立方根函数。 I want to check the results using math/ cmplx.Pow() , but for the life of me, I can't figure out how.我想使用 math/ cmplx.Pow()检查结果,但对于我来说,我无法弄清楚如何。 How do I do this?我该怎么做呢?

Have you tried myCubicRootOfx = Pow(x, 1.0/3) ?您是否尝试过myCubicRootOfx = Pow(x, 1.0/3)

edited : thanks to Jason McCreary comment:编辑:感谢Jason McCreary评论:
We cannot use 1/3 as the 2nd parameter to Pow as this is a integer division and hence doesn't produce the expected 1/3 value.我们不能使用1/3作为Pow的第二个参数,因为这是一个整数除法,因此不会产生预期的 1/3 值。 By using 1.0/3 or 1/3.0 etc. we effectively produce a float with the 0.333333... value.通过使用1.0/31/3.0等,我们有效地生成了一个具有 0.333333... 值的浮点数。

I wrote the cube root function using Newton's method as part of the Go Tour Exercise 47 .作为围棋练习 47的一部分,我使用牛顿法编写了立方根函数。 Perhaps the two functions below ( Cbrt1 and Cbrt ) are helpful.也许下面的两个函数( Cbrt1Cbrt )会有所帮助。

package main

import (
    "fmt"
    "math/cmplx"
)

// Newton's method cube root function that hopes for
//   convergence within 20 iterations
func Cbrt1(x complex128) complex128 {
    var z complex128 = x
    for i:= 0; i < 20; i++ {
        z = z - ((z*z*z - x) / (3.0*z*z))
    }
    return z
}

// Newton's method cube root function that runs until stable
func Cbrt(x complex128) complex128 {
    var z, z0 complex128 = x, x
    for {
        z = z - ((z*z*z - x) / (3.0*z*z))
        if cmplx.Abs(z - z0) < 1e-10 {
            break
        }
        z0 = z
    }
    return z
}

func main() {
    fmt.Println(Cbrt(2.0) , "should match" , cmplx.Pow(2, 1.0/3.0))
}

As you're using Newton's method, I suppose you're starting with a positive real number.当您使用牛顿法时,我想您是从一个正实数开始的。

So you don't need complex numbers.所以你不需要复杂的数字。

You may simply do你可以简单地做

package main

import (
    "fmt"
    "math"
)

func main() {
    x := 100.0
    root := math.Pow(x, 1.0/3.0)
    fmt.Println(root)
}

For example,例如,

package main

import (
    "fmt"
    "math/cmplx"
)

func main() {
    var x complex128
    x = -8
    y := cmplx.Pow(x, 1.0/3.0)
    fmt.Println(y)
    x = -27i
    y = cmplx.Pow(x, 1.0/3.0)
    fmt.Println(y)
    x = -8 - 27i
    y = cmplx.Pow(x, 1.0/3.0)
    fmt.Println(y)
    x = complex(-8, -27)
    y = cmplx.Pow(x, 1.0/3.0)
    fmt.Println(y)
}

Output:输出:

(1+1.732050807568877i)
(2.5980762113533156-1.4999999999999996i)
(2.4767967587776756-1.7667767800295509i)
(2.4767967587776756-1.7667767800295509i)

The Go Programming Language Specification Go 编程语言规范

Package cmplx包 cmplx

Looks like go has changed more recently than some of the other answers, so I figured I would update - they built cuberoot right into math as Cbrt .看起来 go 比其他一些答案最近发生了变化,所以我想我会更新 - 他们将 cuberoot 直接构建到math中作为Cbrt It takes, and returns, a float64 .它接受并返回一个float64 A quick reference is at godoc math | grep Cbrt快速参考在godoc math | grep Cbrt godoc math | grep Cbrt (if you've got godoc installed, which I highly recommend) godoc math | grep Cbrt (如果你安装了godoc ,我强烈推荐)

import "math"

func main() {
    var x float64 = math.Cbrt(8)
    fmt.Println(x) // prints 2
}

try something like this尝试这样的事情

package main

import(
        "fmt"
        "math/cmplx"
    )


func Cbrt(x complex128) complex128 {
    z := complex128(1)


    for i:=0;i<100;i++ {  // OR JUST for{ since you will outrun complex128 in worth case
      last_z := z

      z = z - ((z*z*z - x)/(3 *z*z))    
      if last_z == z{
        return z
      }
    }
    return z
}

func main() {
    fmt.Println("good enough", Cbrt(9))
    fmt.Println("builtin", cmplx.Pow(9, 1.0/3.0))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM