I was doing some rounding calculations and happened upon a question. How can I express the highest quantity less than 1 for a given floating point type?
That is, how I write/represent value x
such that x < 1, x + y >= 1
for any y > 0
.
In fractions this would be x = (q-1)/q
where q
is the precision of the type. For example, if you are counting in 1/999
increments then x = 998/999
.
For a given type (float, double, long double), how could one express the value x
in code?
I also wonder if such a value actually exists for all values of y
. That is, as y's
exponent gets smaller perhaps the relation doesn't hold anymore. So an answer with some range restriction on y
is also acceptable. (The value of x
I want still does exist, the relationship may just not properly express it.)
A Floating Point number usually has a decimal point. This means that 0, 3.14, 6.5, and -125.5 are Floating Point numbers.
So how many “normal” non-zero numbers are there between 0 and 1? The negative exponents range from -1 all the way to -126. In each case, we have 223 distinct floating-point numbers because the mantissa is made of 23 bits. So we have 126 x 223 normal floating-point numbers in [0,1).
f = realmax returns the largest finite floating-point number in IEEE® double precision. This is equal to (2-2^(-52))*2^1023 . f = realmax( precision ) returns the largest finite floating-point number in IEEE single or double precision.
C99 defines nextafter()
function. Use it like
#include <math.h>
double under_one = nextafter(1, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With