The largest representable float
value less than 1 is returned by nexttowardf(1, -INFINITY)
.
This will generally have a different fraction part than, for example, the largest representable float
value less than 2, which is nexttowardf(2, -INFINITY)
. This is because numbers of different magnitudes generally have different numbers of bits available for the fraction part (because some of the bits are used for the integer part). Large numbers have zero bits for the fraction part.
When float
is an IEEE-754 32-bit binary floating-point value, which is common in modern implementations, the largest float
below 1 is 0.999999940395355224609375. When the routines that convert decimal numerals to float
are good quality and the rounding mode is to-nearest (the common default), then the point where numbers switch from rounding to 0.999999940395355224609375 to rounding to 1 is halfway between those two values (and the exact midpoint will round to 1).
Properly converting decimal numerals to binary floating-point is complicated. It is a solved problem and there are academic papers about it, but you should generally rely on existing library code, if it is doing the job properly. Doing it correctly yourself will require a significant investment of time.