本文整理匯總了Golang中math.Float64frombits函數的典型用法代碼示例。如果您正苦於以下問題:Golang Float64frombits函數的具體用法?Golang Float64frombits怎麽用?Golang Float64frombits使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Float64frombits函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: atof64
func atof64(s string) (f float64, err error) {
if val, ok := special(s); ok {
return val, nil
}
var d decimal
if !d.set(s) {
return 0, syntaxError(fnParseFloat, s)
}
if optimize {
if f, ok := d.atof64(); ok {
return f, nil
}
// Try another fast path.
ext := new(extFloat)
if ok := ext.AssignDecimal(&d); ok {
b, ovf := ext.floatBits()
f = math.Float64frombits(b)
if ovf {
err = rangeError(fnParseFloat, s)
}
return f, err
}
}
b, ovf := d.floatBits(&float64info)
f = math.Float64frombits(b)
if ovf {
err = rangeError(fnParseFloat, s)
}
return f, err
}
示例2: DecodeFloatAscending
// DecodeFloatAscending returns the remaining byte slice after decoding and the decoded
// float64 from buf.
func DecodeFloatAscending(buf []byte) ([]byte, float64, error) {
if PeekType(buf) != Float {
return buf, 0, util.Errorf("did not find marker")
}
switch buf[0] {
case floatNaN, floatNaNDesc:
return buf[1:], math.NaN(), nil
case floatNeg:
b, u, err := DecodeUint64Ascending(buf[1:])
if err != nil {
return b, 0, err
}
u = ^u
return b, math.Float64frombits(u), nil
case floatZero:
return buf[1:], 0, nil
case floatPos:
b, u, err := DecodeUint64Ascending(buf[1:])
if err != nil {
return b, 0, err
}
return b, math.Float64frombits(u), nil
default:
return nil, 0, util.Errorf("unknown prefix of the encoded byte slice: %q", buf)
}
}
示例3: TestCounterAdd
func TestCounterAdd(t *testing.T) {
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
ConstLabels: Labels{"a": "1", "b": "2"},
}).(*counter)
counter.Inc()
if expected, got := 1., math.Float64frombits(counter.valBits); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
counter.Add(42)
if expected, got := 43., math.Float64frombits(counter.valBits); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
t.Errorf("Expected error %q, got %q.", expected, got)
}
m := &dto.Metric{}
counter.Write(m)
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:43 > `, m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
}
}
示例4: TestFloat64_Random
func TestFloat64_Random(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
i := 0
for i < randomChecks {
a1 := math.Float64frombits(randomUint64())
a2 := math.Float64frombits(randomUint64())
if math.IsNaN(a1) || math.IsNaN(a2) {
continue
}
i++
r1 := make([]byte, 8)
PutFloat64(r1, a1)
v1 := Float64(r1)
assert.Equal(t, a1, v1)
r2 := make([]byte, 8)
PutFloat64(r2, a2)
switch {
case a1 < a2:
assert.Equal(t, -1, bytes.Compare(r1, r2), "%v %v", a1, a2)
case a1 == a2:
assert.Equal(t, 0, bytes.Compare(r1, r2), "%v %v", a1, a2)
case a1 > a2:
assert.Equal(t, +1, bytes.Compare(r1, r2), "%v %v", a1, a2)
}
}
}
示例5: Transform
func (smp complexLEF64Sampler) Transform(buf []byte, data accel.DSPSplitComplex) {
for i := 0; i < len(data.Real); i++ {
j := i * 16
data.Real[i] = float32(math.Float64frombits(binary.LittleEndian.Uint64(buf[j : j+8])))
data.Imag[i] = float32(math.Float64frombits(binary.LittleEndian.Uint64(buf[j+8 : j+16])))
}
}
示例6: read
func (p *PlayerPosition) read(rr io.Reader) (err error) {
var tmp [8]byte
var tmp0 uint64
if _, err = rr.Read(tmp[:8]); err != nil {
return
}
tmp0 = (uint64(tmp[7]) << 0) | (uint64(tmp[6]) << 8) | (uint64(tmp[5]) << 16) | (uint64(tmp[4]) << 24) | (uint64(tmp[3]) << 32) | (uint64(tmp[2]) << 40) | (uint64(tmp[1]) << 48) | (uint64(tmp[0]) << 56)
p.X = math.Float64frombits(tmp0)
var tmp1 uint64
if _, err = rr.Read(tmp[:8]); err != nil {
return
}
tmp1 = (uint64(tmp[7]) << 0) | (uint64(tmp[6]) << 8) | (uint64(tmp[5]) << 16) | (uint64(tmp[4]) << 24) | (uint64(tmp[3]) << 32) | (uint64(tmp[2]) << 40) | (uint64(tmp[1]) << 48) | (uint64(tmp[0]) << 56)
p.Y = math.Float64frombits(tmp1)
var tmp2 uint64
if _, err = rr.Read(tmp[:8]); err != nil {
return
}
tmp2 = (uint64(tmp[7]) << 0) | (uint64(tmp[6]) << 8) | (uint64(tmp[5]) << 16) | (uint64(tmp[4]) << 24) | (uint64(tmp[3]) << 32) | (uint64(tmp[2]) << 40) | (uint64(tmp[1]) << 48) | (uint64(tmp[0]) << 56)
p.Z = math.Float64frombits(tmp2)
if p.OnGround, err = ReadBool(rr); err != nil {
return
}
return
}
示例7: decode
func (c *Compressor) decode(buf *bytes.Buffer) []float64 {
head, _ := buf.ReadByte()
var pred int64
if (head & 0x80) != 0 {
pred = c.pred2.Prediction()
} else {
pred = c.pred1.Prediction()
}
nzb := (head & 0x70) >> 4
if nzb > 3 {
nzb++
}
dst := make([]byte, 8-nzb)
// FIXME: errors
buf.Read(dst)
diff := c.ToLong(dst)
actual := pred ^ diff
c.pred1.Update(actual)
c.pred2.Update(actual)
var ret []float64
ret = append(ret, math.Float64frombits(uint64(actual)))
if (head & 0x08) != 0 {
pred = c.pred2.Prediction()
} else {
pred = c.pred1.Prediction()
}
nzb = head & 0x07
if nzb > 3 {
nzb++
}
dst = make([]byte, 8-nzb)
// FIXME: errors
buf.Read(dst)
diff = c.ToLong(dst)
if nzb == 7 && diff == 0 {
return ret
}
actual = pred ^ diff
c.pred1.Update(actual)
c.pred2.Update(actual)
return append(ret, math.Float64frombits(uint64(actual)))
}
示例8: Unmarshal
// Unmarshal overwrites f with the restored value of the TMFRAME found
// in the by []byte data.
func (f *Frame) Unmarshal(by []byte) (rest []byte, err error) {
// zero it all
*f = Frame{}
n := int64(len(by))
if n < 8 {
return by, TooShortErr
}
prim := binary.LittleEndian.Uint64(by[:8])
pti := PTI(prim % 8)
f.Prim = int64(prim)
switch pti {
case PtiZero:
f.V0 = 0.0
return by[8:], nil
case PtiOne:
f.V0 = 1.0
return by[8:], nil
case PtiOneFloat64:
if n < 16 {
return by, TooShortErr
}
f.V0 = math.Float64frombits(binary.LittleEndian.Uint64(by[8:16]))
return by[16:], nil
case PtiTwo64:
if n < 24 {
return by, TooShortErr
}
f.V0 = math.Float64frombits(binary.LittleEndian.Uint64(by[8:16]))
f.V1 = int64(binary.LittleEndian.Uint64(by[16:24]))
return by[24:], nil
case PtiNull:
return by[8:], nil
case PtiNA:
return by[8:], nil
case PtiNaN:
// don't actually do this, as it make reflect.DeepEquals not work (of course): f.V0 = MyNaN
return by[8:], nil
case PtiUDE:
ude := binary.LittleEndian.Uint64(by[8:16])
f.Ude = int64(ude)
ucount := ude & KeepLow43Bits
ulen := int64(ucount)
if n < 16+ulen {
return by, TooShortErr
}
if ulen > 0 {
f.Data = by[16 : 16+ucount-1] // -1 because the zero terminating byte only goes on the wire
}
return by[16+ucount:], nil
default:
panic(fmt.Sprintf("unrecog pti: %v", pti))
}
// panic("should never get here")
}
示例9: readByteSortableFloat
func readByteSortableFloat(b []byte) float64 {
if b[0] < 0x80 {
for i, v := range b[:8] {
b[i] = v ^ 255
}
return math.Float64frombits(binary.BigEndian.Uint64(b))
}
return -math.Float64frombits(binary.BigEndian.Uint64(b))
}
示例10: value
func (d *decoder) value(v reflect.Value) {
switch v.Kind() {
case reflect.Array:
l := v.Len()
for i := 0; i < l; i++ {
d.value(v.Index(i))
}
case reflect.Struct:
l := v.NumField()
for i := 0; i < l; i++ {
d.value(v.Field(i))
}
case reflect.Slice:
l := v.Len()
for i := 0; i < l; i++ {
d.value(v.Index(i))
}
case reflect.Int8:
v.SetInt(int64(d.int8()))
case reflect.Int16:
v.SetInt(int64(d.int16()))
case reflect.Int32:
v.SetInt(int64(d.int32()))
case reflect.Int64:
v.SetInt(d.int64())
case reflect.Uint8:
v.SetUint(uint64(d.uint8()))
case reflect.Uint16:
v.SetUint(uint64(d.uint16()))
case reflect.Uint32:
v.SetUint(uint64(d.uint32()))
case reflect.Uint64:
v.SetUint(d.uint64())
case reflect.Float32:
v.SetFloat(float64(math.Float32frombits(d.uint32())))
case reflect.Float64:
v.SetFloat(math.Float64frombits(d.uint64()))
case reflect.Complex64:
v.SetComplex(complex(
float64(math.Float32frombits(d.uint32())),
float64(math.Float32frombits(d.uint32())),
))
case reflect.Complex128:
v.SetComplex(complex(
math.Float64frombits(d.uint64()),
math.Float64frombits(d.uint64()),
))
}
}
示例11: ReqSET2
func ReqSET2(msg []byte, pId uint16) error {
// TODO: check len(msg)
switch msg[0] {
case 0: //Unit
unitId := binary.BigEndian.Uint32(msg[1:5])
unit, ok := Units[unitId]
if !ok {
return errors.New("No such Unit")
}
unit.Lock()
defer unit.Unlock()
if unit.Owner.A != pId {
return errors.New("Do not own Unit")
}
switch msg[5] { //Switch attributes
case 0: //X,Y
if pId != 0 {
return errors.New("Can't change Unit.X/Y")
}
x := math.Float64frombits(binary.BigEndian.Uint64(msg[6:14]))
y := math.Float64frombits(binary.BigEndian.Uint64(msg[14:22]))
unit.X.Update(x)
unit.Y.Update(y)
case 1: //Vx,Vy
vx := math.Float64frombits(binary.BigEndian.Uint64(msg[6:14]))
vy := math.Float64frombits(binary.BigEndian.Uint64(msg[14:22]))
if math.Sqrt(vx*vx+vy*vy) > unit.MaxSpeed.A {
return errors.New("Desired speed > Unit.MaxSpeed")
}
deltafov(unitId, 5, vx, vy)
unit.Vx.Update(vx)
unit.Vy.Update(vy)
updateFn := func(id ...interface{}) {
unit := Units[id[0].(uint32)]
unit.Lock()
defer unit.Unlock()
//TODO: Check for confilcts
unit.X.Update(unit.X.A + unit.Vx.A)
unit.Y.Update(unit.Y.A + unit.Vy.A)
}
Timers[timersI] = &Timer{fn: updateFn,
args: []interface{}{unitId},
Delta: 1.0,
id: timersI,
inform: unit.Vx.inform}
Timers[timersI].Go()
//unit.MvTimer.Update(timersI)
timersI++
}
}
return nil
}
示例12: main
func main() {
AES_KEY := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
PID := []byte{0, 1}
ServerAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:8888")
LocalAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:0")
Conn, _ := net.DialUDP("udp", LocalAddr, ServerAddr)
defer Conn.Close()
go func() {
for {
p := make([]byte, 2048)
n, _ := Conn.Read(p)
msg, err := encryption.Decrypt(p[:n], AES_KEY)
if err != nil {
fmt.Printf("%s\n", err)
return
}
switch msg[0] {
case 0:
fmt.Printf("Unit[")
id := binary.BigEndian.Uint32(msg[1:5])
fmt.Printf("%d].%d := %x\n", id, msg[5], msg[6:])
case 1:
fmt.Printf("Timer[")
id := binary.BigEndian.Uint32(msg[1:5])
start := math.Float64frombits(binary.BigEndian.Uint64(msg[5:13]))
delta := math.Float64frombits(binary.BigEndian.Uint64(msg[13:21]))
fmt.Printf("%d] := %f->%f\n", id, start, delta)
break
default:
fmt.Printf("%d[", msg[0])
id := binary.BigEndian.Uint32(msg[1:5])
fmt.Printf("%d].%d := %x\n", id, msg[5], msg[6:])
}
}
}()
for {
input := make([]byte, 0)
for {
var x byte
_, err := fmt.Scanf("%x", &x)
if err != nil {
break
}
input = append(input, x)
}
buf := encryption.Encrypt(input, AES_KEY)
buf = append(PID, buf...)
Conn.Write(buf)
}
}
示例13: complex128Decoder
func complex128Decoder(dec *decoder, v reflect.Value) error {
bs := dec.buf[:8]
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
f1 := math.Float64frombits(dec.order.Uint64(bs))
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
v.SetComplex(complex(f1, math.Float64frombits(dec.order.Uint64(bs))))
return nil
}
示例14: parseComplexArray
func parseComplexArray(buf []byte, offset, end int) (interface{}, int, error) {
length := end - offset
cArr := make([]complex128, 0, length/16)
for ; offset < end; offset += 16 {
bitsReal := binary.LittleEndian.Uint64(buf[offset : offset+8])
bitsImag := binary.LittleEndian.Uint64(buf[offset+8 : offset+16])
cArr = append(cArr, complex(math.Float64frombits(bitsReal), math.Float64frombits(bitsImag)))
}
if len(cArr) == 1 {
return cArr[0], offset, nil
}
return cArr, offset, nil
}
示例15: complex128Decoder
func complex128Decoder(dec *decoder, p unsafe.Pointer) error {
bs := dec.buf[:8]
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
f1 := math.Float64frombits(dec.order.Uint64(bs))
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
v := (*complex128)(p)
*v = complex(f1, math.Float64frombits(dec.order.Uint64(bs)))
return nil
}