1
0
mirror of https://github.com/golang/go synced 2024-10-05 09:21:22 -06:00
go/src/lib/math/pow.go
Russ Cox bc67ea4f8f adapt to new compiler types
R=r
OCL=18024
CL=18024
2008-10-29 13:09:39 -07:00

67 lines
977 B
Go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
import math "math"
/*
arg1 ^ arg2 (exponentiation)
*/
export func
pow(arg1,arg2 float64) float64
{
var temp float64;
var l int32;
if arg2 < 0 {
return 1/pow(arg1, -arg2);
}
if arg1 <= 0 {
if(arg1 == 0) {
if arg2 <= 0 {
return sys.NaN();
}
return 0;
}
temp = floor(arg2);
if temp != arg2 {
panic(sys.NaN());
}
l = int32(temp);
if l&1 != 0 {
return -pow(-arg1, arg2);
}
return pow(-arg1, arg2);
}
temp = floor(arg2);
if temp != arg2 {
if arg2-temp == .5 {
if temp == 0 {
return sqrt(arg1);
}
return pow(arg1, temp) * sqrt(arg1);
}
return exp(arg2 * log(arg1));
}
l = int32(temp);
temp = 1;
for {
if l&1 != 0 {
temp = temp*arg1;
}
l >>= 1;
if l == 0 {
return temp;
}
arg1 *= arg1;
}
panic("unreachable")
}