Golang Float32bits示例

说明

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

编程语言: Golang

命名空间/包名称: math

示例#1
文件: memory_test.go 项目: jamesbjackson/cuda4

func TestMemsetAsync(t *testing.T) {
	N := int64(32 * 1024)
	host1 := make([]float32, N)
	for i := range host1 {
		host1[i] = float32(i)
	}
	host2 := make([]float32, N)
	dev1 := MemAlloc(int64(4 * N))
	MemcpyHtoD(dev1, (unsafe.Pointer(&host1[0])), 4*N)
	str := StreamCreate()
	MemsetD32Async(dev1, math.Float32bits(42), N, str)
	MemsetD32Async(dev1, math.Float32bits(21), N/2, str)
	MemcpyDtoH((unsafe.Pointer(&host2[0])), dev1, 4*N)
	str.Synchronize()
	(&str).Destroy()
	for i := 0; i < len(host2)/2; i++ {
		if host2[i] != 21 {
			t.Fail()
		}
	}
	for i := len(host2) / 2; i < len(host2); i++ {
		if host2[i] != 42 {
			t.Fail()
		}
	}
	dev1.Free()
}

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

func test32(a, b float32) {
	abits := math.Float32bits(a)
	bbits := math.Float32bits(b)
	if abits != bbits {
		panic(fmt.Sprintf("%08x != %08x\n", abits, bbits))
	}
}

示例#3
文件: gateway.pb.go 项目: TheThingsNetwork/ttn

func (m *GPSMetadata) MarshalTo(dAtA []byte) (int, error) {
	var i int
	_ = i
	var l int
	_ = l
	if m.Time != 0 {
		dAtA[i] = 0x8
		i++
		i = encodeVarintGateway(dAtA, i, uint64(m.Time))
	}
	if m.Latitude != 0 {
		dAtA[i] = 0x15
		i++
		i = encodeFixed32Gateway(dAtA, i, uint32(math.Float32bits(float32(m.Latitude))))
	}
	if m.Longitude != 0 {
		dAtA[i] = 0x1d
		i++
		i = encodeFixed32Gateway(dAtA, i, uint32(math.Float32bits(float32(m.Longitude))))
	}
	if m.Altitude != 0 {
		dAtA[i] = 0x20
		i++
		i = encodeVarintGateway(dAtA, i, uint64(m.Altitude))
	}
	return i, nil
}

示例#4
文件: binary.go 项目: tstranex/carpcomm

func WriteComplex64LE(w io.Writer, c complex64) error {
	b := make([]byte, 8)

	// little-endian

	re := math.Float32bits(real(c))
	for i := 0; i < 4; i++ {
		b[i] = byte(re & 255)
		re = re >> 8
	}

	im := math.Float32bits(imag(c))
	for i := 4; i < 8; i++ {
		b[i] = byte(im & 255)
		im = im >> 8
	}

	n, err := w.Write(b)
	if n != 8 {
		return errors.New(fmt.Sprintf(
			"Too few bytes written: %d, %s", n, err.Error()))
	}

	return nil
}

示例#5
文件: 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
}

示例#6
文件: rat_haxe_test.go 项目: joao-parana/tardisgo

