c++ - Alternative to C++11's std::nextafter and std::nexttoward for C++03? -
as title says, functionality i'm after provided c++11's math libraries find next floating point value towards particular value.
aside pulling code out of std library (which may have resort to), alternatives c++03 (using gcc 4.4.6)?
platform dependently, assuming ieee754, , modulo endianness, can store data of floating point number in integer, increment one, , retrieve 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 , infinities, of course.
Comments
Post a Comment