Golang Float64bits示例

说明

golang float64bits示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: math

示例#1
文件: encoding.go 项目: yujinqiu/banshee

// encodeValue encodes db value from metric.
func encodeValue(m *models.Metric) []byte {
	b := make([]byte, 8+8+8)
	binary.BigEndian.PutUint64(b[:8], math.Float64bits(m.Value))
	binary.BigEndian.PutUint64(b[8:8+8], math.Float64bits(m.Score))
	binary.BigEndian.PutUint64(b[8+8:], math.Float64bits(m.Average))
	return b
}

示例#2
文件: resultset_helper.go 项目: ZhiephieCook/gh-ost

func formatBinaryValue(value interface{}) ([]byte, error) {
	switch v := value.(type) {
	case int8:
		return Uint64ToBytes(uint64(v)), nil
	case int16:
		return Uint64ToBytes(uint64(v)), nil
	case int32:
		return Uint64ToBytes(uint64(v)), nil
	case int64:
		return Uint64ToBytes(uint64(v)), nil
	case int:
		return Uint64ToBytes(uint64(v)), nil
	case uint8:
		return Uint64ToBytes(uint64(v)), nil
	case uint16:
		return Uint64ToBytes(uint64(v)), nil
	case uint32:
		return Uint64ToBytes(uint64(v)), nil
	case uint64:
		return Uint64ToBytes(uint64(v)), nil
	case uint:
		return Uint64ToBytes(uint64(v)), nil
	case float32:
		return Uint64ToBytes(math.Float64bits(float64(v))), nil
	case float64:
		return Uint64ToBytes(math.Float64bits(v)), nil
	case []byte:
		return v, nil
	case string:
		return hack.Slice(v), nil
	default:
		return nil, errors.Errorf("invalid type %T", value)
	}
}

示例#3
文件: frame.go 项目: glycerine/tmframe

// Marshal serialized the Frame into bytes. We'll
// reuse the space pointed to by buf if there is
// sufficient space in it. We return the bytes
// that we wrote, plus any error.
func (f *Frame) Marshal(buf []byte) ([]byte, error) {
	n := f.NumBytes()

	var m []byte
	if int64(len(buf)) >= n {
		m = buf[:n]
	} else {
		m = make([]byte, n)
	}
	binary.LittleEndian.PutUint64(m[:8], uint64(f.Prim))
	if n == 8 {
		return m, nil
	}
	pti := f.GetPTI()
	switch pti {
	case PtiOneInt64:
		binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
	case PtiOneFloat64:
		binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
	case PtiTwo64:
		binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
		binary.LittleEndian.PutUint64(m[16:24], uint64(f.Ude))
	case PtiUDE:
		binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
		if n == 16 {
			return m, nil
		}
		copy(m[16:], f.Data)
		m[n-1] = 0
	}

	return m, nil
}

示例#4
文件: remixoscope.go 项目: mkb218/remixoscope

func marshal() (outstring []string) {
	// <bandcount>|trackname|beat0band0lbeat0band0r...Beat0bandNr
	// numbers aren't intended to be human readable, but it is easier to emit human readable integers
	out := make([]string, 0)
	out = append(out, fmt.Sprintf("%d", bands))
	for _, track := range sources {
		out = append(out, fmt.Sprintf("|%s|%d|", track.filename, len(track.beats)))
		if len(track.beats) <= 128 {
			for i := uint(0); i < bands; i += 1 {
				for j := uint(0); j < uint(len(track.beats)); j += 1 {
					//					fmt.Fprintf(os.Stderr, "%d %d %f %f\n", i, j, track.beats[j].buckets[i].left, track.beats[j].buckets[i].right)
				}
			}
		}
		for _, beat := range track.beats {
			for _, band := range beat.buckets {
				l := math.Float64bits(band.left)
				r := math.Float64bits(band.right)
				var sb bytes.Buffer
				binary.Write(&sb, binary.BigEndian, l)
				binary.Write(&sb, binary.BigEndian, r)
				out = append(out, sb.String())
			}
		}
	}
	return out
}

