Created
August 28, 2013 21:56
-
-
Save flc/6371841 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Errors
http://tour.golang.org/#56
This file contains 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("cannot Sqrt negative number: %f", e) | |
} | |
const e = 1e-8 // small delta | |
func Sqrt(x float64) (float64, error) { | |
if x < 0 { | |
return 0, ErrNegativeSqrt(x) | |
} | |
z := x // starting point | |
for { | |
new_z := z - ((z*z - x) / (2*z)) | |
if math.Abs(new_z - z) < e { | |
return new_z, nil | |
} | |
z = new_z | |
} | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
return fmt.Sprintf("cannot Sqrt negative number: %g", e)
or
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0.0 {
return x, ErrNegativeSqrt(x)
}
z, zz := 1.0, 0.0
for math.Abs(z-zz) > 1e-8 {
z, zz = z-(z*z-x)/(2*z), z
}
return 0, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
import (
"fmt"
)
type ErrNegativeSqrt struct {
Err float64
}
type PosSqrt struct {
Pos float64
}
func (e ErrNegativeSqrt) Error() string {
if err := Sqrt; err != nil { // вызываем метод Sqrt, проверяем, что он возвращает не nil значение
return fmt.Sprint("cannot Sqrt negative number: ", (float64(e.Err)))
}
return fmt.Sprint("sdf ", (float64(e.Err)))
}
func (i PosSqrt) Error() string {
return fmt.Sprint("converted string ", (float64(i.Pos)))
}
func Sqrt(x float64) error { // x принимает данные во float64 и отдаёт во float64
z := x / 2
if x < 0 {
return ErrNegativeSqrt{
x,
}
} else if x >= 0 {
// нужна для работы в примере ниже
for z < x { // начинаю цикл
z -= ((z*z - x) / (2 * z))
if f := z; ((f * f) - x) <= 0 { // по идее, здесь как раз должны
return PosSqrt{
f,
}
} else if x < (f * f) {
continue
} else if x > (f * f) {
return PosSqrt{
f,
}
}
return PosSqrt{
z,
}
}
}
return PosSqrt{
x,
}
}
func main() {
fmt.Println(Sqrt(0))
fmt.Println(Sqrt(-9))
fmt.Println(Sqrt(9))
fmt.Println(Sqrt(9.6))
fmt.Println(Sqrt(-9.6))
fmt.Println(Sqrt(-0.6))
fmt.Println(Sqrt(2))
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution:
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %.0f", e)
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}