本文介绍了替代C ++ 11的std :: nextafter和std :: nexttoward的C ++ 03?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

正如标题所述,C ++ 11的数学函数库提供了我所使用的功能,以便找到针对特定值的下一个浮点值。



除了拉出std库的代码(我可能不得不诉诸),任何替代方法这样做与C ++ 03(使用GCC 4.4.6)?

  float input = 3.15; 

uint32_t tmp;

unsigned char * p = reinterpret_cast< unsigned char *>(& tmp);
unsigned char * q = reinterpret_cast< unsigned char *>(& input);

p [0] = q [0]; p [1] = q [1]; p [2] = q [2]。 p [3] = q [3]; // endianness?

++ tmp;

q [0] = p [0]; q [1] = p [1]; q [2] = p [2]; q [3] = p [3];

返回输入;

当然要注意零,NaN和无穷大。


As the title says, the functionality I'm after is provided by C++11's math libraries to find the next floating point value towards a particular value.

Aside from pulling the code out of the std library (which I may have to resort to), any alternatives to do this with C++03 (using GCC 4.4.6)?

解决方案

Platform dependently, assuming IEEE754, and modulo endianness, you can store the data of the floating point number in an integer, increment by one, and retrieve the result:

float input = 3.15;

uint32_t tmp;

unsigned char * p = reinterpret_cast<unsigned char *>(&tmp);
unsigned char * q = reinterpret_cast<unsigned char *>(&input);

p[0] = q[0]; p[1] = q[1]; p[2] = q[2]; p[3] = q[3];  // endianness?!

++tmp;

q[0] = p[0]; q[1] = p[1]; q[2] = p[2]; q[3] = p[3];

return input;

Beware of zeros, NaNs and infinities, of course.

这篇关于替代C ++ 11的std :: nextafter和std :: nexttoward的C ++ 03?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:38