示例#5
文件: frame.go 项目: glycerine/tmframe

// Blake2b returns the 64-byte BLAKE2b cryptographic
// hash of the Frame. This is useful for hashing and
// de-duplicating a stream of Frames.
//
// reference: https://godoc.org/github.com/codahale/blake2
// reference: https://blake2.net/
// reference: https://tools.ietf.org/html/rfc7693
//
func (f *Frame) Blake2b() []byte {
	h, err := blake2b.New(nil)
	panicOn(err)

	n := f.NumBytes()

	var m [24]byte
	binary.LittleEndian.PutUint64(m[:8], uint64(f.Prim))
	switch {
	case n == 8:
		h.Write(m[:8])
	default:
		pti := f.GetPTI()
		switch pti {
		case PtiOneInt64:
			binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
			h.Write(m[:16])
		case PtiOneFloat64:
			binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
			h.Write(m[:16])
		case PtiTwo64:
			binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
			binary.LittleEndian.PutUint64(m[16:24], uint64(f.Ude))
			h.Write(m[:24])
		case PtiUDE:
			binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
			h.Write(m[:16])
			h.Write(f.Data)
		}
	}

	return []byte(h.Sum(nil))
}

示例#6
文件: softfloat64_test.go 项目: Xiahl1990/go

// turn uint64 op into float64 op
func fop(f func(x, y uint64) uint64) func(x, y float64) float64 {
	return func(x, y float64) float64 {
		bx := math.Float64bits(x)
		by := math.Float64bits(y)
		return math.Float64frombits(f(bx, by))
	}
}

示例#7
文件: issue14651.go 项目: Xiahl1990/go

func test64(a, b float64) {
	abits := math.Float64bits(a)
	bbits := math.Float64bits(b)
	if abits != bbits {
		panic(fmt.Sprintf("%016x != %016x\n", abits, bbits))
	}
}

示例#8
文件: internal.pb.go 项目: cuongdo/cockroach

func (m *InternalTimeSeriesSample) MarshalTo(data []byte) (int, error) {
	var i int
	_ = i
	var l int
	_ = l
	data[i] = 0x8
	i++
	i = encodeVarintInternal(data, i, uint64(m.Offset))
	data[i] = 0x30
	i++
	i = encodeVarintInternal(data, i, uint64(m.Count))
	data[i] = 0x39
	i++
	i = encodeFixed64Internal(data, i, uint64(math.Float64bits(float64(m.Sum))))
	if m.Max != nil {
		data[i] = 0x41
		i++
		i = encodeFixed64Internal(data, i, uint64(math.Float64bits(float64(*m.Max))))
	}
	if m.Min != nil {
		data[i] = 0x49
		i++
		i = encodeFixed64Internal(data, i, uint64(math.Float64bits(float64(*m.Min))))
	}
	return i, nil
}

示例#9
文件: drawing_paths.go 项目: badgerodon/go

func (self PathData) Interator() NextElement {
	count := 0
	num_data := len(self)

	return func() (PathDataType, []PathDataPoint) {
		if count >= num_data {
			return -1, nil
		}

		length := int(math.Float64bits(self[count]) >> 32)
		path_type := int((math.Float64bits(self[count]) << 32) >> 32)
		count += 2

		var pathPoints []PathDataPoint
		if length >= 2 {
			length--
			pathPoints = make([]PathDataPoint, length)
			for index := range pathPoints {
				pathPoints[index].x = self[count]
				pathPoints[index].y = self[count+1]
			}
			count += (length * 2)
		} else {
			pathPoints = []PathDataPoint{}
		}

		return PathDataType(path_type), pathPoints
	}
}

示例#10
文件: softfloat64_test.go 项目: Xiahl1990/go

func testcmp(t *testing.T, f, g float64) {
	hcmp, hisnan := hwcmp(f, g)
	scmp, sisnan := Fcmp64(math.Float64bits(f), math.Float64bits(g))
	if int32(hcmp) != scmp || hisnan != sisnan {
		err(t, "cmp(%g, %g) = sw %v, %v, hw %v, %v\n", f, g, scmp, sisnan, hcmp, hisnan)
	}
}

示例#11
文件: play_serverbound_proto.go 项目: suedadam/steven

func (p *PlayerPositionLook) write(ww io.Writer) (err error) {
	var tmp [8]byte
	tmp0 := math.Float64bits(p.X)
	tmp[0] = byte(tmp0 >> 56)
	tmp[1] = byte(tmp0 >> 48)
	tmp[2] = byte(tmp0 >> 40)
	tmp[3] = byte(tmp0 >> 32)
	tmp[4] = byte(tmp0 >> 24)
	tmp[5] = byte(tmp0 >> 16)
	tmp[6] = byte(tmp0 >> 8)
	tmp[7] = byte(tmp0 >> 0)
	if _, err = ww.Write(tmp[:8]); err != nil {
		return
	}
	tmp1 := math.Float64bits(p.Y)
	tmp[0] = byte(tmp1 >> 56)
	tmp[1] = byte(tmp1 >> 48)
	tmp[2] = byte(tmp1 >> 40)
	tmp[3] = byte(tmp1 >> 32)
	tmp[4] = byte(tmp1 >> 24)
	tmp[5] = byte(tmp1 >> 16)
	tmp[6] = byte(tmp1 >> 8)
	tmp[7] = byte(tmp1 >> 0)
	if _, err = ww.Write(tmp[:8]); err != nil {
		return
	}
	tmp2 := math.Float64bits(p.Z)
	tmp[0] = byte(tmp2 >> 56)
	tmp[1] = byte(tmp2 >> 48)
	tmp[2] = byte(tmp2 >> 40)
	tmp[3] = byte(tmp2 >> 32)
	tmp[4] = byte(tmp2 >> 24)
	tmp[5] = byte(tmp2 >> 16)
	tmp[6] = byte(tmp2 >> 8)
	tmp[7] = byte(tmp2 >> 0)
	if _, err = ww.Write(tmp[:8]); err != nil {
		return
	}
	tmp3 := math.Float32bits(p.Yaw)
	tmp[0] = byte(tmp3 >> 24)
	tmp[1] = byte(tmp3 >> 16)
	tmp[2] = byte(tmp3 >> 8)
	tmp[3] = byte(tmp3 >> 0)
	if _, err = ww.Write(tmp[:4]); err != nil {
		return
	}
	tmp4 := math.Float32bits(p.Pitch)
	tmp[0] = byte(tmp4 >> 24)
	tmp[1] = byte(tmp4 >> 16)
	tmp[2] = byte(tmp4 >> 8)
	tmp[3] = byte(tmp4 >> 0)
	if _, err = ww.Write(tmp[:4]); err != nil {
		return
	}
	if err = WriteBool(ww, p.OnGround); err != nil {
		return
	}
	return
}

示例#12
文件: write_bytes.go 项目: 0x20h/grafana

// AppendComplex128 appends a complex128 to the slice as a MessagePack extension
func AppendComplex128(b []byte, c complex128) []byte {
	o, n := ensure(b, Complex128Size)
	o[n] = mfixext16
	o[n+1] = Complex128Extension
	big.PutUint64(o[n+2:], math.Float64bits(real(c)))
	big.PutUint64(o[n+10:], math.Float64bits(imag(c)))
	return o
}

示例#13
文件: zset.go 项目: scozss/setdb

func writeByteSortableFloat(b []byte, f float64) {
	if math.Signbit(f) {
		binary.BigEndian.PutUint64(b, math.Float64bits(f))
		for i, v := range b[:8] {
			b[i] = v ^ 255
		}
	} else {
		binary.BigEndian.PutUint64(b, math.Float64bits(-f))
	}
}

示例#14
文件: blasfuzz.go 项目: btracey/blasfuzz