func TestFloat32SpecialCases(t *testing.T) {
	//switch runtime.GOARCH {
	//case "cs", "java":
	//	return
	//}
	for _, input := range float64inputs {
		if strings.HasPrefix(input, "long:") {
			if testing.Short() {
				continue
			}
			input = input[len("long:"):]
		}

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

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

			// Careful: negative Rats too small for
			// float64 become -0, but Rat obviously cannot
			// preserve the sign from SetString("-0").
			switch {
			case math.Float32bits(e) == math.Float32bits(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(float64(f)) {
			continue
		}

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

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

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

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

func complex64Encoder(enc *encoder, v reflect.Value) error {
	bs := enc.buf[:8]
	x := v.Complex()
	enc.order.PutUint32(bs, math.Float32bits(float32(real(x))))
	enc.order.PutUint32(bs[4:], math.Float32bits(float32(imag(x))))
	_, err := enc.Write(bs)
	return err
}

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

func complex64Encoder(enc *encoder, p unsafe.Pointer) error {
	bs := enc.buf[:8]
	v := (*complex64)(p)
	enc.order.PutUint32(bs, math.Float32bits(real(*v)))
	enc.order.PutUint32(bs[4:], math.Float32bits(imag(*v)))
	_, err := enc.Write(bs)
	return err
}

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

// AppendComplex64 appends a complex64 to the slice as a MessagePack extension
func AppendComplex64(b []byte, c complex64) []byte {
	o, n := ensure(b, Complex64Size)
	o[n] = mfixext8
	o[n+1] = Complex64Extension
	big.PutUint32(o[n+2:], math.Float32bits(real(c)))
	big.PutUint32(o[n+6:], math.Float32bits(imag(c)))
	return o
}

示例#10
文件: gl.go 项目: ych1/mobile

func (ctx *context) PolygonOffset(factor, units float32) {
	ctx.enqueue(call{
		args: fnargs{
			fn: glfnPolygonOffset,
			a0: uintptr(math.Float32bits(factor)),
			a1: uintptr(math.Float32bits(units)),
		},
	})
}

示例#11
文件: gl.go 项目: ych1/mobile

func (ctx *context) DepthRangef(n, f float32) {
	ctx.enqueue(call{
		args: fnargs{
			fn: glfnDepthRangef,
			a0: uintptr(math.Float32bits(n)),
			a1: uintptr(math.Float32bits(f)),
		},
	})
}

示例#12
文件: gl.go 项目: samanalysis/mobile

func (ctx *context) PolygonOffset(factor, units float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnPolygonOffset,
			a0: C.uintptr_t(math.Float32bits(factor)),
			a1: C.uintptr_t(math.Float32bits(units)),
		},
	})
}

示例#13
文件: gl.go 项目: samanalysis/mobile

func (ctx *context) DepthRangef(n, f float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnDepthRangef,
			a0: C.uintptr_t(math.Float32bits(n)),
			a1: C.uintptr_t(math.Float32bits(f)),
		},
	})
}

示例#14
文件: gl.go 项目: samanalysis/mobile

func (ctx *context) Uniform2f(dst Uniform, v0, v1 float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniform2f,
			a0: dst.c(),
			a1: C.uintptr_t(math.Float32bits(v0)),
			a2: C.uintptr_t(math.Float32bits(v1)),
		},
	})
}

示例#15
文件: gl.go 项目: ych1/mobile

func (ctx *context) Uniform2f(dst Uniform, v0, v1 float32) {
	ctx.enqueue(call{
		args: fnargs{
			fn: glfnUniform2f,
			a0: dst.c(),
			a1: uintptr(math.Float32bits(v0)),
			a2: uintptr(math.Float32bits(v1)),
		},
	})
}

示例#16
文件: gl.go 项目: ych1/mobile

func (ctx *context) VertexAttrib2f(dst Attrib, x, y float32) {
	ctx.enqueue(call{
		args: fnargs{
			fn: glfnVertexAttrib2f,
			a0: dst.c(),
			a1: uintptr(math.Float32bits(x)),
			a2: uintptr(math.Float32bits(y)),
		},
	})
}

示例#17
文件: gl.go 项目: samanalysis/mobile

func (ctx *context) VertexAttrib2f(dst Attrib, x, y float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnVertexAttrib2f,
			a0: dst.c(),
			a1: C.uintptr_t(math.Float32bits(x)),
			a2: C.uintptr_t(math.Float32bits(y)),
		},
	})
}

示例#18
文件: float8_test.go 项目: mewmew/playground

func TestFloatFloat32(t *testing.T) {
	for i, g := range golden {
		f32 := g.f8.Float32()
		if f32 != g.f32 {
			got := math.Float32bits(f32)
			want := math.Float32bits(g.f32)
			t.Errorf("i=%d: expected %032b, got %032b.", i, want, got)
			continue
		}
	}
}

示例#19
文件: gl.go 项目: ych1/mobile

func (ctx *context) BlendColor(red, green, blue, alpha float32) {
	ctx.enqueue(call{
		args: fnargs{
			fn: glfnBlendColor,
			a0: uintptr(math.Float32bits(red)),
			a1: uintptr(math.Float32bits(green)),
			a2: uintptr(math.Float32bits(blue)),
			a3: uintptr(math.Float32bits(alpha)),
		},
	})
}

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

// WriteComplex64 writes a complex64 to the writer
func (mw *Writer) WriteComplex64(f complex64) error {
	o, err := mw.require(10)
	if err != nil {
		return err
	}
	mw.buf[o] = mfixext8
	mw.buf[o+1] = Complex64Extension
	big.PutUint32(mw.buf[o+2:], math.Float32bits(real(f)))
	big.PutUint32(mw.buf[o+6:], math.Float32bits(imag(f)))
	return nil
}

