50.go 257 B

12345678910111213141516171819
  1. package main
  2. func myPow(x float64, n int) float64 {
  3. if n < 0 {
  4. n = -n
  5. x = 1 / x
  6. }
  7. res := 1.0
  8. for base := x; n > 0; base, n = base*base, n>>1 {
  9. if n&1 == 1 {
  10. res *= base
  11. }
  12. }
  13. return res
  14. }
  15. /* func main() {
  16. fmt.Println(myPow(2.0, -10))
  17. } */