数的三次方根-golang
blablabla
数的三次方根-golang
package main
import (
"fmt"
"math"
)
func search(x float64) float64 {
const eps = 1e-8
sign := 1
// 负数
if x < 0 {
x = math.Abs(x)
sign = -1
}
l, r := 0.0, math.Max(1.0, x)
for math.Abs(l - r) > eps {
mid := (l + r) / 2
if (math.Pow(mid, 3) - x) > eps {
r = mid
continue
}
l = mid
}
return float64(sign) * l
}
func main() {
var num float64
fmt.Scanf("%f", &num)
fmt.Printf("%.6f\n", search(num))
}