0 Star 0 Fork 650

李明/gf

forked from John/gf 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
float32.go 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
package gtype
import (
"sync/atomic"
"gitee.com/johng/gf/g/encoding/gbinary"
)
type Float32 struct {
val uint32
}
func NewFloat32(value...float32) *Float32 {
if len(value) > 0 {
return &Float32{ val : float32ToUint32InBits(value[0]) }
}
return &Float32{}
}
func (t *Float32)Set(value float32) {
atomic.StoreUint32(&t.val, float32ToUint32InBits(value) )
}
func (t *Float32)Val() float32 {
return uint32ToFloat32InBits(atomic.LoadUint32(&t.val))
}
func (t *Float32)Add(delta float32) float32 {
return uint32ToFloat32InBits(atomic.AddUint32(&t.val, float32ToUint32InBits(delta)))
}
// 通过二进制的方式将float32转换为uint32(都是32bits)
func float32ToUint32InBits(value float32) uint32 {
b := gbinary.Encode(value)
i := gbinary.DecodeToUint32(b)
return i
}
// 通过二进制的方式将uint32转换为float32(都是32bits)
func uint32ToFloat32InBits(value uint32) float32 {
b := gbinary.Encode(value)
f := gbinary.DecodeToFloat32(b)
return f
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/limingsky/gf.git
git@gitee.com:limingsky/gf.git
limingsky
gf
gf
c9a36a8224f1

搜索帮助