示例#21
文件: gl.go 项目: samanalysis/mobile

func (ctx *context) BlendColor(red, green, blue, alpha float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnBlendColor,
			a0: C.uintptr_t(math.Float32bits(red)),
			a1: C.uintptr_t(math.Float32bits(green)),
			a2: C.uintptr_t(math.Float32bits(blue)),
			a3: C.uintptr_t(math.Float32bits(alpha)),
		},
	})
}

示例#22
文件: if_test.go 项目: bjwbell/gensimd

func TestIf(t *testing.T) {

	count := 0

	for j := -63; j <= 63; j++ {

		count++

		x := int64(0)
		if j < 0 {
			x = -1 << uint(-j)
		} else {
			x = 1<<uint(j) - 1
		}

		if ift0s(uint8(x)) != ift0(uint8(x)) {
			t.Errorf("ift0s (%v) != ift0 (%v)", ift0s(uint8(x)), ift0(uint8(x)))
		}
		if ift1s(uint16(x)) != ift1(uint16(x)) {
			t.Errorf("ift1s (%v) != ift1 (%v)", ift1s(uint16(x)), ift1(uint16(x)))
		}
		if ift2s(uint32(x)) != ift2(uint32(x)) {
			t.Errorf("ift2s (%v) != ift2 (%v)", ift2s(uint32(x)), ift2(uint32(x)))
		}
		if ift3s(uint64(x)) != ift3(uint64(x)) {
			t.Errorf("ift3s (%v) != ift3 (%v)", ift3s(uint64(x)), ift3(uint64(x)))
		}

		if ift4s(int8(x)) != ift4(int8(x)) {
			t.Errorf("ift4s (%v) != ift4 (%v)", ift4s(int8(x)), ift4(int8(x)))
		}
		if ift5s(int16(x)) != ift5(int16(x)) {
			t.Errorf("ift5s (%v) != ift5 (%v)", ift5s(int16(x)), ift5(int16(x)))
		}
		if ift6s(int32(x)) != ift6(int32(x)) {
			t.Errorf("ift6s (%v) != ift6 (%v)", ift6s(int32(x)), ift6(int32(x)))
		}
		if ift7s(x) != ift7(x) {
			t.Errorf("ift7s (%v) != ift7 (%v)", ift7s(x), ift7(x))
		}

		if ift8s(float32(x)) != ift8(float32(x)) {
			sbits := math.Float32bits(ift8s(float32(x)))
			tbits := math.Float32bits(ift8(float32(x)))
			t.Errorf("ift8s (x=%v) (%v=%08x) != ift8 (%v=%08x)", float32(x), ift8s(float32(x)), sbits, ift8(float32(x)), tbits)
		}
		if ift9s(float64(x)) != ift9(float64(x)) {
			t.Errorf("ift9s (%v) != ift9 (%v)", ift9s(float64(x)), ift9(float64(x)))
		}
	}

	t.Log("Test Count:", count)
}

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

func marshalFloat(info TypeInfo, value interface{}) ([]byte, error) {
	switch v := value.(type) {
	case Marshaler:
		return v.MarshalCQL(info)
	case float32:
		return encInt(int32(math.Float32bits(v))), nil
	}
	rv := reflect.ValueOf(value)
	switch rv.Type().Kind() {
	case reflect.Float32:
		return encInt(int32(math.Float32bits(float32(rv.Float())))), nil
	}
	return nil, marshalErrorf("can not marshal %T into %s", value, info)
}

示例#24
文件: gogoproto.pb.go 项目: jmptrader/messenger

func (m *GoGoProtobufTestMessage1) MarshalTo(data []byte) (n int, err error) {
	var i int
	_ = i
	var l int
	_ = l
	if m.F0 != nil {
		data[i] = 0x8
		i++
		i = encodeVarintGogoproto(data, i, uint64(uint32(*m.F0)))
	}
	if m.F1 != nil {
		data[i] = 0x12
		i++
		i = encodeVarintGogoproto(data, i, uint64(len(*m.F1)))
		i += copy(data[i:], *m.F1)
	}
	if m.F2 != nil {
		data[i] = 0x1d
		i++
		i = encodeFixed32Gogoproto(data, i, uint32(math2.Float32bits(*m.F2)))
	}
	if m.XXX_unrecognized != nil {
		i += copy(data[i:], m.XXX_unrecognized)
	}
	return i, nil
}

