Created
September 13, 2014 06:27
-
-
Save oldergod/8b17d29a04e7498b0be5 to your computer and use it in GitHub Desktop.
Tour of Go 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
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| type ErrNegativeSqrt float64 | |
| func (e ErrNegativeSqrt) Error() string { | |
| return fmt.Sprintf("WHAT THE FUCK IS THAT: %.0f", float64(e)) | |
| } | |
| func Sqrt(f float64, z float64) (float64, error) { | |
| if f < 0 { | |
| return math.NaN(), ErrNegativeSqrt(f) | |
| } | |
| for i := 0; i < 10; i++ { | |
| z = z - (z*z-f)/(z*z) | |
| } | |
| return z, nil | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2, 1)) | |
| fmt.Println(Sqrt(-2, 1)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment