gpt4 book ai didi

C++仅使用整数数学将 float 转换为十六进制

转载 作者:行者123 更新时间:2023-11-28 06:24:19 25 4
gpt4 key购买 nike

我需要将应被视为 float 的整数转换为其等效的十六进制数。

例如:

Float To Hex

1 = 0x3f800000

2 = 0x40000000

12345 = 0x4640e400

它永远是整数,而不是分数,例如 0.5。这可以通过内存分配或格式化功能来完成,但在使用它的情况下,它没有内存访问权限,也根本没有 API 调用。

我试过这个想法,但根本行不通 http://bytes.com/topic/c/answers/219928-how-convert-float-hex#post886069

最佳答案

函数floatAsUint_s()下面重新解释了 32 位 IEEE-754 float作为unsigned int对于 |x| 的任何输入 x在 [1, 2128) 中,或者为零。信息摘自 float一次一点,结果 unsigned int一次一位地从这些位构建。如果输入和输出都驻留在处理器寄存器而不是内存中,则在重新解释过程中不需要额外的内存。

/* re-interpret IEEE-754 float x, |x| in [1, 2**128) or 0, as unsigned int */
unsigned int floatAsUint_s (float x)
{
unsigned int i;

/* extract sign bit, proceed with absolute value */
i = (((x == 0.0f) ? (1.0f / x) : x) < 0.0f) ? 0x80000000 : 0x00000000;
x = (((x == 0.0f) ? (1.0f / x) : x) < 0.0f) ? -x : x;
/* extract exponent, which is positive per specification */
if (x >= 1.84467441e19f) { x /= 1.84467441e19f; i |= 1 << 29; }
if (x >= 4.29496730e9f) { x /= 4.29496730e9f; i |= 1 << 28; }
if (x >= 65536.0f) { x /= 65536.0f; i |= 1 << 27; }
if (x >= 256.0f) { x /= 256.0f; i |= 1 << 26; }
if (x >= 16.0f) { x /= 16.0f; i |= 1 << 25; }
if (x >= 4.0f) { x /= 4.0f; i |= 1 << 24; }
if (x >= 2.0f) { x /= 2.0f; i |= 1 << 23; }
i += (x == 0.0f) ? 0 : (127 << 23); // add IEEE-754 specified exponent bias
/* extract mantissa */
x = x - 1.0f; // remove hidden bit
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 22; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 21; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 20; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 19; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 18; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 17; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 16; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 15; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 14; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 13; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 12; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 11; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 10; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 9; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 8; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 7; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 6; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 5; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 4; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 3; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 2; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 1; }
x = x + x; if (x >= 1.0f) { x -= 1.0f; i |= 1 << 0; }
return i;
}

/* reference implementation */
unsigned int floatAsUint (float a)
{
unsigned int i;
unsigned char *ap = (unsigned char *)&a, *ip = (unsigned char*)&i;
for (unsigned int c = 0; c < sizeof (i); c++) {
*ip++ = *ap++;
}
return i;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (void)
{
unsigned int res, ref;
float s = -1.0f;
while (s < 2.0f) {
float x = 0.0f;
while (x < 3.40282346e38f) {
ref = floatAsUint (s * x);
res = floatAsUint_s (s * x);
if (ref != res) {
printf ("error @ % 15.8e: res= %08x ref=%08x\n", x, res, ref);
exit (EXIT_FAILURE);
}
x = (x == 0.0f) ? 1.0f : nextafterf (x, 3.40282346e38f);
}
s += 2.0f;
}
return EXIT_SUCCESS;
}

问题中规范的另一种解释如下:给定一个 int x, |x|在 [0, 224] 中,生成 x 值的 IEEE-754 单精度编码,存储在 uint32_t 中.仅使用整数运算进行转换。

正非零整数 <= 224 的位模式与 IEEE-754 float 的尾数(隐藏位已恢复)的位模式相同它被转换为,仅适本地移动。因此,我们需要通过左移整数来进行归一化,直到其最高有效 1 位位于隐藏尾数位的位置,即第 23 位。归一化所需的移位次数告诉我们整数的二次幂大小,从而确定 float 的指数。我们需要记住添加 IEEE-754 规定的指数偏差,然后将符号、指数和尾数部分结合起来以获得最终结果。

函数make_float_s()在下面的代码中实现了上述算法。

#include <stdint.h>

/* For |a| in [0,2**24], generate IEEE-754 float encoding with same value */
uint32_t make_float_s (int a)
{
uint32_t i;
int e = 0;

i = (a < 0) ? 0x80000000 : 0x00000000; // sign bit
if (a) {
a = (a < 0) ? -a : a;
while (a < 0x00800000) { // normalize mantissa
e++;
a = a + a;
}
e = 127 + (22 - e); // determine biased exponent
i += (e << 23) + a; // combine sign, exponent, mantissa
}
return i;
}

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

uint32_t float_as_uint (float a)
{
uint32_t i;
memcpy (&i, &a, sizeof(i));
return i;
}

/* reference function */
uint32_t make_float (int a)
{
return float_as_uint ((float)a);
}

int main (void)
{
uint32_t res, ref;
int a, s;

a=1; printf("%d encoded as IEEE-754 float: %08x\n", a, make_float_s(a));
a=2; printf("%d encoded as IEEE-754 float: %08x\n", a, make_float_s(a));
a=12345; printf("%d encoded as IEEE-754 float: %08x\n", a, make_float_s(a));

s = -1;
while (s < 2) {
a = 0;
while (a <= 16777216) {
res = make_float_s (s * a);
ref = make_float (s * a);
if (res != ref) {
printf ("error @ % 7d: res=%08x ref=%08x\n", s * a, res, ref);
exit (EXIT_FAILURE);
}
a++;
}
s += 2;
}
return EXIT_SUCCESS;
}

关于C++仅使用整数数学将 float 转换为十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com