func SameF64Approx(str string, c, native, absTol, relTol float64) {
	if math.IsNaN(c) && math.IsNaN(native) {
		return
	}
	if !floats.EqualWithinAbsOrRel(c, native, absTol, relTol) {
		cb := math.Float64bits(c)
		nb := math.Float64bits(native)
		same := floats.EqualWithinAbsOrRel(c, native, absTol, relTol)
		panic(fmt.Sprintf("Case %s: Float64 mismatch. c = %v, native = %v\n cb: %v, nb: %v\n%v,%v,%v", str, c, native, cb, nb, same, absTol, relTol))
	}
}

示例#15
文件: encoder_unsafe.go 项目: rainycape/gondola

func complex128Encoder(enc *encoder, p unsafe.Pointer) error {
	bs := enc.buf[:8]
	v := (*complex128)(p)
	enc.order.PutUint64(bs, math.Float64bits(real(*v)))
	if _, err := enc.Write(bs); err != nil {
		return err
	}
	enc.order.PutUint64(bs, math.Float64bits(imag(*v)))
	_, err := enc.Write(bs)
	return err
}

示例#16
文件: encoder.go 项目: rainycape/gondola

func complex128Encoder(enc *encoder, v reflect.Value) error {
	bs := enc.buf[:8]
	x := v.Complex()
	enc.order.PutUint64(bs, math.Float64bits(real(x)))
	if _, err := enc.Write(bs); err != nil {
		return err
	}
	enc.order.PutUint64(bs, math.Float64bits(imag(x)))
	_, err := enc.Write(bs)
	return err
}

示例#17
文件: syscall_windows_test.go 项目: achanda/go

func TestFloatArgs(t *testing.T) {
	if _, err := exec.LookPath("gcc"); err != nil {
		t.Skip("skipping test: gcc is missing")
	}
	if runtime.GOARCH != "amd64" {
		t.Skipf("skipping test: GOARCH=%s", runtime.GOARCH)
	}

	const src = `
#include <stdint.h>
#include <windows.h>

uintptr_t cfunc(uintptr_t a, double b, float c, double d) {
	if (a == 1 && b == 2.2 && c == 3.3f && d == 4.4e44) {
		return 1;
	}
	return 0;
}
`
	tmpdir, err := ioutil.TempDir("", "TestFloatArgs")
	if err != nil {
		t.Fatal("TempDir failed: ", err)
	}
	defer os.RemoveAll(tmpdir)

	srcname := "mydll.c"
	err = ioutil.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
	if err != nil {
		t.Fatal(err)
	}
	outname := "mydll.dll"
	cmd := exec.Command("gcc", "-shared", "-s", "-Werror", "-o", outname, srcname)
	cmd.Dir = tmpdir
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("failed to build dll: %v - %v", err, string(out))
	}
	dllpath := filepath.Join(tmpdir, outname)

	dll := syscall.MustLoadDLL(dllpath)
	defer dll.Release()

	proc := dll.MustFindProc("cfunc")

	r, _, err := proc.Call(
		1,
		uintptr(math.Float64bits(2.2)),
		uintptr(math.Float32bits(3.3)),
		uintptr(math.Float64bits(4.4e44)),
	)
	if r != 1 {
		t.Errorf("got %d want 1 (err=%v)", r, err)
	}
}

示例#18
文件: ratconv_test.go 项目: achanda/go

