Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express highest floating point quantity that is less than 1

Tags:

c++

c

math

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.)

like image 543
edA-qa mort-ora-y Avatar asked Dec 29 '10 08:12

edA-qa mort-ora-y


People also ask

Is 1 a floating-point number?

A Floating Point number usually has a decimal point. This means that 0, 3.14, 6.5, and -125.5 are Floating Point numbers.

How many floats are there between 0 and 1?

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).

How do you find the largest floating-point value?

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.


1 Answers

C99 defines nextafter() function. Use it like

#include <math.h>
double under_one = nextafter(1, 0);
like image 160
qrdl Avatar answered Oct 14 '22 09:10

qrdl