福哥答案2020-08-10:
浮点数=符号位+阶码+尾数,阶码加1就是浮点数的2倍值。
代码用golang编写,如下:
package test33_add
import (
"fmt"
"math"
"testing"
)
/*
//https://www.bbsmax.com/A/6pdDX7gRdw/
//https://www.bbsmax.com/A/8Bz8GWVLdx/
//http://blog.sina.com.cn/s/blog_60670ed80100ro3l.html
IEEE754标准的浮点数采用以下四种基本形式:
1.单精度(32位):E=8位,M=23位;
2.拓展单精度:E>=11位,M=31位;
3.双精度(64位):E=11位,M=52位;
4.拓展双精度:E>=15位,M>=63位。
*/
//go test -v -test.run TestAdd
func TestAdd(t *testing.T) {
//9.625
//以上面的9.625为例,其规范浮点数表达为 1.001101*(2^3)
fmt.Println("根据符号位,阶数,尾数,打印9.625")
fmt.Println(GetFloat32(0, 0b10000010, 0b00110100000000000000000))
fmt.Println(GetFloat64(0, 0b10000000010, 0b0011010000000000000000000000000000000000000000000000))
fmt.Println("------------------------")
fmt.Println("打印2倍数:")
fmt.Println(GetTwoFloat32(1.1))
fmt.Println(GetTwoFloat64(1.1))
return
}
//循环,不用加号求两数之和
func Add32(a uint32, b uint32) uint32 {
xor := uint32(0) //按位异或
c := uint32(1) //按位与,左移一位。初始值为1,是为了进入for循环
for c != 0 {
xor = a ^ b //按位异或
c = a & b << 1 //按位与,左移一位。初始值为了,是为了进入for循环
a = xor
b = c
}
return a
}
//递归,不用加号求两数之和
func Add64(a uint64, b uint64) uint64 {
if b == 0 {
return a
} else {
return Add64(a^b, a&b<<1)
}
}
//根据符号位,阶码,尾数求浮点数
func GetFloat64(s uint64, exp uint64, frac uint64) float64 {
s = s << 63
exp = exp & 0b00000111_11111111 << 52
frac = frac & 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111
return math.Float64frombits(s | exp | frac)
}
//根据符号位,阶码,尾数求浮点数
func GetFloat32(s uint32, exp uint32, frac uint32) float32 {
s = s << 31
exp = exp & 0b00000111_11111111 << 23
frac = frac & 0b00000000_01111111_11111111_11111111
return math.Float32frombits(s | exp | frac)
}
//根据浮点数求符号位,阶码,尾数
func FromFloat64(f float64) (uint64, uint64, uint64) {
u := math.Float64bits(f)
return u >> 63, u >> 52 & 0b00000111_11111111, u & 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111
}
//根据浮点数求符号位,阶码,尾数
func FromFloat32(f float32) (uint32, uint32, uint32) {
u := math.Float32bits(f)
return u >> 31, u >> 23 & 0b00000111_11111111, u & 0b00000000_01111111_11111111_11111111
}
//单精度2倍
func GetTwoFloat32(f float32) float32 {
s, exp, frac := FromFloat32(f)
if exp == 0 && frac == 0 { //0值
return 0
}
if exp == 0x11111111 { //超过边界
return f
}
if exp == 0x11111110 { //*2超过边界
return GetFloat32(s, 0x11111111, 0)
}
exp = Add32(exp, 1)
return GetFloat32(s, exp, frac)
}
//双精度2倍
func GetTwoFloat64(f float64) float64 {
s, exp, frac := FromFloat64(f)
if exp == 0 && frac == 0 { //0值
return 0
}
if exp == 0x111_11111111 { //超过边界
return f
}
if exp == 0x111_11111110 { //*2超过边界
return GetFloat64(s, 0x111_11111111, 0)
}
exp = Add64(exp, 1)
return GetFloat64(s, exp, frac)
}
敲命令 go test -v -test.run TestAdd 如下,结果如下:
评论