Exemple #1
0
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
		}
	}
}
Exemple #2
0
// 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")
		}
	}
	*/
}
Exemple #3
0
// 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")
		}
	}
}
Exemple #4
0
// 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
}
Exemple #5
0
// 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)
}
Exemple #6
0
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)
	}
}
Exemple #7
0
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
}
Exemple #8
0
//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))
}
Exemple #9
0
// 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
}
Exemple #10
0
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)))
}
Exemple #11
0
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
}
Exemple #12
0
// 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
}
Exemple #13
0
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)
}
Exemple #14
0
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])
			}
		}
	}
}
Exemple #15
0
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)
		}
	}
}
Exemple #16
0
// Compute Wilkinson shift from symmetric 2x2 trailing submatrix
// Stable formula from Hogben 2007, 42.3
func wilkinson(tn1, tnn1, tn float64) float64 {
	var d, tsq float64
	d = (tn1 - tn) / 2.0
	tsq = math.Hypot(d, tnn1)
	u := tn - math.Copysign((tnn1/(math.Abs(d)+tsq))*tnn1, d)
	return u
}
Exemple #17
0
// RoundFloat rounds float val to the nearest integer value with float64 format, like MySQL Round function.
// RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html
// so rounding use "round half away from zero".
// e.g, 1.5 -> 2, -1.5 -> -2.
func RoundFloat(f float64) float64 {
	if math.Abs(f) < 0.5 {
		return 0
	}

	return math.Trunc(f + math.Copysign(0.5, f))
}
Exemple #18
0
func builtinMath_round(call FunctionCall) Value {
	number := toFloat(call.Argument(0))
	value := math.Floor(number + 0.5)
	if value == 0 {
		value = math.Copysign(0, number)
	}
	return toValue(value)
}
Exemple #19
0
func TestFloat64(t *testing.T) {
	base := []float64{
		0,
		math.Copysign(0, -1),
		-1,
		1,
		math.NaN(),
		math.Inf(+1),
		math.Inf(-1),
		0.1,
		1.5,
		1.9999999999999998,     // all 1s mantissa
		1.3333333333333333,     // 1.010101010101...
		1.1428571428571428,     // 1.001001001001...
		1.112536929253601e-308, // first normal
		2,
		4,
		8,
		16,
		32,
		64,
		128,
		256,
		3,
		12,
		1234,
		123456,
		-0.1,
		-1.5,
		-1.9999999999999998,
		-1.3333333333333333,
		-1.1428571428571428,
		-2,
		-3,
		1e-200,
		1e-300,
		1e-310,
		5e-324,
		1e-105,
		1e-305,
		1e+200,
		1e+306,
		1e+307,
		1e+308,
	}
	all := make([]float64, 200)
	copy(all, base)
	for i := len(base); i < len(all); i++ {
		all[i] = rand.NormFloat64()
	}

	test(t, "+", add, fop(Fadd64), all)
	test(t, "-", sub, fop(Fsub64), all)
	if GOARCH != "386" { // 386 is not precise!
		test(t, "*", mul, fop(Fmul64), all)
		test(t, "/", div, fop(Fdiv64), all)
	}
}
Exemple #20
0
func TestCopySign(t *testing.T) {
	f := func(x struct{ X, Y float32 }) bool {
		y := Copysign(x.X, x.Y)
		return y == float32(math.Copysign(float64(x.X), float64(x.Y)))
	}
	if err := quick.Check(f, nil); err != nil {
		t.Error(err)
	}
}
Exemple #21
0
func formatFloat(x float64, fmt byte, prec int) string {
	s := strconv.FormatFloat(x, fmt, prec, 64)
	// Protect against negative zero.
	z := strconv.FormatFloat(math.Copysign(0, -1), fmt, prec, 64)
	if s == z {
		return strconv.FormatFloat(0, fmt, prec, 64)
	}
	return s
}
Exemple #22
0
// Tries to parse a Geo degrees value from a string as it was found in some
// EXIF data.
// Supported formats so far:
// - "52,00000,50,00000,34,01180" ==> 52 deg 50'34.0118"
//   Probably due to locale the comma is used as decimal mark as well as the
//   separator of three floats (degrees, minutes, seconds)
//   http://en.wikipedia.org/wiki/Decimal_mark#Hindu.E2.80.93Arabic_numeral_system
// - "52.0,50.0,34.01180" ==> 52deg50'34.0118"
// - "52,50,34.01180"     ==> 52deg50'34.0118"
func parseTagDegreesString(s string) (float64, error) {
	const unparsableErrorFmt = "Unknown coordinate format: %s"
	isSplitRune := func(c rune) bool {
		return c == ',' || c == ';'
	}
	parts := strings.FieldsFunc(s, isSplitRune)
	var degrees, minutes, seconds float64
	var err error
	switch len(parts) {
	case 6:
		degrees, err = strconv.ParseFloat(parts[0]+"."+parts[1], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		minutes, err = strconv.ParseFloat(parts[2]+"."+parts[3], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		minutes = math.Copysign(minutes, degrees)
		seconds, err = strconv.ParseFloat(parts[4]+"."+parts[5], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		seconds = math.Copysign(seconds, degrees)
	case 3:
		degrees, err = strconv.ParseFloat(parts[0], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		minutes, err = strconv.ParseFloat(parts[1], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		minutes = math.Copysign(minutes, degrees)
		seconds, err = strconv.ParseFloat(parts[2], 64)
		if err != nil {
			return 0.0, fmt.Errorf(unparsableErrorFmt, s)
		}
		seconds = math.Copysign(seconds, degrees)
	default:
		return 0.0, fmt.Errorf(unparsableErrorFmt, s)
	}
	return degrees + minutes/60.0 + seconds/3600.0, nil
}
Exemple #23
0
func (ai *FollowAI) Act(db *engine.EntityDB, eid engine.Entity) {
	epos := db.Get(eid, "position").(*base.Position)
	tpos := db.Get(ai.Target, "position").(*base.Position)

	dx, dy := int64(0), int64(0)
	if epos.X > tpos.X+1 || epos.X < tpos.X-1 {
		dx = int64(math.Copysign(1, float64(tpos.X-epos.X)))
	}
	if epos.Y > tpos.Y+1 || epos.Y < tpos.Y-1 {
		dy = int64(math.Copysign(1, float64(tpos.Y-epos.Y)))
	}

	// Since the bat is situated at z-level 2, it will never find
	// a wall with art symbol '#' at one level below, so this move
	// function still allows the bat to fly over stones! ^_^
	// Hence the "+1" for the z; so the bat stays one level above
	//  the player.
	base.HelperMove(db, eid, dx, dy, tpos.Z-epos.Z+1)
}
Exemple #24
0
func (a *Actor) UpdateAI(currScene *Scene) {
	var m Vector2d

	if Distance(a.Pos, currScene.PC.Pos) < 160 {
		a.Chasing = true
		a.MvStack = Vector2d{
			(currScene.PC.Pos.X - a.Pos.X) / a.Speed.X,
			(currScene.PC.Pos.Y - a.Pos.Y) / a.Speed.Y,
		}
	} else {
		a.Chasing = false
	}

	if a.MvStack.X > 0 {
		m = MVSpeed.Right
		a.MvStack.X -= 1
	}

	if a.MvStack.X < 0 {
		m = MVSpeed.Left
		a.MvStack.X += 1
	}

	if a.MvStack.Y > 0 {
		m = MVSpeed.Down
		a.MvStack.Y -= 1
	}

	if a.MvStack.Y < 0 {
		m = MVSpeed.Up
		a.MvStack.Y += 1
	}

	a.Move(m, currScene)

	// logic for npc wandering
	if !a.Chasing && a.MvStack.X == 0 && a.MvStack.Y == 0 {
		a.MvStack = Vector2d{
			rand.Int31n(3*tileSize) * int32(math.Copysign(1.0, float64(rand.Int31n(1))-1.0)),
			rand.Int31n(3*tileSize) * int32(math.Copysign(1.0, float64(rand.Int31n(1))-1.0)),
		}
	}
}
Exemple #25
0
func (t Tournament) Ranks() []float64 {
	m := t.Matrix()
	eig := mat64.Eigen(m, 1e-10)
	_, c := eig.V.Dims()
	var ranks []float64
	for i := 0; i < c; i++ {
		ranks = eig.V.Col(nil, i)
		sense := math.Copysign(1, ranks[0])
		for _, val := range ranks {
			if sense == 0 {
				sense = math.Copysign(1, val)
			}
			if val*sense < 0 {
				ranks = nil
				break
			}
		}

		if ranks != nil {
			min := 1e100
			max := 0.0
			for i := range ranks {
				col := t.Matrix().Col(nil, i)
				row := t.Matrix().Row(nil, i)
				tot := 0.0
				for j := range col {
					tot += col[j] + row[j]
				}
				ranks[i] = ranks[i] * sense / tot // normalize to # games
				min = math.Min(ranks[i], min)
				max = math.Max(ranks[i], max)
			}

			// uncomment this to make min score = 0 and max score = 1
			//for i := range ranks {
			//	ranks[i] = (ranks[i] - min) / (max - min)
			//}
			break
		}
	}

	return ranks
}
Exemple #26
0
func (h histogrammer) gv(v float64) string {
	if math.IsNaN(v) || math.IsInf(v, 0) {
		return fmt.Sprint(v)
	}
	c := "+"
	if v < 0 {
		c = "-"
	}
	v = math.Abs(math.Trunc(v + math.Copysign(0.5, v))) // Abs(Round(v))
	return strings.Repeat(c, int(v))
}
Exemple #27
0
func TestSignedZeroAndInfinity(t *testing.T) {
	tests := []struct {
		f    ExactFloat
		want float64
	}{
		{SignedZero(1), math.Copysign(0, 1)},
		{SignedZero(-1), math.Copysign(0, -1)},
		{Infinity(1), math.Inf(1)},
		{Infinity(-1), math.Inf(-1)},
	}
	for _, test := range tests {
		var wantsign int
		if math.Signbit(test.want) {
			wantsign = -1
		} else {
			wantsign = 1
		}
		if test.f.sign != wantsign {
			t.Errorf("sign %v: got %d, want %d", test.f, test.f.sign, wantsign)
		}
	}
}
Exemple #28
0
// Dlarfg generates an elementary reflector for a Householder matrix. It creates
// a real elementary reflector of order n such that
//  H * (alpha) = (beta)
//      (    x)   (   0)
//  H^T * H = I
// H is represented in the form
//  H = 1 - tau * (1; v) * (1 v^T)
// where tau is a real scalar.
//
// On entry, x contains the vector x, on exit it contains v.
func (impl Implementation) Dlarfg(n int, alpha float64, x []float64, incX int) (beta, tau float64) {
	if n < 0 {
		panic(nLT0)
	}
	if n <= 1 {
		return alpha, 0
	}
	checkVector(n-1, x, incX)
	bi := blas64.Implementation()
	xnorm := bi.Dnrm2(n-1, x, incX)
	if xnorm == 0 {
		return alpha, 0
	}
	beta = -math.Copysign(impl.Dlapy2(alpha, xnorm), alpha)
	safmin := dlamchS / dlamchE
	knt := 0
	if math.Abs(beta) < safmin {
		// xnorm and beta may be innacurate, scale x and recompute.
		rsafmn := 1 / safmin
		for {
			knt++
			bi.Dscal(n-1, rsafmn, x, incX)
			beta *= rsafmn
			alpha *= rsafmn
			if math.Abs(beta) >= safmin {
				break
			}
		}
		xnorm = bi.Dnrm2(n-1, x, incX)
		beta = -math.Copysign(impl.Dlapy2(alpha, xnorm), alpha)
	}
	tau = (beta - alpha) / beta
	bi.Dscal(n-1, 1/(alpha-beta), x, incX)
	for j := 0; j < knt; j++ {
		beta *= safmin
	}
	return beta, tau
}
Exemple #29
0
func TestToDouble(t *testing.T) {
	tests := []struct {
		f    ExactFloat
		want float64
	}{
		{NewExactFloat(0.0), 0.0},
		{NewExactFloat(math.Copysign(0, -1)), math.Copysign(0, -1)},
		{NewExactFloat(1.0), 1.0},
		{NewExactFloat(-1.0), -1.0},
		{NewExactFloat(2.5), 2.5},
		{NewExactFloat(-2.5), -2.5},
		{NewExactFloat(math.SmallestNonzeroFloat64), math.SmallestNonzeroFloat64},
		{NewExactFloat(math.MaxFloat64), math.MaxFloat64},
		{NewExactFloat(12345.6789), 12345.6789},
		{NewExactFloat(-12345.6789), -12345.6789},
	}
	for _, test := range tests {
		got := test.f.ToDouble()
		if got != test.want {
			t.Errorf("%v.ToDouble() = %v, want %v", test.f, got, test.want)
		}
	}
}
Exemple #30
0
func limitedNewRadius(lastRadius, newRadius, length float64) float64 {
	// with undefined values, anything new passes
	if math.IsNaN(lastRadius + length) {
		return newRadius
	}

	radiusSlope := (newRadius - lastRadius) / length
	if math.Abs(radiusSlope) > maxRadiusSlope {
		return lastRadius + math.Copysign(maxRadiusSlope*length, radiusSlope)
		//newRadius += math.Copysign(maxDeltaRadius, deltaRadius) - deltaRadius
		//fmt.Println("would have had", nextRadius, "and now have", newRadius)
	}
	return newRadius
}