Created
February 4, 2022 11:21
-
-
Save ilyas-shah/45f81964b480f92ed7ac6e708676c7b7 to your computer and use it in GitHub Desktop.
Tour of GO Error exercise
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" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprint("Can't Sqrt negative number: ", float64(e)) | |
} | |
func Sqrt(x float64) (float64, error) { | |
z := 1.0 | |
if x < 0 { | |
return z, ErrNegativeSqrt(x) | |
} | |
for i := 0; i < 10; i++ { | |
z -= (z*z - x) / (2 * z) | |
} | |
return z, nil | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-3)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment