Is there a way to programmatically get the double that is closest to 1.0, but isn't actually 1.0?
One hacky way to do this would be to memcpy the double to a same-sized integer, and then subtract one. The way IEEE754 floating-point formats work, this would end up decreasing the exponent by one while changing the fractional part from all zeros (1.000000000000) to all ones (1.111111111111). However there exist machines where integers are stored little-endian while floating-point is stored big-endian, so that won't always work.
nextafter()
is the only proper way of achieving what he wants. – Denman1.0000...
binary is decrement to0.111111....
and to normalize it, you must shift it to the left:1.11111...
which requires you to decrement the exponent. And then you are 2 ulp away from 1.0. So no, subtracting one from the integral value does NOT give you what is asked here. – Denman