首页 > 其他 > 详细

A Tour of Go Advanced Exercise: Complex cube roots

时间:2014-10-28 08:07:30      阅读:279      评论:0      收藏:0      [点我收藏+]

 

 

Let‘s explore Go‘s built-in support for complex numbers via the complex64 and complex128 types. For cube roots, Newton‘s method amounts to repeating:

bubuko.com,布布扣

Find the cube root of 2, just to make sure the algorithm works. There is a Pow function in the math/cmplx package.

package main

import "fmt"

func Cbrt(x complex128) complex128 {
    z := complex128(1)
    for i := 1;i < 10;i++{
        z = z - (z*z*z -x)/(3*z*z)
    }
    return z
}

func main() {
    fmt.Println(Cbrt(8))
}

 

A Tour of Go Advanced Exercise: Complex cube roots

原文:http://www.cnblogs.com/ghgyj/p/4055707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!