Esempio n. 1
0
File: float.go Progetto: gmwu/go
// SetFloat64 sets z to the (possibly rounded) value of x and returns z.
// If z's precision is 0, it is changed to 53 (and rounding will have
// no effect). SetFloat64 panics with ErrNaN if x is a NaN.
func (z *Float) SetFloat64(x float64) *Float {
	if z.prec == 0 {
		z.prec = 53
	}
	if math.IsNaN(x) {
		panic(ErrNaN{"Float.SetFloat64(NaN)"})
	}
	z.acc = Exact
	z.neg = math.Signbit(x) // handle -0, -Inf correctly
	if x == 0 {
		z.form = zero
		return z
	}
	if math.IsInf(x, 0) {
		z.form = inf
		return z
	}
	// normalized x != 0
	z.form = finite
	fmant, exp := math.Frexp(x) // get normalized mantissa
	z.mant = z.mant.setUint64(1<<63 | math.Float64bits(fmant)<<11)
	z.exp = int32(exp) // always fits
	if z.prec < 53 {
		z.round(0)
	}
	return z
}
Esempio n. 2
0
// Return the maximum level such that the length is at least the given value,
// or zero if there is no such level. The return value is always a valid level.
func (m Metric) MaxLevel(value float64) int {
	if value <= 0 {
		return maxLevel
	}
	_, level := math.Frexp(m.Deriv / value)
	level = max(0, min(maxLevel, (level-1)>>uint(m.dim-1)))
	return level
}
Esempio n. 3
0
// Assign sets f to the value of x.
func (f *extFloat) Assign(x float64) {
	if x < 0 {
		x = -x
		f.neg = true
	}
	x, f.exp = math.Frexp(x)
	f.mant = uint64(x * float64(1<<64))
	f.exp -= 64
}
Esempio n. 4
0
// Return the minimum level such that the length is at most the given value,
// or maxLevel if there is no such level. The return value is always a valid
// level.
func (m Metric) MinLevel(value float64) int {
	if value <= 0 {
		return maxLevel
	}
	// This code is equivalent to computing a floating-point "level"
	// value and rounding up. Frexp() returns a fraction in the range
	// [0.5, 1) and the corresponding exponent.
	_, level := math.Frexp(value / m.Deriv)
	level = max(0, min(maxLevel, -((level-1)>>uint(m.dim-1))))
	return level
}
Esempio n. 5
0
// give the binary representation of integer s
func toBinary(s int) []int {
	_, t := math.Frexp(float64(s))
	var (
		bit = s
		bin = make([]int, t)
	)
	for k := t - 1; k >= 0; k-- {
		bin[k] = bit % 2
		bit /= 2
	}
	return bin
}
Esempio n. 6
0
// Return the smallest number integer k where 2^k is greater than
// or equal to n.
//
// XXX DEPRECATED
func NextExp2(n uint) (k uint) {

	if n == 0 || n == uint(1) {
		k = uint(0)
	} else if n == uint(2) {
		k = uint(1)
	} else {
		f := float64(n)
		frac, exp := math.Frexp(f)
		//we are guaranteed that frac is in [1/2, 1)
		if frac == float64(0.5) {
			k = uint(exp - 1)
		} else {
			k = uint(exp)
		}
	}
	return k
}
Esempio n. 7
0
// Return the smallest number k which is a power of two and greater than
// or equal to n.
//
// XXX DEPRECATED
func NextPow2(n uint) (k uint) {

	if n == 0 {
		k = uint(1)
	} else if n == uint(1) || n == uint(2) {
		k = n
	} else {
		f := float64(n)
		frac, exp := math.Frexp(f)
		//we are guaranteed that frac is in [1/2, 1)
		if frac == float64(0.5) {
			k = n
		} else {
			k = uint(math.Pow(float64(2), float64(exp)))
		}
	}
	return k
}
Esempio n. 8
0
func NewExactFloat(v float64) ExactFloat {
	f := ExactFloat{bn: big.NewInt(0)}
	sb := math.Signbit(v)
	if sb {
		f.sign = -1
	} else {
		f.sign = 1
	}
	if math.IsNaN(v) {
		f.set_nan()
	} else if math.IsInf(v, int(f.sign)) {
		f.set_inf(f.sign)
	} else {
		frac, exp := math.Frexp(math.Abs(v))
		m := uint64(math.Ldexp(frac, doubleMantissaBits))
		f.bn = f.bn.SetUint64(m)
		f.bn_exp = exp - doubleMantissaBits
		f.Canonicalize()
	}
	return f
}
Esempio n. 9
0
func Mpmovecflt(a *Mpflt, c float64) {
	if Mpdebug != 0 /*TypeKind(100016)*/ {
		fmt.Printf("\nconst %g", c)
	}
	Mpmovecfix(&a.Val, 0)
	a.Exp = 0
	var f float64
	var l int
	var i int
	if c == 0 {
		goto out
	}
	if c < 0 {
		a.Val.Neg = 1
		c = -c
	}

	f, i = math.Frexp(c)
	a.Exp = int16(i)

	for i := 0; i < 10; i++ {
		f = f * Mpbase
		l = int(math.Floor(f))
		f = f - float64(l)
		a.Exp -= Mpscale
		a.Val.A[0] = l
		if f == 0 {
			break
		}
		Mpshiftfix(&a.Val, Mpscale)
	}

out:
	mpnorm(a)
	if Mpdebug != 0 /*TypeKind(100016)*/ {
		fmt.Printf(" = %v\n", Fconv(a, 0))
	}
}
Esempio n. 10
0
func NewFreeze(d time.Duration, in Sound) *Freeze {
	f := Dtof(d, in.SampleRate())

	n := f
	if n == 0 || n&(n-1) != 0 {
		_, e := math.Frexp(float64(n))
		n = int(math.Ldexp(1, e))
	}

	frz := &Freeze{mono: newmono(nil), prv: make(Discrete, n)}
	frz.sig = frz.prv[:f]

	inps := GetInputs(in)
	dp := new(Dispatcher)

	// t := time.Now()
	for i := 0; i < n; i += in.BufferLen() {
		dp.Dispatch(1, inps...)
		ringcopy(frz.sig[i:i+in.BufferLen()], in.Samples(), 0)
	}
	// log.Println("freeze took", time.Now().Sub(t))
	return frz
}
Esempio n. 11
0
// Return P(document is in category | document)
func (bc *BayesianClassifier) CategoryProbabilities(text string, categories []string) (map[string]float64, error) {
	tokens, err := bc.tokenizer.Tokenize(text)
	if err != nil {
		return nil, err
	}
	probs, err := bc.filteredProbabilities(tokens, categories)
	if err != nil {
		return nil, err
	}

	// Fisher's method for combining probabilities
	catProb := make(map[string]float64, len(categories))
	for cat, pr := range probs {
		p := 0.5
		n := len(pr)
		if n > 0 {
			p = 1.0
			pExp := 0.0
			for _, tp := range pr {
				p *= tp
				if p < 1e-200 { // prevent underflow
					var e int
					p, e = math.Frexp(p)
					pExp += float64(e)
				}
			}
			// Compute the natural log of the product = sum of the logs:
			// ln(x * 2**i) = ln(x) + i * ln(2).
			p = -2.0 * (math.Log(p) + pExp*math.Ln2)
			p = invChi2(p, 2*n)
		}
		catProb[cat] = p
	}

	return catProb, nil
}
Esempio n. 12
0
func Frexp(f float32) (frac float32, exp int) {
	frac64, exp := math.Frexp(float64(f))
	frac = float32(frac64)
	return
}
Esempio n. 13
0
func ext۰math۰Frexp(fr *Frame, args []Value) Value {
	frac, int := math.Frexp(args[0].(float64))
	return tuple{frac, int}
}
Esempio n. 14
0
func ftoa(f float64, prec int) string {
	fr, exp := math.Frexp(f)
	v := int64(fr * (1 << 53))
	e := exp - 53

	buf := make([]byte, 1000)
	n := copy(buf, strconv.FormatInt(v, 10))
	//n := copy(buf, strconv.Itoa64(v))

	for ; e > 0; e-- {
		δ := 0
		if buf[0] >= '5' {
			δ = 1
		}
		x := byte(0)
		for i := n - 1; i >= 0; i-- {
			x += 2 * (buf[i] - '0')
			x, buf[i+δ] = x/10, x%10+'0'
		}
		if δ == 1 {
			buf[0] = '1'
			n++
		}
	}
	dp := n

	for ; e < 0; e++ {
		if buf[n-1]%2 != 0 {
			buf[n] = '0'
			n++
		}
		δ, x := 0, byte(0)
		if buf[0] < '2' {
			δ, x = 1, buf[0]-'0'
			n--
			dp--
		}
		for i := 0; i < n; i++ {
			x = x*10 + buf[i+δ] - '0'
			buf[i], x = x/2+'0', x%2
		}
	}

	if prec > 0 {
		if n > prec {
			if buf[prec] > '5' || buf[prec] == '5' && (nonzero(buf[prec+1:n]) || buf[prec-1]%2 == 1) {
				i := prec - 1
				for i >= 0 && buf[i] == '9' {
					buf[i] = '0'
					i--
				}
				if i >= 0 {
					buf[i]++
				} else {
					buf[0] = '1'
					dp++
				}
			}
			n = prec
		}
		for n < prec {
			buf[n] = '0'
			n++
		}
	}
	return fmt.Sprintf("%c.%se%+d", buf[0], buf[1:n], dp-1)
}
Esempio n. 15
0
var mathLibrary = []RegistryFunction{
	{"abs", mathUnaryOp(math.Abs)},
	{"acos", mathUnaryOp(math.Acos)},
	{"asin", mathUnaryOp(math.Asin)},
	{"atan2", mathBinaryOp(math.Atan2)},
	{"atan", mathUnaryOp(math.Atan)},
	{"ceil", mathUnaryOp(math.Ceil)},
	{"cosh", mathUnaryOp(math.Cosh)},
	{"cos", mathUnaryOp(math.Cos)},
	{"deg", mathUnaryOp(func(x float64) float64 { return x / radiansPerDegree })},
	{"exp", mathUnaryOp(math.Exp)},
	{"floor", mathUnaryOp(math.Floor)},
	{"fmod", mathBinaryOp(math.Mod)},
	{"frexp", func(l *State) int {
		f, e := math.Frexp(CheckNumber(l, 1))
		l.PushNumber(f)
		l.PushInteger(e)
		return 2
	}},
	{"ldexp", func(l *State) int {
		x, e := CheckNumber(l, 1), CheckInteger(l, 2)
		l.PushNumber(math.Ldexp(x, e))
		return 1
	}},
	{"log", func(l *State) int {
		x := CheckNumber(l, 1)
		if l.IsNoneOrNil(2) {
			l.PushNumber(math.Log(x))
		} else if base := CheckNumber(l, 2); base == 10.0 {
			l.PushNumber(math.Log10(x))
Esempio n. 16
0
func mathFrexp(L *LState) int {
	v1, v2 := math.Frexp(float64(L.CheckNumber(1)))
	L.Push(LNumber(v1))
	L.Push(LNumber(v2))
	return 2
}
Esempio n. 17
0
// Frexp breaks f into a normalized fraction
// and an integral power of two.
// It returns frac and exp satisfying f == frac × 2**exp,
// with the absolute value of frac in the interval [½, 1).
//
// Special cases are:
//	Frexp(±0) = ±0, 0
//	Frexp(±Inf) = ±Inf, 0
//	Frexp(NaN) = NaN, 0
func Frexp(f float32) (frac float32, exp int) {
	frac64, e := math.Frexp(float64(f))
	return float32(frac64), e
}