简体   繁体   English

将uint32读取为没有溢出的浮点型吗?

[英]Read an uint32 as a float without overflow?

I'm working on a program that does that kind of things : it takes an array of float values, and encodes the values three by three in a base64 string. 我正在研究一种执行此类操作的程序:它需要一个浮点值数组,并在base64字符串中将值三乘三编码。 When the whole array is encoded, if there are some values left (number < 3), it encodes them and adds the necessary '=' (for more informations, see here ). 对整个数组进行编码后,如果还剩下一些值(数字<3),则会对其进行编码并添加必要的'=' (有关更多信息,请参见此处 )。

Anyway, here is not the problem. 无论如何,这不是问题。 My problem is that, at the very beginning of this base64 string, I need to indicate the number of floats following, but as an uint32. 我的问题是,在此base64字符串的开头,我需要指出后面的浮点数,但要作为uint32。 However, I'm forced to use my function using floats... So I have to use a hack, and initialize a pointer to an uint32, but read it as a float. 但是,我被迫使用浮点数来使用我的函数...因此,我必须使用hack,并初始化指向uint32的指针,但将其读取为浮点数。

Here is how I do it: 这是我的方法:

uint32_t *ivalue = (uint32_t *)malloc(sizeof (uint32_t));
float *fvalue = (float *)ivalue;
// (...) some code
*ivalue = 2 * sizeof (float); // The value I want, 2 * 4 in this case (on my computer)
tempoCoor.push_back(*fvalue);

tempoCoor is a std::vector I fill, and when the size is 3, I encode its content and empty it. tempoCoor是我填充的std :: vector,当大小为3时,我对其内容进行编码并将其清空。

However, there is a problem with my hackish method... When I compute values smaller than 256 (I think this is this value), everything is fine, but not when my values are greater or equal than 256. Why is that ? 但是,我的hackish方法存在问题...当我计算出小于256的值(我认为这是该值)时,一切都很好,但是当我的值大于或等于256时不是。这是为什么? How could I fix it ? 我该如何解决?

If I was not clear in any way, please ask me more details. 如果我不清楚,请向我询问更多详细信息。 I can explain more if needed. 如果需要,我可以解释更多。

Ok, so here some explanations on what is wrong with that code: 好的,所以这里有一些代码错误的解释:
1. Why are you using malloc in C++? 1.为什么在C ++中使用malloc It is a bad habit and only in some few special cases appropriate. 这是一个坏习惯,仅在一些特殊情况下才适用。 Use new instead! 改用new (Also don't cast malloc [in C] or at least use C++ casts) (也不要在[C]中强制转换malloc或至少使用C ++强制转换)
2. Why are you even allocating memory on the heap? 2.为什么还要在堆上分配内存? it seems completely unnecessary in this case. 在这种情况下,似乎完全没有必要。
3. Don't use C-Style casts in C++ code - C++ casts ( static_cast , etc.) are safer and contain some error checking. 3.不要在C ++代码中使用C-Style强制转换-C ++强制转换( static_cast等)更安全,并且包含一些错误检查。

So here is how this code might look like. 所以这是这段代码的样子。

uint32_t ivalue = 2 * sizeof(float); //simply assigning the value  
// ... some Code
tempoCoor.push_back(static_cast<float>(ivalue)); //using proper C++ cast to convert

As you were using memory on the heap and accessing it directly, by reinterpreting a pointer (which would be equivalent to reinterpret_cast ) you are using the data which was written there as if it was a float, which it obviously wasn't. 当您在堆上使用内存并直接访问它时,通过重新解释一个指针(相当于reinterpret_cast ),您正在使用写入那里的数据,就好像它是一个浮点数,显然不是。 Once your values got big enough some of the bits were interpreted as being part of the exponent and you received wrong values. 一旦您的值足够大,某些位将被解释为指数的一部分,并且您收到了错误的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM