github.com/ActiveState/go@v0.0.0-20170614201249-0b81c023a722/src/math/pow.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  func isOddInt(x float64) bool {
     8  	xi, xf := Modf(x)
     9  	return xf == 0 && int64(xi)&1 == 1
    10  }
    11  
    12  // Special cases taken from FreeBSD's /usr/src/lib/msun/src/e_pow.c
    13  // updated by IEEE Std. 754-2008 "Section 9.2.1 Special values".
    14  
    15  // Pow returns x**y, the base-x exponential of y.
    16  //
    17  // Special cases are (in order):
    18  //	Pow(x, ±0) = 1 for any x
    19  //	Pow(1, y) = 1 for any y
    20  //	Pow(x, 1) = x for any x
    21  //	Pow(NaN, y) = NaN
    22  //	Pow(x, NaN) = NaN
    23  //	Pow(±0, y) = ±Inf for y an odd integer < 0
    24  //	Pow(±0, -Inf) = +Inf
    25  //	Pow(±0, +Inf) = +0
    26  //	Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
    27  //	Pow(±0, y) = ±0 for y an odd integer > 0
    28  //	Pow(±0, y) = +0 for finite y > 0 and not an odd integer
    29  //	Pow(-1, ±Inf) = 1
    30  //	Pow(x, +Inf) = +Inf for |x| > 1
    31  //	Pow(x, -Inf) = +0 for |x| > 1
    32  //	Pow(x, +Inf) = +0 for |x| < 1
    33  //	Pow(x, -Inf) = +Inf for |x| < 1
    34  //	Pow(+Inf, y) = +Inf for y > 0
    35  //	Pow(+Inf, y) = +0 for y < 0
    36  //	Pow(-Inf, y) = Pow(-0, -y)
    37  //	Pow(x, y) = NaN for finite x < 0 and finite non-integer y
    38  func Pow(x, y float64) float64
    39  
    40  func pow(x, y float64) float64 {
    41  	switch {
    42  	case y == 0 || x == 1:
    43  		return 1
    44  	case y == 1:
    45  		return x
    46  	case y == 0.5:
    47  		return Sqrt(x)
    48  	case y == -0.5:
    49  		return 1 / Sqrt(x)
    50  	case IsNaN(x) || IsNaN(y):
    51  		return NaN()
    52  	case x == 0:
    53  		switch {
    54  		case y < 0:
    55  			if isOddInt(y) {
    56  				return Copysign(Inf(1), x)
    57  			}
    58  			return Inf(1)
    59  		case y > 0:
    60  			if isOddInt(y) {
    61  				return x
    62  			}
    63  			return 0
    64  		}
    65  	case IsInf(y, 0):
    66  		switch {
    67  		case x == -1:
    68  			return 1
    69  		case (Abs(x) < 1) == IsInf(y, 1):
    70  			return 0
    71  		default:
    72  			return Inf(1)
    73  		}
    74  	case IsInf(x, 0):
    75  		if IsInf(x, -1) {
    76  			return Pow(1/x, -y) // Pow(-0, -y)
    77  		}
    78  		switch {
    79  		case y < 0:
    80  			return 0
    81  		case y > 0:
    82  			return Inf(1)
    83  		}
    84  	}
    85  
    86  	absy := y
    87  	flip := false
    88  	if absy < 0 {
    89  		absy = -absy
    90  		flip = true
    91  	}
    92  	yi, yf := Modf(absy)
    93  	if yf != 0 && x < 0 {
    94  		return NaN()
    95  	}
    96  	if yi >= 1<<63 {
    97  		return Exp(y * Log(x))
    98  	}
    99  
   100  	// ans = a1 * 2**ae (= 1 for now).
   101  	a1 := 1.0
   102  	ae := 0
   103  
   104  	// ans *= x**yf
   105  	if yf != 0 {
   106  		if yf > 0.5 {
   107  			yf--
   108  			yi++
   109  		}
   110  		a1 = Exp(yf * Log(x))
   111  	}
   112  
   113  	// ans *= x**yi
   114  	// by multiplying in successive squarings
   115  	// of x according to bits of yi.
   116  	// accumulate powers of two into exp.
   117  	x1, xe := Frexp(x)
   118  	for i := int64(yi); i != 0; i >>= 1 {
   119  		if i&1 == 1 {
   120  			a1 *= x1
   121  			ae += xe
   122  		}
   123  		x1 *= x1
   124  		xe <<= 1
   125  		if x1 < .5 {
   126  			x1 += x1
   127  			xe--
   128  		}
   129  	}
   130  
   131  	// ans = a1*2**ae
   132  	// if flip { ans = 1 / ans }
   133  	// but in the opposite order
   134  	if flip {
   135  		a1 = 1 / a1
   136  		ae = -ae
   137  	}
   138  	return Ldexp(a1, ae)
   139  }