Last active
August 24, 2023 12:09
-
-
Save alkstsgv/79862c4e3eb40baa728b91678891a732 to your computer and use it in GitHub Desktop.
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" | |
) | |
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