func TestFloat64SpecialCases(t *testing.T) {
	for _, input := range float64inputs {
		if strings.HasPrefix(input, "long:") {
			if !*long {
				continue
			}
			input = input[len("long:"):]
		}

		r, ok := new(Rat).SetString(input)
		if !ok {
			t.Errorf("Rat.SetString(%q) failed", input)
			continue
		}
		f, exact := r.Float64()

		// 1. Check string -> Rat -> float64 conversions are
		// consistent with strconv.ParseFloat.
		// Skip this check if the input uses "a/b" rational syntax.
		if !strings.Contains(input, "/") {
			e, _ := strconv.ParseFloat(input, 64)

			// Careful: negative Rats too small for
			// float64 become -0, but Rat obviously cannot
			// preserve the sign from SetString("-0").
			switch {
			case math.Float64bits(e) == math.Float64bits(f):
				// Ok: bitwise equal.
			case f == 0 && r.Num().BitLen() == 0:
				// Ok: Rat(0) is equivalent to both +/- float64(0).
			default:
				t.Errorf("strconv.ParseFloat(%q) = %g (%b), want %g (%b); delta = %g", input, e, e, f, f, f-e)
			}
		}

		if !isFinite(f) {
			continue
		}

		// 2. Check f is best approximation to r.
		if !checkIsBestApprox64(t, f, r) {
			// Append context information.
			t.Errorf("(input was %q)", input)
		}

		// 3. Check f->R->f roundtrip is non-lossy.
		checkNonLossyRoundtrip64(t, f)

		// 4. Check exactness using slow algorithm.
		if wasExact := new(Rat).SetFloat64(f).Cmp(r) == 0; wasExact != exact {
			t.Errorf("Rat.SetString(%q).Float64().exact = %t, want %t", input, exact, wasExact)
		}
	}
}

示例#19
文件: write.go 项目: glycerine/rmq

// WriteComplex128 writes a complex128 to the writer
func (mw *Writer) WriteComplex128(f complex128) error {
	o, err := mw.require(18)
	if err != nil {
		return err
	}
	mw.buf[o] = mfixext16
	mw.buf[o+1] = Complex128Extension
	big.PutUint64(mw.buf[o+2:], math.Float64bits(real(f)))
	big.PutUint64(mw.buf[o+10:], math.Float64bits(imag(f)))
	return nil
}

示例#20
文件: float.go 项目: jsternberg/influxdb

func (s *FloatEncoder) Push(v float64) {
	// Only allow NaN as a sentinel value
	if math.IsNaN(v) && !s.finished {
		s.err = fmt.Errorf("unsupported value: NaN")
		return
	}
	if s.first {
		// first point
		s.val = v
		s.first = false
		s.bw.WriteBits(math.Float64bits(v), 64)
		return
	}

	vDelta := math.Float64bits(v) ^ math.Float64bits(s.val)

	if vDelta == 0 {
		s.bw.WriteBit(bitstream.Zero)
	} else {
		s.bw.WriteBit(bitstream.One)

		leading := bits.Clz(vDelta)
		trailing := bits.Ctz(vDelta)

		// Clamp number of leading zeros to avoid overflow when encoding
		leading &= 0x1F
		if leading >= 32 {
			leading = 31
		}

		// TODO(dgryski): check if it's 'cheaper' to reset the leading/trailing bits instead
		if s.leading != ^uint64(0) && leading >= s.leading && trailing >= s.trailing {
			s.bw.WriteBit(bitstream.Zero)
			s.bw.WriteBits(vDelta>>s.trailing, 64-int(s.leading)-int(s.trailing))
		} else {
			s.leading, s.trailing = leading, trailing

			s.bw.WriteBit(bitstream.One)
			s.bw.WriteBits(leading, 5)

			// Note that if leading == trailing == 0, then sigbits == 64.  But that
			// value doesn't actually fit into the 6 bits we have.
			// Luckily, we never need to encode 0 significant bits, since that would
			// put us in the other case (vdelta == 0).  So instead we write out a 0 and
			// adjust it back to 64 on unpacking.
			sigbits := 64 - leading - trailing
			s.bw.WriteBits(sigbits, 6)
			s.bw.WriteBits(vDelta>>trailing, int(sigbits))
		}
	}

	s.val = v
}

示例#21
文件: floats.go 项目: sbinet/gonum-floats

// EqualWithinULP returns true if a and b are equal to within
// the specified number of floating point units in the last place.
func EqualWithinULP(a, b float64, ulp uint) bool {
	if a == b {
		return true
	}
	if math.IsNaN(a) || math.IsNaN(b) {
		return false
	}
	if math.Signbit(a) != math.Signbit(b) {
		return math.Float64bits(math.Abs(a))+math.Float64bits(math.Abs(b)) <= uint64(ulp)
	}
	return ulpDiff(math.Float64bits(a), math.Float64bits(b)) <= uint64(ulp)
}

示例#22
文件: compressor.go 项目: apple314159/go-fpc

func (c *Compressor) Encode(buf *bytes.Buffer, d, e float64) {
	dbits := int64(math.Float64bits(d))
	diff1d := c.pred1.Prediction() ^ dbits
	diff2d := c.pred2.Prediction() ^ dbits

	pred1BetterForD := countLeadingZeros(uint64(diff1d)) >= countLeadingZeros(uint64(diff2d))

	c.pred1.Update(dbits)
	c.pred2.Update(dbits)

	ebits := int64(math.Float64bits(e))
	diff1e := c.pred1.Prediction() ^ ebits
	diff2e := c.pred2.Prediction() ^ ebits

	pred1BetterForE := countLeadingZeros(uint64(diff1e)) >= countLeadingZeros(uint64(diff2e))

	c.pred1.Update(ebits)
	c.pred2.Update(ebits)

	var code byte
	if pred1BetterForD {
		zb := encodeZeroBytes(diff1d)
		code |= byte(zb << 4)
	} else {
		zb := encodeZeroBytes(diff2d)
		code |= 0x80
		code |= byte(zb << 4)
	}

	if pred1BetterForE {
		zb := encodeZeroBytes(diff1e)
		code |= byte(zb)
	} else {
		zb := encodeZeroBytes(diff2e)
		code |= 0x08
		code |= byte(zb)
	}

	// FIXME: ignoring errors
	buf.WriteByte(code)
	if pred1BetterForD {
		buf.Write(c.ToByteArray(diff1d))
	} else {
		buf.Write(c.ToByteArray(diff2d))
	}

	if pred1BetterForE {
		buf.Write(c.ToByteArray(diff1e))
	} else {
		buf.Write(c.ToByteArray(diff2e))
	}
}

示例#23
文件: serialize.go 项目: gcnonato/gohistogram

func (h *NumericHistogram) Bytes() []byte {
	b := make([]byte, 2*8*len(h.bins)+8+8+8)
	binary.LittleEndian.PutUint64(b, h.total)
	binary.LittleEndian.PutUint64(b[8:], uint64(h.maxbins))
	binary.LittleEndian.PutUint64(b[16:], uint64(len(h.bins)))
	p := 24
	for _, v := range h.bins {
		binary.LittleEndian.PutUint64(b[p:], math.Float64bits(v.value))
		binary.LittleEndian.PutUint64(b[p+8:], math.Float64bits(v.count))
		p += 16
	}
	return b
}

示例#24
文件: util.go 项目: kohkimakimoto/bucketstore

func toIndexedBytes(value interface{}) ([]byte, byte) {
	// https://golang.org/pkg/encoding/json/#Unmarshal
	switch converted := value.(type) {
	case bool:
		// JSON boolean
		if converted {
			return []byte{1}, ValueTypeBool
		} else {
			return []byte{0}, ValueTypeBool
		}
	case string:
		// JSON string
		return []byte(converted), ValueTypeString
	case float64:
		// JSON number
		// http://stackoverflow.com/questions/22491876/convert-byte-array-uint8-to-float64-in-golang
		bits := math.Float64bits(converted)
		bytes := make([]byte, 8)
		binary.BigEndian.PutUint64(bytes, bits)
		return bytes, ValueTypeFloat64
	case int:
		// JSON number
		c := float64(converted)

		bits := math.Float64bits(c)
		bytes := make([]byte, 8)
		binary.BigEndian.PutUint64(bytes, bits)
		return bytes, ValueTypeFloat64
	case int64:
		// JSON number
		c := float64(converted)

		bits := math.Float64bits(c)
		bytes := make([]byte, 8)
		binary.BigEndian.PutUint64(bytes, bits)
		return bytes, ValueTypeFloat64
	case nil:
		// JSON null
		return nil, ValueTypeNil
	case []interface{}:
		// JSON array
		// not support indexing...
		return nil, valueTypeNoIndex
	case map[string]interface{}:
		// JSON object
		// not support indexing...
		return nil, valueTypeNoIndex
	default:
		return nil, valueTypeNoIndex
	}
}

