本文整理汇总了Golang中math.Copysign函数的典型用法代码示例。如果您正苦于以下问题:Golang Copysign函数的具体用法?Golang Copysign怎么用?Golang Copysign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Copysign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: animate
func (w *fluxWindow) animate() {
target := <-w.target
vel := ZP
for {
next := time.After(time.Second / fps)
r := ZR
Do(w, func() {
Pan(w, Rect(w).Min.Add(vel.Div(fps)))
r = Rect(w)
})
d := target.Sub(r.Center())
d.X = math.Copysign(math.Max(0, math.Abs(d.X)-r.Dx()/3), d.X)
d.Y = math.Copysign(math.Max(0, math.Abs(d.Y)-r.Dy()/3), d.Y)
vel = vel.Add(d).Mul(.8)
if vel.Len() < .1 {
next = nil
}
select {
case <-next:
case target = <-w.target:
case <-w.pause:
target = <-w.target
}
}
}
示例2: TestNegativeZero
// negative zero is a good test because:
// 1) 0 and -0 are equal, yet have distinct representations.
// 2) 0 is represented as all zeros, -0 isn't.
// I'm not sure the language spec actually requires this behavior,
// but it's what the current map implementation does.
func TestNegativeZero(t *testing.T) {
m := make(map[float64]bool, 0)
m[+0.0] = true
m[math.Copysign(0.0, -1.0)] = true // should overwrite +0 entry
if len(m) != 1 {
t.Error("length wrong")
}
/* gccgo fails this test; this is not required by the spec.
for k := range m {
if math.Copysign(1.0, k) > 0 {
t.Error("wrong sign")
}
}
*/
m = make(map[float64]bool, 0)
m[math.Copysign(0.0, -1.0)] = true
m[+0.0] = true // should overwrite -0.0 entry
if len(m) != 1 {
t.Error("length wrong")
}
/* gccgo fails this test; this is not required by the spec.
for k := range m {
if math.Copysign(1.0, k) < 0 {
t.Error("wrong sign")
}
}
*/
}
示例3: TestNegativeZero
// negative zero is a good test because:
// 1) 0 and -0 are equal, yet have distinct representations.
// 2) 0 is represented as all zeros, -0 isn't.
// I'm not sure the language spec actually requires this behavior,
// but it's what the current map implementation does.
func TestNegativeZero(t *testing.T) {
m := make(map[float64]bool, 0)
m[+0.0] = true
m[math.Copysign(0.0, -1.0)] = true // should overwrite +0 entry
if len(m) != 1 {
t.Error("length wrong")
}
for k := range m {
if math.Copysign(1.0, k) > 0 {
t.Error("wrong sign")
}
}
m = make(map[float64]bool, 0)
m[math.Copysign(0.0, -1.0)] = true
m[+0.0] = true // should overwrite -0.0 entry
if len(m) != 1 {
t.Error("length wrong")
}
for k := range m {
if math.Copysign(1.0, k) < 0 {
t.Error("wrong sign")
}
}
}
示例4: Drotg
// DrotG gives plane rotation
//
// _ _ _ _ _ _
// | c s | | a | | r |
// | -s c | * | b | = | 0 |
// _ _ _ _ _ _
//
// r = ±(a^2 + b^2)
// c = a/r, the cosine of the plane rotation
// s = b/r, the sine of the plane rotation
//
// NOTE: Netlib library seems to give a different
// sign for r when a or b is zero than other implementations
// and different than BLAS technical manual
func (Blas) Drotg(a, b float64) (c, s, r, z float64) {
if b == 0 && a == 0 {
return 1, 0, a, 0
}
/*
if a == 0 {
if b > 0 {
return 0, 1, b, 1
}
return 0, -1, -b, 1
}
*/
absA := math.Abs(a)
absB := math.Abs(b)
aGTb := absA > absB
r = math.Hypot(a, b)
if aGTb {
r = math.Copysign(r, a)
} else {
r = math.Copysign(r, b)
}
c = a / r
s = b / r
if aGTb {
z = s
} else if c != 0 { // r == 0 case handled above
z = 1 / c
} else {
z = 1
}
return
}
示例5: esRotateToTarget
// Smoothly rotates the entity from its current rotation to the target
// rotation
func esRotateToTarget(r RotationComponent, t *targetRotationComponent) {
py, pp := r.Yaw(), r.Pitch()
ty, tp := t.TargetYaw(), t.TargetPitch()
if t.pPitch != tp || t.pYaw != ty || t.time >= 4 {
t.sYaw, t.sPitch = py, pp
t.time = 0
t.pPitch = tp
t.pYaw = ty
}
sy, sp := t.sYaw, t.sPitch
dy, dp := ty-sy, tp-sp
// Make sure we go for the shortest route.
// e.g. (in degrees) 1 to 359 is quicker
// to decrease to wrap around than it is
// to increase all the way around
if dy > math.Pi || dy < -math.Pi {
sy += math.Copysign(math.Pi*2, dy)
dy = ty - sy
}
if dp > math.Pi || dp < -math.Pi {
sp += math.Copysign(math.Pi*2, dp)
dp = tp - sp
}
t.time = math.Min(4.0, t.time+Client.delta)
py = sy + dy*(1/4.0)*t.time
pp = sp + dp*(1/4.0)*t.time
r.SetPitch(pp)
r.SetYaw(py)
}
示例6: TestGrowWithNegativeZero
func TestGrowWithNegativeZero(t *testing.T) {
t.Skip("fails with gccgo")
negzero := math.Copysign(0.0, -1.0)
m := make(map[FloatInt]int, 4)
m[FloatInt{0.0, 0}] = 1
m[FloatInt{0.0, 1}] = 2
m[FloatInt{0.0, 2}] = 4
m[FloatInt{0.0, 3}] = 8
growflag := true
s := 0
cnt := 0
negcnt := 0
// The first iteration should return the +0 key.
// The subsequent iterations should return the -0 key.
// I'm not really sure this is required by the spec,
// but it makes sense.
// TODO: are we allowed to get the first entry returned again???
for k, v := range m {
if v == 0 {
continue
} // ignore entries added to grow table
cnt++
if math.Copysign(1.0, k.x) < 0 {
if v&16 == 0 {
t.Error("key/value not updated together 1")
}
negcnt++
s |= v & 15
} else {
if v&16 == 16 {
t.Error("key/value not updated together 2", k, v)
}
s |= v
}
if growflag {
// force a hashtable resize
for i := 0; i < 100; i++ {
m[FloatInt{3.0, i}] = 0
}
// then change all the entries
// to negative zero
m[FloatInt{negzero, 0}] = 1 | 16
m[FloatInt{negzero, 1}] = 2 | 16
m[FloatInt{negzero, 2}] = 4 | 16
m[FloatInt{negzero, 3}] = 8 | 16
growflag = false
}
}
if s != 15 {
t.Error("entry missing", s)
}
if cnt != 4 {
t.Error("wrong number of entries returned by iterator", cnt)
}
if negcnt != 3 {
t.Error("update to negzero missed by iteration", negcnt)
}
}
示例7: same
func same(f, g float64) bool {
if math.IsNaN(f) && math.IsNaN(g) {
return true
}
if math.Copysign(1, f) != math.Copysign(1, g) {
return false
}
return f == g
}
示例8: RoundInt
//RoundInt - rounds integers to a place:
// Example: RoundInt(12345, 3) would return 12000
func RoundInt(num, places int) int {
_num := float64(num)
factor := int(math.Pow(10, float64(places)))
_div := int(math.Abs(_num)) / factor
_mod := int(math.Abs(_num)) % factor
if (_mod) >= (factor / 2) {
return int(math.Copysign(float64(((_div + 1) * factor)), _num))
}
return int(math.Copysign(float64(((_div) * factor)), _num))
}
示例9: Round
// Round rounds a value. RoundOn is usually .5.
func Round(val float64, roundOn float64, places int) float64 {
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
_div := math.Copysign(div, val)
_roundOn := math.Copysign(roundOn, val)
if _div >= _roundOn {
return math.Ceil(digit) / pow
}
return math.Floor(digit) / pow
}
示例10: TestRoundTrips
func TestRoundTrips(t *testing.T) {
assertRoundTrips := func(v Value) {
vs := NewTestValueStore()
out := DecodeValue(EncodeValue(v, vs), vs)
assert.True(t, v.Equals(out))
}
assertRoundTrips(Bool(false))
assertRoundTrips(Bool(true))
assertRoundTrips(Number(0))
assertRoundTrips(Number(-0))
assertRoundTrips(Number(math.Copysign(0, -1)))
intTest := []int64{1, 2, 3, 7, 15, 16, 17,
127, 128, 129,
254, 255, 256, 257,
1023, 1024, 1025,
2048, 4096, 8192, 32767, 32768, 65535, 65536,
4294967295, 4294967296,
9223372036854779,
92233720368547760,
}
for _, v := range intTest {
f := float64(v)
assertRoundTrips(Number(f))
f = math.Copysign(f, -1)
assertRoundTrips(Number(f))
}
floatTest := []float64{1.01, 1.001, 1.0001, 1.00001, 1.000001, 100.01, 1000.000001}
for _, f := range floatTest {
assertRoundTrips(Number(f))
f = math.Copysign(f, -1)
assertRoundTrips(Number(f))
}
assertRoundTrips(Number(math.MaxFloat64))
assertRoundTrips(Number(math.Nextafter(1, 2) - 1))
assertRoundTrips(String(""))
assertRoundTrips(String("foo"))
assertRoundTrips(String("AINT NO THANG"))
assertRoundTrips(String("💩"))
assertRoundTrips(NewStruct("", StructData{"a": Bool(true), "b": String("foo"), "c": Number(2.3)}))
listLeaf := newList(newListLeafSequence(nil, Number(4), Number(5), Number(6), Number(7)))
assertRoundTrips(listLeaf)
assertRoundTrips(newList(newListMetaSequence([]metaTuple{
newMetaTuple(NewRef(listLeaf), orderedKeyFromInt(10), 10, nil),
newMetaTuple(NewRef(listLeaf), orderedKeyFromInt(20), 20, nil),
}, nil)))
}
示例11: Quaternion
func Quaternion(m glm.Mat3d) glm.Quatd {
q := glm.Quatd{}
m00, m01, m02 := m[0], m[1], m[2]
m10, m11, m12 := m[3], m[4], m[5]
m20, m21, m22 := m[6], m[7], m[8]
q.W = math.Sqrt(math.Max(0, 1+m00+m11+m22)) / 2
q.V[0] = math.Copysign(math.Sqrt(math.Max(0, 1+m00-m11-m22))/2, m12-m21)
q.V[1] = math.Copysign(math.Sqrt(math.Max(0, 1-m00+m11-m22))/2, m20-m02)
q.V[2] = math.Copysign(math.Sqrt(math.Max(0, 1-m00-m11+m22))/2, m01-m10)
return q
}
示例12: round
// round is the "missing" round function from the math lib
func round(val float64, roundOn float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
_div := math.Copysign(div, val)
_roundOn := math.Copysign(roundOn, val)
if _div >= _roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}
示例13: ToDoubleHelper
func (f ExactFloat) ToDoubleHelper() float64 {
sign := float64(f.sign)
if !f.is_normal() {
if f.is_zero() {
return math.Copysign(0, sign)
}
if f.is_inf() {
return math.Inf(f.sign)
}
return math.Copysign(math.NaN(), sign)
}
mantissa := f.bn.Uint64()
return sign * math.Ldexp(float64(mantissa), f.bn_exp)
}
示例14: Rprop
func (sh *Shoehorn) Rprop(S [][]float64, G0 [][]float64, G1 [][]float64, step_size_min float64, step_size_max float64) {
var (
o, d int
gprod float64
)
for o = 0; o < sh.no; o++ {
for d = 0; d < sh.nd; d++ {
// Update the step size (consistent gradient directions get a boost, inconsistent directions get reduced).
gprod = G0[o][d] * G1[o][d]
if gprod > 0. {
S[o][d] *= 1.01
} else if gprod < 0. {
S[o][d] *= 0.5
}
// Apply caps.
if S[o][d] < step_size_min {
S[o][d] = step_size_min
}
if S[o][d] > step_size_max {
S[o][d] = step_size_max
}
// Update the position based on the sign of its magnitude and the learned step size (RProp doesn't use gradient magnitudes).
if (gprod > 0.) && (G1[o][d] != 0.) {
sh.L[o][d] -= math.Copysign(S[o][d], G1[o][d])
}
}
}
}
示例15: TestToString
func TestToString(t *testing.T) {
tests := []struct {
a ExactFloat
want string
}{
{NewExactFloat(math.NaN()), "nan"},
{NewExactFloat(math.Inf(1)), "inf"},
{NewExactFloat(math.Inf(-1)), "-inf"},
{NewExactFloat(0), "0"},
{NewExactFloat(math.Copysign(0, -1)), "-0"},
{NewExactFloat(1.0), "1"},
{NewExactFloat(1.5), "1.5"},
{NewExactFloat(1. / 512), "0.001953125"},
{NewExactFloat(1.23456789), "1.2345678899999999"},
{NewExactFloat(math.SmallestNonzeroFloat64), "4.9406564584124654e-324"},
{NewExactFloat(math.MaxFloat64), "1.7976931348623157e+308"},
{NewExactFloat(math.MaxFloat64).Add(NewExactFloat(math.MaxFloat64)), "3.59538626972463142e+308"},
}
for _, test := range tests {
got := test.a.ToString()
if got != test.want {
t.Errorf("%v.ToString() = %s, want %s", test.a, got, test.want)
}
}
}