示例#25
文件: issue14553.go 项目: Samurais/go

func main() {
	for _, t := range []struct {
		value float32
		bits  uint32
	}{
		{0e+00, 0x00000000},
		{1e-45, 0x00000000},
		{2e-45, 0x00000001},
		{3e-45, 0x00000002},
		{4e-45, 0x00000003},
		{5e-45, 0x00000004},
		{6e-45, 0x00000004},
		{7e-45, 0x00000005},
		{8e-45, 0x00000006},
		{9e-45, 0x00000006},
		{1.0e-44, 0x00000007},
		{1.1e-44, 0x00000008},
		{1.2e-44, 0x00000009},
	} {
		got := math.Float32bits(t.value)
		want := t.bits
		if got != want {
			panic(fmt.Sprintf("bits(%g) = 0x%08x; want 0x%08x", t.value, got, want))
		}
	}
}

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

func float32Encoder(enc *encoder, p unsafe.Pointer) error {
	bs := enc.buf[:4]
	v := (*float32)(p)
	enc.order.PutUint32(bs, math.Float32bits(*v))
	_, err := enc.Write(bs)
	return err
}

示例#27
文件: .main.go 项目: richiebful/ptime

func byteList(byteOrder binary.ByteOrder, values []float32) []byte {
	le := false
	switch byteOrder {
	case binary.BigEndian:
	case binary.LittleEndian:
		le = true
	default:
		panic(fmt.Sprintf("invalid byte order %v", byteOrder))
	}

	b := make([]byte, 4*len(values))

	for i, v := range values {
		u := math.Float32bits(v)
		if le {
			b[4*i+0] = byte(u >> 0)
			b[4*i+1] = byte(u >> 8)
			b[4*i+2] = byte(u >> 16)
			b[4*i+3] = byte(u >> 24)
		} else {
			b[4*i+0] = byte(u >> 24)
			b[4*i+1] = byte(u >> 16)
			b[4*i+2] = byte(u >> 8)
			b[4*i+3] = byte(u >> 0)
		}
	}
	return b
}

示例#28
文件: plugin.pb.go 项目: katarzyna-z/snap

func _Metric_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
	m := msg.(*Metric)
	// data
	switch x := m.Data.(type) {
	case *Metric_StringData:
		b.EncodeVarint(9<<3 | proto.WireBytes)
		b.EncodeStringBytes(x.StringData)
	case *Metric_Float32Data:
		b.EncodeVarint(10<<3 | proto.WireFixed32)
		b.EncodeFixed32(uint64(math.Float32bits(x.Float32Data)))
	case *Metric_Float64Data:
		b.EncodeVarint(11<<3 | proto.WireFixed64)
		b.EncodeFixed64(math.Float64bits(x.Float64Data))
	case *Metric_Int32Data:
		b.EncodeVarint(12<<3 | proto.WireVarint)
		b.EncodeVarint(uint64(x.Int32Data))
	case *Metric_Int64Data:
		b.EncodeVarint(13<<3 | proto.WireVarint)
		b.EncodeVarint(uint64(x.Int64Data))
	case *Metric_BytesData:
		b.EncodeVarint(14<<3 | proto.WireBytes)
		b.EncodeRawBytes(x.BytesData)
	case *Metric_BoolData:
		t := uint64(0)
		if x.BoolData {
			t = 1
		}
		b.EncodeVarint(15<<3 | proto.WireVarint)
		b.EncodeVarint(t)
	case nil:
	default:
		return fmt.Errorf("Metric.Data has unexpected type %T", x)
	}
	return nil
}

示例#29
文件: conn_write.go 项目: sedzinreri/go-pgsql

func (conn *Conn) writeFloat32(f float32) {
	var buf [4]byte
	b := buf[:]

	binary.BigEndian.PutUint32(b, math.Float32bits(f))
	conn.write(b)
}

示例#30
文件: platform_webgl.go 项目: hajimehoshi/nanovgo

func castFloat32ToByte(vertexes []float32) []byte {
	b := make([]byte, len(vertexes)*4)
	for i, v := range vertexes {
		binary.LittleEndian.PutUint32(b[4*i:], math.Float32bits(v))
	}
	return b
}

展开阅读全文