本文整理汇总了Golang中math.Float32frombits函数的典型用法代码示例。如果您正苦于以下问题:Golang Float32frombits函数的具体用法?Golang Float32frombits怎么用?Golang Float32frombits使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Float32frombits函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFloat32_Random
func TestFloat32_Random(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
i := 0
for i < randomChecks {
a1 := math.Float32frombits(randomUint32())
a2 := math.Float32frombits(randomUint32())
if math.IsNaN(float64(a1)) || math.IsNaN(float64(a2)) {
continue
}
i++
r1 := make([]byte, 4)
PutFloat32(r1, a1)
v1 := Float32(r1)
assert.Equal(t, a1, v1)
r2 := make([]byte, 4)
PutFloat32(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)
}
}
}
示例2: read
func (u *UseEntity) read(rr io.Reader) (err error) {
var tmp [4]byte
if u.TargetID, err = ReadVarInt(rr); err != nil {
return
}
if u.Type, err = ReadVarInt(rr); err != nil {
return
}
if u.Type == 2 {
var tmp0 uint32
if _, err = rr.Read(tmp[:4]); err != nil {
return
}
tmp0 = (uint32(tmp[3]) << 0) | (uint32(tmp[2]) << 8) | (uint32(tmp[1]) << 16) | (uint32(tmp[0]) << 24)
u.TargetX = math.Float32frombits(tmp0)
var tmp1 uint32
if _, err = rr.Read(tmp[:4]); err != nil {
return
}
tmp1 = (uint32(tmp[3]) << 0) | (uint32(tmp[2]) << 8) | (uint32(tmp[1]) << 16) | (uint32(tmp[0]) << 24)
u.TargetY = math.Float32frombits(tmp1)
var tmp2 uint32
if _, err = rr.Read(tmp[:4]); err != nil {
return
}
tmp2 = (uint32(tmp[3]) << 0) | (uint32(tmp[2]) << 8) | (uint32(tmp[1]) << 16) | (uint32(tmp[0]) << 24)
u.TargetZ = math.Float32frombits(tmp2)
}
if u.Type == 0 || u.Type == 2 {
if u.Hand, err = ReadVarInt(rr); err != nil {
return
}
}
return
}
示例3: Transform
func (smp complexLEF32Sampler) Transform(buf []byte, data accel.DSPSplitComplex) {
for i := 0; i < len(data.Real); i++ {
j := i * 8
data.Real[i] = math.Float32frombits(binary.LittleEndian.Uint32(buf[j : j+4]))
data.Imag[i] = math.Float32frombits(binary.LittleEndian.Uint32(buf[j+4 : j+8]))
}
}
示例4: 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()),
))
}
}
示例5: complex64Decoder
func complex64Decoder(dec *decoder, v reflect.Value) error {
bs := dec.buf[:8]
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
v.SetComplex(complex(
float64(math.Float32frombits(dec.order.Uint32(bs))),
float64(math.Float32frombits(dec.order.Uint32(bs[4:]))),
))
return nil
}
示例6: complex64Decoder
func complex64Decoder(dec *decoder, p unsafe.Pointer) error {
bs := dec.buf[:8]
if err := readAtLeast(dec, bs, 8); err != nil {
return err
}
v := (*complex64)(p)
*v = complex(
math.Float32frombits(dec.order.Uint32(bs)),
math.Float32frombits(dec.order.Uint32(bs[4:])),
)
return nil
}
示例7: handleConnection
func handleConnection(conn net.Conn) {
player := &Player{Conn: conn}
defer func() {
log.Printf("Player %q left (%s)\n", player.Nick, conn.RemoteAddr())
mu.Lock()
game.Leave(player)
mu.Unlock()
}()
var nickLength uint32
binary.Read(conn, binary.BigEndian, &nickLength)
nickBytes := make([]byte, nickLength)
if _, err := io.ReadFull(conn, nickBytes); err != nil {
log.Println(err)
return
}
player.Nick = string(nickBytes)
mu.Lock()
game.Join(player, 200, 200)
mu.Unlock()
log.Printf("Player %q joined (%s)\n", player.Nick, conn.RemoteAddr())
buf := make([]byte, 12)
for {
if _, err := io.ReadFull(conn, buf); err != nil {
if err != io.EOF {
log.Println(err)
}
return
}
mu.Lock()
player.JoystickX = math.Float32frombits(binary.BigEndian.Uint32(buf))
player.JoystickY = math.Float32frombits(binary.BigEndian.Uint32(buf[4:]))
buttons := binary.BigEndian.Uint32(buf[8:])
player.ButtonA = buttons&1 != 0
player.ButtonB = buttons&2 != 0
if player.JoystickX < -1 {
player.JoystickX = -1
} else if player.JoystickX > 1 {
player.JoystickX = 1
}
if player.JoystickY < -1 {
player.JoystickY = 1
} else if player.JoystickY > 1 {
player.JoystickY = 1
}
mu.Unlock()
}
}
示例8: ReadFloat32
func (p *Parser) ReadFloat32(value *float32) *Parser {
bufFloat32 := make([]byte, 4)
if bufFloat32 = p.buffer.Next(4); len(bufFloat32) < 4 {
p.err = errors.New("Not enough bytes in buffer")
return p
}
if p.Endian() == BigEndian {
(*value) = math.Float32frombits(binary.BigEndian.Uint32(bufFloat32))
} else {
(*value) = math.Float32frombits(binary.LittleEndian.Uint32(bufFloat32))
}
return p
}
示例9: decodeFloat
func (d *simpleDecDriver) decodeFloat(chkOverflow32 bool) (f float64) {
switch d.bd {
case simpleVdFloat32:
f = float64(math.Float32frombits(d.r.readUint32()))
case simpleVdFloat64:
f = math.Float64frombits(d.r.readUint64())
default:
if d.bd >= simpleVdPosInt1 && d.bd <= simpleVdNegInt8 {
_, i, _ := d.decIntAny()
f = float64(i)
} else {
decErr("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
}
}
// check overflow (logic adapted from std pkg reflect/value.go OverflowFloat()
if chkOverflow32 {
f2 := f
if f2 < 0 {
f2 = -f
}
if math.MaxFloat32 < f2 && f2 <= math.MaxFloat64 {
decErr("Overflow float32 value: %v", f2)
}
}
return
}
示例10: ReadFloat32
func ReadFloat32(buf []byte, format byte, endianness binary.ByteOrder) float32 {
encoding := format & EncodingMask
if encoding == EncodingFloatingPoint {
return math.Float32frombits(endianness.Uint32(buf))
} else {
offset := 0
if endianness == binary.LittleEndian {
offset = len(buf) - 1
}
var neg byte = 0
if encoding == EncodingSignedInt && buf[offset]&(1<<7) != 0 {
neg = 0xFF
}
tmp := []byte{neg, neg, neg, neg}
if endianness == binary.BigEndian {
copy(tmp[4-len(buf):], buf)
} else {
copy(tmp, buf)
}
sample := endianness.Uint32(tmp)
div := math.Pow(2, float64(len(buf)*8-1))
if encoding == EncodingSignedInt {
return float32(float64(int32(sample)) / div)
} else {
return float32(float64(sample)/div - 1.0)
}
}
}
示例11: TestFloat32
func TestFloat32(t *testing.T) {
var buf = make([]byte, 4)
var expected = float32(2 << 15)
// Put
bytes, err := LittleEndian.PutFloat32(buf, 0, expected)
assert.Nil(t, err)
assert.Equal(t, expected, math.Float32frombits(binary.LittleEndian.Uint32(buf)))
assert.Equal(t, uint64(4), bytes)
// Put error (index = -1)
bytes, err = LittleEndian.PutFloat32(buf, -1, expected)
assert.NotNil(t, err)
assert.Equal(t, uint64(0), bytes)
// Put error (index = 2)
bytes, err = LittleEndian.PutFloat32(buf, 2, expected)
assert.NotNil(t, err)
assert.Equal(t, uint64(0), bytes)
// Get
val, err := LittleEndian.Float32(buf, 0)
assert.Nil(t, err)
assert.Equal(t, expected, val)
// Get Error (index = -1)
val, err = LittleEndian.Float32(buf, -1)
assert.NotNil(t, err)
assert.Equal(t, float32(0), val)
// Get Error (index = 2)
val, err = LittleEndian.Float32(buf, 2)
assert.NotNil(t, err)
assert.Equal(t, float32(0), val)
}
示例12: TestRoundTrip32
// TestRoundTrip32 tries a fraction of all finite positive float32 values.
func TestRoundTrip32(t *testing.T) {
step := uint32(997)
if testing.Short() {
step = 99991
}
count := 0
for i := uint32(0); i < 0xff<<23; i += step {
f := math.Float32frombits(i)
if i&1 == 1 {
f = -f // negative
}
s := FormatFloat(float64(f), 'g', -1, 32)
parsed, err := ParseFloat(s, 32)
parsed32 := float32(parsed)
switch {
case err != nil:
t.Errorf("ParseFloat(%q, 32) gave error %s", s, err)
case float64(parsed32) != parsed:
t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32)
case parsed32 != f:
t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f)
}
count++
}
t.Logf("tested %d float32's", count)
}
示例13: decodeFloat4
func decodeFloat4(vr *ValueReader) float32 {
if vr.Len() == -1 {
vr.Fatal(ProtocolError("Cannot decode null into float32"))
return 0
}
if vr.Type().DataType != Float4Oid {
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into float32", vr.Type().DataType)))
return 0
}
switch vr.Type().FormatCode {
case TextFormatCode:
s := vr.ReadString(vr.Len())
n, err := strconv.ParseFloat(s, 32)
if err != nil {
vr.Fatal(ProtocolError(fmt.Sprintf("Received invalid float4: %v", s)))
return 0
}
return float32(n)
case BinaryFormatCode:
if vr.Len() != 4 {
vr.Fatal(ProtocolError(fmt.Sprintf("Received an invalid size for an float4: %d", vr.Len())))
return 0
}
i := vr.ReadInt32()
return math.Float32frombits(uint32(i))
default:
vr.Fatal(ProtocolError(fmt.Sprintf("Unknown field description format code: %v", vr.Type().FormatCode)))
return 0
}
}
示例14: Float32
// Float32 converts the 8-bit floating-point value to a float32. The 8-bit and
// the 32-bit floating-point notation formats are described at Float8 and New
// respectively.
func (f Float8) Float32() float32 {
if f == 0 {
// Return the floating-point value 0.
return 0
}
// bits is the bit representation of the float32 value.
var bits uint32
// Sign bit.
if f.Sign() {
bits |= 0x80000000
}
// Exponent.
exp := f.Exp()
exp-- // adjust exponent for the implicit 1.
// Encode exponent.
xexp := uint8(exp)
xexp += 127 // excess 127 notation.
bits |= uint32(xexp) << 23
// Mantissa.
mantissa := f.Mantissa()
// remove the implicit 1 from the mantissa.
mantissa &^= 0x8
// Encode the mantissa.
bits |= uint32(mantissa) << 20
return math.Float32frombits(bits)
}
示例15: readFloat32
// read/write float32.
func (ps *PacketSerializer) readFloat32(reader io.Reader) (v float32, err error) {
var vUint32 uint32
if vUint32, err = ps.readUint32(reader); err != nil {
return
}
return math.Float32frombits(vUint32), nil
}