Last active
January 15, 2017 00:10
-
-
Save amstee/f11b3f6bb45a14e36719409c15f21376 to your computer and use it in GitHub Desktop.
An answer for the exercise errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "fmt" | |
| ) | |
| type ErrNegativeSqrt float64 | |
| func (e ErrNegativeSqrt) Error() string { | |
| return fmt.Sprintf("cannot Sqrt negative number: %d", int(e)) | |
| } | |
| func Sqrt(x float64) (z float64, err error) { | |
| if x < 0 { | |
| err := ErrNegativeSqrt(x) | |
| return 0, err | |
| } | |
| z = 1.0 | |
| for i := 0; i < 20; i++ { | |
| z = z - (z * z - x) / (2 * z) | |
| } | |
| return z, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment