50.go 270 B

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