So tell us the platform you are implementing for. But surely it has IEEE754 floating-point? and therefore CPU instructions that do exactly what you want? or rather what the C++ standard wants?