A Tour of Go Advanced Exercise: Complex cube roots

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:

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))
}
时间: 2024-11-05 13:38:34

A Tour of Go Advanced Exercise: Complex cube roots的相关文章

Exercise 1.8 牛顿法求立方根

题目: Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value: (r/(y*y)+2*y)/3. Use this formula to implement a cube-root procedure analogous to the squa

LA 6459 Infinite Go (模拟,搜索)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4470 Go is a proverbial board game originated in China. It has been proved to be the most difficult board game in the world. "The rules

EBS Custom Password Rules

https://blogs.oracle.com/manojmadhusoodanan/entry/custom_password_rules Custom Password Rules By Manoj Madhusoodanan-Oracle on Apr 30, 2012 Every organization has certain policies in the login credentials.Oracle Applications has a flexibility to impl

OPC and .NET

Note: recent OPC standards, including Unified Architecture (UA) and Express Interface (Xi) were designed to natively support .NET. The content of this page largely relates to techniques for using the older COM-based OPC specifications from .NET appli

Root of a polynomial in one variable

Root of a polynomial in one variable DAOYI PENG Linear algebraic equation $ax+b=0,\,a\neq0$. Solution:\begin{equation} x=-b/a. \end{equation} Quadratic equation $ax^2+bx+c=0,\,a\neq0$. Solution: \begin{equation} x_1=\frac{-b+\sqrt{b^2-4ac}}{2a}, \qua

windows下的gsl(科学计算库)配置

一.GSL介绍 GNU科学计算函数库GSL(GNU Scientific Library)是一个强大的C/C++数值计算函数库,它是一个自由软件,是GNU项目软件的一个部分,遵循GPL协议.GSL是一个为C和C++程序员提供的科学数值运算库.该科学计算库异常强大,函数库提供了大量的数值计算程序,如随机函数.特殊函数和拟合函数等等,整个函数库大约有1000多个函数,几乎涵盖了科学计算的各个方面.提供了如下方面的支持: Complex Numbers          Roots of Polyno

理工科应该的知道的C/C++数学计算库(转)

理工科应该的知道的C/C++数学计算库(转) 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分析...这些或多或少与我们常用的软件息息相关,假如有一天你只需要这些大型软件系统的某一个很有限的功能,你是不是也要因此再用一用那动辄几个g的软件呢?其实我觉得如果系统不是很大,不是很复杂,我们个人完全有可能自己去编写代码来实现这些‘’有限的功能‘’.别以为这是件很困难的事情,我总以为大学期间学的c语言是极其有用的,只要你会基本的c语言语法,你就可以的. 下面我来介绍几个非常

一元二次方程求解C++实现

----------------------------------------------------------------------------------------------------------------------------- typedef double Number int CesiumMath::sign(Number value){ if (value > 0) { return 1; } if (value < 0) { return -1; } return

A Tour of Go Exercise: Errors

Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers. Create a new type type ErrNegativeSqrt float64 an