gpt4 book ai didi

c++ - 有符号浮点变量转换为无符号 DWORD

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:51 26 4
gpt4 key购买 nike

我正在尝试将带符号的浮点变量转换为 DWORD...DWORD 将由另一个程序使用,因此 DWORD 变量类型很重要...

首先...可以将带符号的 DWORD 解释为未签名的 DWORD...吗?

另外...我如何将带符号的 float 转换为 DWORD(带符号)...?

最佳答案

I am trying to convert a signed floating point variable to DWORD...the DWORD is to be used by another program so the DWORD variable type is important...

firstly...can a signed DWORD be interpreted as an unsigned DWORD..?

also...how can i convert a signed float to DWORD(signed)..?

DWORD 是由 Windows API 定义的 32 位无符号整数类型。没有“签名的 DWORD”这样的东西。可能你的意思是相应的签名类型。

在 C++ 中,所有浮点类型(floatdoublelong double)都是有符号类型,所以谈论“带符号的 float ”是不寻常的。大概你的意思是它可以是一个负值。将负浮点值转换为无符号整数主要有两种可能性:

  • 好像浮点值,比如-1.23,被转换为有符号整数,然后调整为2的合适倍数 i>n 使其进入无符号整数范围,或者

  • 好像 浮点值,再次说-1.23,调整为 2n 的合适倍数以带来将其转换为无符号整数范围,然后转换为无符号整数类型。

这些过程通常会产生不同的结果,相差 1。但是,对其进行测试,这是 Visual C++ 11.0 和 MinGW g++ 4.7.1 使用的第一个过程。而且我怀疑这是标准强制要求的(例如,C++11 终于对负数的整数除法结果有了明确的规定):

#include <math.h>
#include <windows.h>
using namespace std;

#include <iostream>
DWORD test1()
{
double floatValue = -1.23456;
DWORD const dwValue = floatValue;

cout << dwValue << endl;
return dwValue;
}

DWORD test2()
{
double floatValue = -1.23456;
double reduced = floatValue + (1uLL << 32);
DWORD const dwValue = reduced;

cout << dwValue << endl;
return dwValue;
}

int main()
{
cout << DWORD(-1) << endl;
DWORD const t1 = test1(); cout << 1uLL + DWORD(-1) - t1 << endl;
DWORD const t2 = test2(); cout << 1uLL + DWORD(-1) - t2 << endl;
}

输出,使用 Visual C++ 11.0 和 MinGW g++ 4.7.1:

42949672954294967295142949672942

Generally, the conversion (performed by simple assignment or initialization) loses data.

The compiler may warn about that. One possible way to make it shut up, which may or may not work, is then to use a static_cast to make the conversion explicit. Anyway, be aware that the conversion must lose data in the general case, because in general a floating point value has more bits than a DWORD.

There is one case, however, where a DWORD has enough bits, and that's for a value of type float, which in Windows is 32-bit IEEE.

You can therefore represent any float value as a DWORD value with the same bits, but the connection between numerical values will then seem pretty arbitrary. And whether it will be practically useful depends on your other application. What does it expect, or what can it handle?

#include <windows.h>
#include <iostream>
using namespace std;

DWORD dwordBitsFrom( float const number )
{
return *reinterpret_cast< DWORD const* >( &number );
}

float floatFromBits( DWORD const bits )
{
return *reinterpret_cast< float const* >( &bits );
}

int main()
{
float const original = -1.23;
DWORD const bits = dwordBitsFrom( original );
float const reconstituted = floatFromBits( bits );

cout << original << endl;
cout << bits << endl;
cout << reconstituted << endl;
}

输出,使用 Visual C++ 11 和 g++ 4.7.1(以及任何实际有用的 Windows C++ 编译器):

-1.233214766244-1.23

Note, however, that while this latter conversion is well defined in Windows, the Windows rule is not supported by the C++ standard. From a strictly formal point of view, inappropriately regarding the above code as platform-independent, the above breaks the strict aliasing rules of C++. Which g++ is very happy to inform you about:

[D:\dev\test]> gnuc --strict-aliasing foo.cppfoo.cpp: In function 'DWORD dwordBitsFrom(float)':foo.cpp:7:55: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]foo.cpp: In function 'float floatFromBits(DWORD)':foo.cpp:12:53: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing][D:\dev\test]> _

In order to make silly compilers such as g++ happy, you have to do the conversion via a buffer of bytes. It's pretty annoying, because the warning is all about the compiler detecting that what you have expressed that you want, is not compatible with some very undesirable marginal optimizations that it might apply. And when it can detect that it shouldn't, why can't it just not not do that undesirable thing, instead of warning about its inability to do it? Or, why can't it just not not do it at all? Since nobody wants it.

But anyway, here's code to make g++ shut up:

#include <windows.h>
#include <iostream>
#include <string.h> // memcpy
using namespace std;

static_assert( sizeof( DWORD ) == sizeof( float ), "y DWORD no same size float?" );

int const nBytes = sizeof( DWORD );

DWORD dwordBitsFrom( float const number )
{
char buffer[nBytes];
DWORD result;

memcpy( buffer, &number, nBytes );
memcpy( &result, buffer, nBytes );
return result;
}

float floatFromBits( DWORD const bits )
{
char buffer[nBytes];
float result;

memcpy( buffer, &bits, nBytes );
memcpy( &result, buffer, nBytes );
return result;
}

int main()
{
float const original = -1.23;
DWORD const bits = dwordBitsFrom( original );
float const reconstituted = floatFromBits( bits );

cout << original << endl;
cout << bits << endl;
cout << reconstituted << endl;
}

当然,由于它是更冗长且效率更低的代码,因此人们可能会认为关闭一个愚蠢的编译器(即 g++)是不值得的。就我个人而言,我认为这值得。相反,只需对 g++ 说 -fno-strict-aliasing,并使用 reinterpret_cast,因为这就是它的作用,,我们将它放在语言。

总而言之,如果您的其他程序需要一个数字值,如果可能的话,该值接近原始浮点值,那么只需通过分配或初始化转换为DWORD,可能使用 static_cast 来抑制编译器警告。

如果您的其他程序需要 float 值的 bits,则只需使用 reinterpret_cast 转换。上面的代码显示了如何使用更低效、冗长和容易引起错误的 memcpy 而只是为了取悦 g++ 编译器。但我的建议是,如果这是相关的,那么只需通过 -fno-strict-aliasing 抑制 g++ 警告(另一个使 g++ 编译器符合实用性的有用选项是 -fwrapv ,应该始终使用恕我直言)。

关于c++ - 有符号浮点变量转换为无符号 DWORD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436729/

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