在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Copy your
Create a new type type ErrNegativeSqrt float64 and make it an func (e ErrNegativeSqrt) Error() string method such that Note: a call to Change your package main import ( "fmt" "strconv" ) type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string{ if e < 0 { return "cannot Sqrt negative number:" + strconv.FormatFloat(float64(e),'f',5,64) } return "" } func Sqrt(f float64) (float64, error) { var e error if f < 0 { return 0,ErrNegativeSqrt(f) } var z float64 = 1 for i := 0; i < 10; i++ { z = z - (z*z - f) / (2 * z) } return z,e } func main() { fmt.Println(Sqrt(2)) fmt.Println(Sqrt(-2)) }
|
请发表评论