Alternative to C++11's std::nextafter and std::nexttoward for C++03?

那年仲夏 提交于 2019-12-06 01:22:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!