本文整理汇总了Golang中math.Pow函数的典型用法代码示例。如果您正苦于以下问题:Golang Pow函数的具体用法?Golang Pow怎么用?Golang Pow使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pow函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: EaseInOutElastic
func EaseInOutElastic(start, end, value float32) float32 {
end -= start
var d float32 = 1.0
var p float32 = d * 0.3
var s float32 = 0.0
var a float32 = 0.0
if value == 0 {
return start
}
value /= d
if value == 1 {
return start + end
}
if a == 0 || a < float32(math.Abs(float64(end))) {
a = end
s = p / 4
} else {
s = p / (2 * float32(math.Pi)) * float32(math.Asin(float64(end/a)))
}
if value < 1 {
return -0.5*(a*float32(math.Pow(2, 10*(float64(value)-1)))*float32(math.Sin(float64((value*d-s)*(2*math.Pi)/p)))) + start
}
return a*float32(math.Pow(2, -10*(float64(value)-1)))*float32(math.Sin(float64((value*d-s)*(2*math.Pi)/p)))*0.5 + end + start
}
示例2: f
func f(x, y float64) float64 {
v := math.Pow(2.0, math.Sin(y)) * math.Pow(2.0, math.Sin(x)) / 12
if math.IsNaN(v) {
return 0
}
return v
}
示例3: LPNorm
// Lp Norm of an array, given p >= 1
func LPNorm(vector []float64, p float64) (float64, error) {
distance := 0.
for _, jj := range vector {
distance += math.Pow(math.Abs(jj), p)
}
return math.Pow(distance, 1/p), nil
}
示例4: SimPearson
func (r *Richard) SimPearson(v1, v2 InnerStruct) float64 {
common := r.CommonKeys(v1, v2)
n := float64(len(common))
if n == 0 {
return 0
}
var sum1, sum2, sumsq1, sumsq2, sump float64
for key, _ := range common {
sum1 += v1[key]
sumsq1 += math.Pow(v1[key], 2)
sum2 += v2[key]
sumsq2 += math.Pow(v2[key], 2)
sump += v1[key] * v2[key]
}
num := sump - ((sum1 * sum2) / n)
den := math.Sqrt((sumsq1 - (math.Pow(sum1, 2))/n) * (sumsq2 - (math.Pow(sum2, 2))/n))
if den == 0 {
return 0
}
return num / den
}
示例5: p205
func p205() {
// p can score from 9 to 36
pScoresSum := make(map[int]int)
throw(4, 9, 0, pScoresSum)
pScoresProb := make(map[int]float64)
pTot := math.Pow(4.0, 9.0)
for id, cnt := range pScoresSum {
pScoresProb[id] = float64(cnt) / pTot
}
cScoresSum := make(map[int]int)
throw(6, 6, 0, cScoresSum)
cTot := math.Pow(6.0, 6.0)
cScoresProb := make(map[int]float64)
for id, cnt := range cScoresSum {
cScoresProb[id] = float64(cnt) / cTot
}
// P(p wins)
// = P( p has x ) * P( c has less than x)
// = P(p has x) * (P(c has x-1) + P(c has x-2) + ... + P(c has 1))
pWinProb := 0.0
for pScore := 9; pScore <= 36; pScore++ {
probCLower := 0.0
for cScore := 6; cScore < pScore; cScore++ {
probCLower += cScoresProb[cScore]
}
pWinProb += pScoresProb[pScore] * probCLower
}
//bruteForce()
fmt.Printf("Prob P Wins %.7f\n", pWinProb)
}
示例6: main
func main() {
var list list.List
scanner := bufio.NewScanner(os.Stdin)
p := getNumber(scanner)
q := getNumber(scanner)
for i := p; i <= q; i++ {
square := i * i
squareStr := strconv.Itoa(square)
mid := len(squareStr) / 2
power := float64(len(squareStr) - mid)
rem := square % int(math.Pow(10, power))
quo := square / int(math.Pow(10, power))
sum := rem + quo
if sum == i {
list.PushBack(i)
}
}
if list.Len() <= 0 {
fmt.Println("INVALID RANGE")
} else {
for e := list.Front(); e != nil; e = e.Next() {
fmt.Printf("%d ", e.Value)
}
}
}
示例7: Distance
// Distance computes the L-norm of s - t. See Norm for special cases.
// A panic will occur if the lengths of s and t do not match.
func Distance(s, t []float64, L float64) float64 {
if len(s) != len(t) {
panic("floats: slice lengths do not match")
}
if len(s) == 0 {
return 0
}
var norm float64
if L == 2 {
for i, v := range s {
diff := t[i] - v
norm = math.Hypot(norm, diff)
}
return norm
}
if L == 1 {
for i, v := range s {
norm += math.Abs(t[i] - v)
}
return norm
}
if math.IsInf(L, 1) {
for i, v := range s {
absDiff := math.Abs(t[i] - v)
if absDiff > norm {
norm = absDiff
}
}
return norm
}
for i, v := range s {
norm += math.Pow(math.Abs(t[i]-v), L)
}
return math.Pow(norm, 1/L)
}
示例8: PixelDistance
func PixelDistance(image image.Image, x, y int, r, g, b, a uint32) float64 {
if x < image.Bounds().Min.X ||
y < image.Bounds().Min.Y ||
x > image.Bounds().Max.X ||
y > image.Bounds().Max.Y {
log.Printf("Invalid pixel at %d, %d", x, y)
return 0.0
}
targetR, targetG, targetB, targetA := image.At(x, y).RGBA()
distance := 0.0
distance += math.Pow(float64(r-targetR), 2)
if math.IsNaN(distance) {
log.Printf("Distance is NaN after red at %d, %d", x, y)
}
distance += math.Pow(float64(g-targetG), 2)
if math.IsNaN(distance) {
log.Printf("Distance is NaN after green at %d, %d", x, y)
}
distance += math.Pow(float64(b-targetB), 2)
if math.IsNaN(distance) {
log.Printf("Distance is NaN after blue at %d, %d", x, y)
}
distance += math.Pow(float64(a-targetA), 2)
if math.IsNaN(distance) {
log.Printf("Distance is NaN after alpha at %d, %d", x, y)
}
distance = math.Sqrt(distance)
if math.IsNaN(distance) {
log.Printf("Distance is NaN after sqrt at %d, %d", x, y)
}
return distance
}
示例9: ScoreFit
// ScoreFit is used to score the fit based on the Google work published here:
// http://www.columbia.edu/~cs2035/courses/ieor4405.S13/datacenter_scheduling.ppt
// This is equivalent to their BestFit v3
func ScoreFit(node *Node, util *Resources) float64 {
// Determine the node availability
nodeCpu := float64(node.Resources.CPU)
if node.Reserved != nil {
nodeCpu -= float64(node.Reserved.CPU)
}
nodeMem := float64(node.Resources.MemoryMB)
if node.Reserved != nil {
nodeMem -= float64(node.Reserved.MemoryMB)
}
// Compute the free percentage
freePctCpu := 1 - (float64(util.CPU) / nodeCpu)
freePctRam := 1 - (float64(util.MemoryMB) / nodeMem)
// Total will be "maximized" the smaller the value is.
// At 100% utilization, the total is 2, while at 0% util it is 20.
total := math.Pow(10, freePctCpu) + math.Pow(10, freePctRam)
// Invert so that the "maximized" total represents a high-value
// score. Because the floor is 20, we simply use that as an anchor.
// This means at a perfect fit, we return 18 as the score.
score := 20.0 - total
// Bound the score, just in case
// If the score is over 18, that means we've overfit the node.
if score > 18.0 {
score = 18.0
} else if score < 0 {
score = 0
}
return score
}
示例10: main
func main() {
i := 1
for i <= 10 {
fmt.Println(i, math.Pow(2, float64(i)), math.Pow(2, 1/float64(i)))
i = i + 1
}
// A more standard initial condition, iterate to final condition loop
for j := 3; j <= 9; j++ {
fmt.Println(j * j)
}
k := 1
for {
if k > 8 {
fmt.Println("k equals", k, "and the loop stops NOW!")
break
} else if k == 5 {
fmt.Println("k equals", k, "?! That's just swell!")
k++
} else {
fmt.Println("k equals", k, "and the loop goes around again...")
k++
}
}
}
示例11: ETA
// Estimate runtime for job
func (m *Machine) ETA() time.Duration {
var eta time.Duration
var lx, ly, lz float64
for _, pos := range m.Positions {
feed := pos.State.Feedrate
if feed <= 0 {
// Just to use something...
feed = 300
}
// Convert from minutes to microseconds
feed /= 60000000
switch pos.State.MoveMode {
case MoveModeNone:
continue
case MoveModeRapid:
// This is silly, but it gives something to calculate with
feed *= 8
case MoveModeDwell:
eta += time.Duration(pos.State.DwellTime) * time.Second
continue
}
dx, dy, dz := pos.X-lx, pos.Y-ly, pos.Z-lz
lx, ly, lz = pos.X, pos.Y, pos.Z
dist := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2) + math.Pow(dz, 2))
eta += time.Duration(dist/feed) * time.Microsecond
}
return eta
}
示例12: FindClosestColor
// This method finds the closest color for a given RGB tuple and returns the name of the color in given mode
func FindClosestColor(RequestedColor []int, mode string) string {
MinColors := make(map[int]string)
var ColorMap map[string]string
// css3 gives the shades while css21 gives the primary or base colors
if mode == "css3" {
ColorMap = gwc.CSS3NamesToHex
} else {
ColorMap = gwc.HTML4NamesToHex
}
for name, hexcode := range ColorMap {
rgb_triplet := gwc.HexToRGB(hexcode)
rd := math.Pow(float64(rgb_triplet[0]-RequestedColor[0]), float64(2))
gd := math.Pow(float64(rgb_triplet[1]-RequestedColor[1]), float64(2))
bd := math.Pow(float64(rgb_triplet[2]-RequestedColor[2]), float64(2))
MinColors[int(rd+gd+bd)] = name
}
keys := make([]int, 0, len(MinColors))
for key := range MinColors {
keys = append(keys, key)
}
sort.Ints(keys)
return MinColors[keys[0]]
}
示例13: qk
func qk(k, n, p float64) float64 {
result := 1.0
for i := 1.0; i <= k; i++ {
result *= (1.0 - math.Pow(1.0-math.Pow(2, -i), n)*(1.0-p))
}
return result
}
示例14: TestGoertzel
func TestGoertzel(t *testing.T) {
samplerate := 1024
blocksize := 1024
freq := 128
samples := make([]float64, blocksize)
w := 2 * math.Pi / float64(samplerate)
for i := 0; i < blocksize; i++ {
samples[i] = math.Sin(float64(i) * float64(freq) * w)
}
g := NewGoertzel([]uint64{128, 129}, samplerate, blocksize)
g.Feed(samples)
m := g.Magnitude()
if e := math.Pow(float64(blocksize)/2, 2); !approxEqual(m[0], e, 1e-8) {
t.Errorf("Goertzel magnitude = %f. Want %f", m[0], e)
}
if !approxEqual(float64(m[1]), 0.0, 1e-10) {
t.Errorf("Foertzel magnitude = %f. Want 0.0", m[1])
}
c := g.Complex()
if e, m := math.Sqrt(math.Pow(float64(blocksize)/2, 2)), cmplx.Abs(complex128(c[0])); !approxEqual(m, e, 1e-8) {
t.Errorf("Goertzel magnitude = %f. Want %f", m, e)
}
if e, p := -math.Pi/2, cmplx.Phase(complex128(c[0])); !approxEqual(p, e, 1e-12) {
t.Errorf("Goertzel phase = %f. Want %f", p, e)
}
}
示例15: ExponentialRegression
// ExponentialRegression returns an exponential regression on data series
func ExponentialRegression(s Series) (regressions Series, err error) {
if len(s) == 0 {
return nil, errors.New("Input must not be empty")
}
var sum [6]float64
for i := 0; i < len(s); i++ {
sum[0] += s[i].X
sum[1] += s[i].Y
sum[2] += s[i].X * s[i].X * s[i].Y
sum[3] += s[i].Y * math.Log(s[i].Y)
sum[4] += s[i].X * s[i].Y * math.Log(s[i].Y)
sum[5] += s[i].X * s[i].Y
}
denominator := (sum[1]*sum[2] - sum[5]*sum[5])
a := math.Pow(math.E, (sum[2]*sum[3]-sum[5]*sum[4])/denominator)
b := (sum[1]*sum[4] - sum[5]*sum[3]) / denominator
for j := 0; j < len(s); j++ {
regressions = append(regressions, Coordinate{
X: s[j].X,
Y: a * math.Pow(2.718281828459045, b*s[j].X),
})
}
return regressions, nil
}