示例#25
文件: marshal.go 项目: nicr9/vault

func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) {
	switch v := value.(type) {
	case Marshaler:
		return v.MarshalCQL(info)
	case float64:
		return encBigInt(int64(math.Float64bits(v))), nil
	}
	rv := reflect.ValueOf(value)
	switch rv.Type().Kind() {
	case reflect.Float64:
		return encBigInt(int64(math.Float64bits(rv.Float()))), nil
	}
	return nil, marshalErrorf("can not marshal %T into %s", value, info)
}

示例#26
文件: mesh_test.go 项目: rwcarlsen/optim

func DiffInUlps(x, y float64) uint64 {
	switch {
	case math.IsNaN(x) || math.IsNaN(y) || math.IsInf(x, 0) || math.IsInf(y, 0):
		return math.MaxInt64
	case x == y:
		return 0
	default:
		xi := math.Float64bits(x)
		yi := math.Float64bits(y)
		if xi > yi {
			return xi - yi
		}
		return yi - xi
	}
}

示例#27
文件: rat.go 项目: klueska/go-akaros

// SetFloat64 sets z to exactly f and returns z.
// If f is not finite, SetFloat returns nil.
func (z *Rat) SetFloat64(f float64) *Rat {
	const expMask = 1<<11 - 1
	bits := math.Float64bits(f)
	mantissa := bits & (1<<52 - 1)
	exp := int((bits >> 52) & expMask)
	switch exp {
	case expMask: // non-finite
		return nil
	case 0: // denormal
		exp -= 1022
	default: // normal
		mantissa |= 1 << 52
		exp -= 1023
	}

	shift := 52 - exp

	// Optimization (?): partially pre-normalise.
	for mantissa&1 == 0 && shift > 0 {
		mantissa >>= 1
		shift--
	}

	z.a.SetUint64(mantissa)
	z.a.neg = f < 0
	z.b.Set(intOne)
	if shift > 0 {
		z.b.Lsh(&z.b, uint(shift))
	} else {
		z.a.Lsh(&z.a, uint(-shift))
	}
	return z.norm()
}

示例#28
文件: decode.go 项目: NonaSuomy/rtlamr

// Bit-value is determined by the sign of each sample after filtering.
func Quantize(input []float64, output []byte) {
	for idx, val := range input {
		output[idx] = byte(math.Float64bits(val)>>63) ^ 0x01
	}

	return
}

示例#29
文件: metric.pb.go 项目: kicombs/componentMetrics

func (m *ValueMetric) MarshalTo(data []byte) (int, error) {
	var i int
	_ = i
	var l int
	_ = l
	if m.Name == nil {
		return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("name")
	} else {
		data[i] = 0xa
		i++
		i = encodeVarintMetric(data, i, uint64(len(*m.Name)))
		i += copy(data[i:], *m.Name)
	}
	if m.Value == nil {
		return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("value")
	} else {
		data[i] = 0x11
		i++
		i = encodeFixed64Metric(data, i, uint64(math.Float64bits(*m.Value)))
	}
	if m.Unit == nil {
		return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("unit")
	} else {
		data[i] = 0x1a
		i++
		i = encodeVarintMetric(data, i, uint64(len(*m.Unit)))
		i += copy(data[i:], *m.Unit)
	}
	if m.XXX_unrecognized != nil {
		i += copy(data[i:], m.XXX_unrecognized)
	}
	return i, nil
}

示例#30
文件: struct.pb.go 项目: maditya/protobuf

func (m *Value_NumberValue) MarshalTo(data []byte) (int, error) {
	i := 0
	data[i] = 0x11
	i++
	i = encodeFixed64Struct(data, i, uint64(math.Float64bits(float64(m.NumberValue))))
	return i, nil
}

展开阅读全文