本文整理汇总了Golang中math.Ldexp函数的典型用法代码示例。如果您正苦于以下问题:Golang Ldexp函数的具体用法?Golang Ldexp怎么用?Golang Ldexp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Ldexp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: glrFloat64frombits
// Float64frombits returns the floating point number corresponding
// the IEEE 754 binary representation b.
//func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) }
func glrFloat64frombits(b uint64) float64 {
var Zero = 0.0
//var NegZero = -Zero
var NaN = Zero / Zero
s := float64(+1)
if b&(1<<63) != 0 {
s = -1
}
e := (b >> 52) & (1<<11 - 1)
m := b & (1<<52 - 1)
if e == (1<<11)-1 {
if m == 0 {
return s / 0
}
return NaN
}
if e != 0 {
m += 1 << 52
}
if e == 0 {
e = 1
}
return math.Ldexp(float64(m), int(e)-1023-52) * s
}
示例2: glrFloat32frombits
// Float32frombits returns the floating point number corresponding
// to the IEEE 754 binary representation b.
// func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }
func glrFloat32frombits(b uint32) float32 {
var Zero = 0.0
//var NegZero = -Zero
var NaN = Zero / Zero
s := float32(+1)
if b&(1<<31) != 0 {
s = -1
}
e := (b >> 23) & (1<<8 - 1)
m := b & (1<<23 - 1)
if e == (1<<8)-1 {
if m == 0 {
return s / 0 // Inf
}
return float32(NaN)
}
if e != 0 {
m += 1 << 23
}
if e == 0 {
e = 1
}
return float32(math.Ldexp(float64(m), int(e)-127-23)) * s
}
示例3: convertFloat
// convertFloat converts the string to a float64value.
func (s *ss) convertFloat(str string, n int) float64 {
if p := indexRune(str, 'p'); p >= 0 {
// Atof doesn't handle power-of-2 exponents,
// but they're easy to evaluate.
f, err := strconv.ParseFloat(str[:p], n)
if err != nil {
// Put full string into error.
if e, ok := err.(*strconv.NumError); ok {
e.Num = str
}
s.error(err)
}
m, err := strconv.Atoi(str[p+1:])
if err != nil {
// Put full string into error.
if e, ok := err.(*strconv.NumError); ok {
e.Num = str
}
s.error(err)
}
return math.Ldexp(f, m)
}
f, err := strconv.ParseFloat(str, n)
if err != nil {
s.error(err)
}
return f
}
示例4: TestFloat32Distribution
func TestFloat32Distribution(t *testing.T) {
//switch runtime.GOARCH {
//case "cs", "java":
// return
//}
// Generate a distribution of (sign, mantissa, exp) values
// broader than the float32 range, and check Rat.Float32()
// always picks the closest float32 approximation.
var add = []int64{
0,
1,
3,
5,
7,
9,
11,
}
var winc, einc = uint64(1), 1 // soak test (~1.5s on x86-64)
if testing.Short() {
winc, einc = 5, 15 // quick test (~60ms on x86-64)
}
for _, sign := range "+-" {
for _, a := range add {
for wid := uint64(0); wid < 30; wid += winc {
b := 1<<wid + a
if sign == '-' {
b = -b
}
for exp := -150; exp < 150; exp += einc {
num, den := NewInt(b), NewInt(1)
if exp > 0 {
num.Lsh(num, uint(exp))
} else {
den.Lsh(den, uint(-exp))
}
r := new(Rat).SetFrac(num, den)
f, _ := r.Float32()
if !checkIsBestApprox32(t, f, r) {
// Append context information.
t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)",
b, exp, f, f, math.Ldexp(float64(b), exp), r)
}
checkNonLossyRoundtrip32(t, f)
}
}
}
}
}
示例5: 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)
}
示例6: ClipToPaddedFace
// ClipToPaddedFace returns the (u,v) coordinates for the portion of the edge AB that
// intersects the given face, but rather than clipping to the square [-1,1]x[-1,1]
// in (u,v) space, this method clips to [-R,R]x[-R,R] where R=(1+padding).
// Padding must be non-negative.
func ClipToPaddedFace(a, b Point, f int, padding float64) (aUV, bUV r2.Point, intersects bool) {
// Fast path: both endpoints are on the given face.
if face(a.Vector) == f && face(b.Vector) == f {
au, av := validFaceXYZToUV(f, a.Vector)
bu, bv := validFaceXYZToUV(f, b.Vector)
return r2.Point{au, av}, r2.Point{bu, bv}, true
}
// Convert everything into the (u,v,w) coordinates of the given face. Note
// that the cross product *must* be computed in the original (x,y,z)
// coordinate system because PointCross (unlike the mathematical cross
// product) can produce different results in different coordinate systems
// when one argument is a linear multiple of the other, due to the use of
// symbolic perturbations.
normUVW := pointUVW(faceXYZtoUVW(f, a.PointCross(b)))
aUVW := pointUVW(faceXYZtoUVW(f, a))
bUVW := pointUVW(faceXYZtoUVW(f, b))
// Padding is handled by scaling the u- and v-components of the normal.
// Letting R=1+padding, this means that when we compute the dot product of
// the normal with a cube face vertex (such as (-1,-1,1)), we will actually
// compute the dot product with the scaled vertex (-R,-R,1). This allows
// methods such as intersectsFace, exitAxis, etc, to handle padding
// with no further modifications.
scaleUV := 1 + padding
scaledN := pointUVW{r3.Vector{X: scaleUV * normUVW.X, Y: scaleUV * normUVW.Y, Z: normUVW.Z}}
if !scaledN.intersectsFace() {
return aUV, bUV, false
}
// TODO(roberts): This is a workaround for extremely small vectors where some
// loss of precision can occur in Normalize causing underflow. When PointCross
// is updated to work around this, this can be removed.
if math.Max(math.Abs(normUVW.X), math.Max(math.Abs(normUVW.Y), math.Abs(normUVW.Z))) < math.Ldexp(1, -511) {
normUVW = pointUVW{normUVW.Mul(math.Ldexp(1, 563))}
}
normUVW = pointUVW{normUVW.Normalize()}
aTan := pointUVW{normUVW.Cross(aUVW.Vector)}
bTan := pointUVW{bUVW.Cross(normUVW.Vector)}
// As described in clipDestination, if the sum of the scores from clipping the two
// endpoints is 3 or more, then the segment does not intersect this face.
aUV, aScore := clipDestination(bUVW, aUVW, pointUVW{scaledN.Mul(-1)}, bTan, aTan, scaleUV)
bUV, bScore := clipDestination(aUVW, bUVW, scaledN, aTan, bTan, scaleUV)
return aUV, bUV, aScore+bScore < 3
}
示例7: TestFloat64Distribution
func TestFloat64Distribution(t *testing.T) {
// Generate a distribution of (sign, mantissa, exp) values
// broader than the float64 range, and check Rat.Float64()
// always picks the closest float64 approximation.
var add = []int64{
0,
1,
3,
5,
7,
9,
11,
}
var winc, einc = uint64(1), int(1) // soak test (~75s on x86-64)
if testing.Short() {
winc, einc = 10, 500 // quick test (~12ms on x86-64)
}
for _, sign := range "+-" {
for _, a := range add {
for wid := uint64(0); wid < 60; wid += winc {
b := int64(1<<wid + a)
if sign == '-' {
b = -b
}
for exp := -1100; exp < 1100; exp += einc {
num, den := NewInt(b), NewInt(1)
if exp > 0 {
num.Lsh(num, uint(exp))
} else {
den.Lsh(den, uint(-exp))
}
r := new(Rat).SetFrac(num, den)
f, _ := r.Float64()
if !checkIsBestApprox(t, f, r) {
// Append context information.
t.Errorf("(input was mantissa %#x, exp %d; f=%g (%b); f~%g; r=%v)",
b, exp, f, f, math.Ldexp(float64(b), exp), r)
}
checkNonLossyRoundtrip(t, f)
}
}
}
}
}
示例8: NewExactFloat
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
}
示例9: NewFreeze
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
}
示例10:
func ext۰math۰Ldexp(fr *frame, args []value) value {
return math.Ldexp(args[0].(float64), args[1].(int))
}
示例11: quotToFloat
// quotToFloat returns the non-negative IEEE 754 double-precision
// value nearest to the quotient a/b, using round-to-even in halfway
// cases. It does not mutate its arguments.
// Preconditions: b is non-zero; a and b have no common factors.
func quotToFloat(a, b nat) (f float64, exact bool) {
// TODO(adonovan): specialize common degenerate cases: 1.0, integers.
alen := a.bitLen()
if alen == 0 {
return 0, true
}
blen := b.bitLen()
if blen == 0 {
panic("division by zero")
}
// 1. Left-shift A or B such that quotient A/B is in [1<<53, 1<<55).
// (54 bits if A<B when they are left-aligned, 55 bits if A>=B.)
// This is 2 or 3 more than the float64 mantissa field width of 52:
// - the optional extra bit is shifted away in step 3 below.
// - the high-order 1 is omitted in float64 "normal" representation;
// - the low-order 1 will be used during rounding then discarded.
exp := alen - blen
var a2, b2 nat
a2 = a2.set(a)
b2 = b2.set(b)
if shift := 54 - exp; shift > 0 {
a2 = a2.shl(a2, uint(shift))
} else if shift < 0 {
b2 = b2.shl(b2, uint(-shift))
}
// 2. Compute quotient and remainder (q, r). NB: due to the
// extra shift, the low-order bit of q is logically the
// high-order bit of r.
var q nat
q, r := q.div(a2, a2, b2) // (recycle a2)
mantissa := low64(q)
haveRem := len(r) > 0 // mantissa&1 && !haveRem => remainder is exactly half
// 3. If quotient didn't fit in 54 bits, re-do division by b2<<1
// (in effect---we accomplish this incrementally).
if mantissa>>54 == 1 {
if mantissa&1 == 1 {
haveRem = true
}
mantissa >>= 1
exp++
}
if mantissa>>53 != 1 {
panic("expected exactly 54 bits of result")
}
// 4. Rounding.
if -1022-52 <= exp && exp <= -1022 {
// Denormal case; lose 'shift' bits of precision.
shift := uint64(-1022 - (exp - 1)) // [1..53)
lostbits := mantissa & (1<<shift - 1)
haveRem = haveRem || lostbits != 0
mantissa >>= shift
exp = -1023 + 2
}
// Round q using round-half-to-even.
exact = !haveRem
if mantissa&1 != 0 {
exact = false
if haveRem || mantissa&2 != 0 {
if mantissa++; mantissa >= 1<<54 {
// Complete rollover 11...1 => 100...0, so shift is safe
mantissa >>= 1
exp++
}
}
}
mantissa >>= 1 // discard rounding bit. Mantissa now scaled by 2^53.
f = math.Ldexp(float64(mantissa), exp-53)
if math.IsInf(f, 0) {
exact = false
}
return
}
示例12: epsilonsqrt
// epsilonsqrt = 2^-26.
func epsilonsqrt() float64 {
return math.Ldexp(1.0, -26)
}
示例13:
}
}
return false
}
// Difficult boundary cases, derived from tables given in
// Vern Paxson, A Program for Testing IEEE Decimal-Binary Conversion
// ftp://ftp.ee.lbl.gov/testbase-report.ps.Z
//
var ftoaTests = []struct {
N int
F float64
A string
}{
// Table 3: Stress Inputs for Converting 53-bit Binary to Decimal, < 1/2 ULP
{0, math.Ldexp(8511030020275656, -342), "9.e-88"},
{1, math.Ldexp(5201988407066741, -824), "4.6e-233"},
{2, math.Ldexp(6406892948269899, +237), "1.41e+87"},
{3, math.Ldexp(8431154198732492, +72), "3.981e+37"},
{4, math.Ldexp(6475049196144587, +99), "4.1040e+45"},
{5, math.Ldexp(8274307542972842, +726), "2.92084e+234"},
{6, math.Ldexp(5381065484265332, -456), "2.891946e-122"},
{7, math.Ldexp(6761728585499734, -1057), "4.3787718e-303"},
{8, math.Ldexp(7976538478610756, +376), "1.22770163e+129"},
{9, math.Ldexp(5982403858958067, +377), "1.841552452e+129"},
{10, math.Ldexp(5536995190630837, +93), "5.4835744350e+43"},
{11, math.Ldexp(7225450889282194, +710), "3.89190181146e+229"},
{12, math.Ldexp(7225450889282194, +709), "1.945950905732e+229"},
{13, math.Ldexp(8703372741147379, +117), "1.4460958381605e+51"},
{14, math.Ldexp(8944262675275217, -1001), "4.17367747458531e-286"},
{15, math.Ldexp(7459803696087692, -707), "1.107950772878888e-197"},
示例14: randomFloat64
// randomFloat64 returns a uniformly distributed value in the range [0,1).
// Note that the values returned are all multiples of 2**-53, which means that
// not all possible values in this range are returned.
func randomFloat64() float64 {
const randomFloatBits = 53
return math.Ldexp(float64(randomBits(randomFloatBits)), -randomFloatBits)
}
示例15: Value
// Return the length on the unit sphere for cells at the given level.
func (m Metric) Value(level int) float64 {
return math.Ldexp(m.Deriv, -m.dim*level)
}