Floating point 在0.0和15.0之间浮动至uint8
我正在编写的代码在某些时候需要从Floating point 在0.0和15.0之间浮动至uint8,floating-point,integer,precision,Floating Point,Integer,Precision,我正在编写的代码在某些时候需要从浮点转换为uint8\t。最酷的部分是,浮动已经具有与uint8\u t相同的值,例如15.0=>15,或4.0=>4 我现在需要编写一个函数,将32位浮点转换为32位无符号整数,这样浮点和整数的值就相同了。因此,只能设置整数的前4位 以下是我的工作内容: Binary representation for: 0.0 00000000000000000000000000000000 => 0000 Binary representation for: 1
浮点
转换为uint8\t
。最酷的部分是,浮动已经具有与uint8\u t
相同的值,例如15.0
=>15
,或4.0
=>4
我现在需要编写一个函数,将32位浮点
转换为32位无符号整数
,这样浮点和整数的值就相同了。因此,只能设置整数的前4位
以下是我的工作内容:
Binary representation for: 0.0 00000000000000000000000000000000 => 0000
Binary representation for: 1.0 00011111110000000000000000000000 => 0001
Binary representation for: 2.0 00100000000000000000000000000000 => 0010
Binary representation for: 3.0 00100000001000000000000000000000 => 0011
Binary representation for: 4.0 00100000010000000000000000000000 => 0100
Binary representation for: 5.0 00100000010100000000000000000000 => 0101
Binary representation for: 6.0 00100000011000000000000000000000 => 0110
Binary representation for: 7.0 00100000011100000000000000000000 => 0111
Binary representation for: 8.0 00100000100000000000000000000000 => ....
Binary representation for: 9.0 00100000100010000000000000000000
Binary representation for: 10.0 00100000100100000000000000000000
Binary representation for: 11.0 00100000100110000000000000000000
Binary representation for: 12.0 00100000101000000000000000000000
Binary representation for: 13.0 00100000101010000000000000000000
Binary representation for: 14.0 00100000101100000000000000000000
Binary representation for: 15.0 00100000101110000000000000000000
你的问题是什么?大多数语言都有一个内置函数来实现这一点。你为什么不能用这个?或者这纯粹是理论上的吗?啊,你能想出一个很酷的方法来操纵这些位,使其在4个最低有效位上有整数表示吗?我知道查找表可以工作,但有什么好办法吗?我正在编写AVX Intrinsic来编写性能代码,所以转换到uint8_t不会给我任何加速,因为我必须在特定情况下编写标量代码。当然,只要将浮点转换为int就可以了。你觉得这太慢了?