# Thread: max float

1. ## max float

helloworld.

i run into this problem, trying to get a max value for float, but reach "16777216.000000", if am adding 1, more if I add 10

is this something to do with the registers on CPU?

Code:
```#include <stdio.h>

main()
{
float i;
int j;

i = 0;
j = 0;

while(1)
{
i = i + 1;
j++;

if(j > 10000)
{
printf("%f\n",i);
j= 0;

}
}

}```
thx, for ya help.

2. It's to do with the number of mantissa bits in a float.

At some point, your float becomes so large that your 1 has too little precision to make any difference at all to the result.
From then on, it's like adding zero.

3. Originally Posted by *nick

Code:
```
while(1)
{
i = i + 1;
j++;

if(j > 10000)
{
printf("%f\n",i);
j= 0;

}
}

}```
The while statement must contain a logical expression as

while(logical expression)
{
statement;
..............
}

yours is not a logical expression but a constant, first correct that

4. Originally Posted by rakeshkool27
The while statement must contain a logical expression as

Code:
```while(logical expression)
{
statement;
..............
}```
yours is not a logical expression but a constant, first correct that
That has no bearing on the results observed by the OP whatsoever. The while statement must simply contain an expression that can be evaluated in a logical true/false context and as such an integer constant fulfills that role.

5. Originally Posted by rakeshkool27
The while statement must contain a logical expression as

while(logical expression)

yours is not a logical expression but a constant, first correct that
while(1) is fine if he wants an infinite loop. And he wants an infinite loop here...

6. You can just use limits.h and print FLT_MAX value.

But if you want to compute it, you could do this;

Code:
```/* Float value got by direct computation */
int i, n;
float fnum = 2.0;
n = 34;

do {
fnum *= --n;

if(n % 10 == 0)
printf("\nThe float is now: %e", fnum);
}while(n > 1);
printf("\nThe float is now: %e\n\n", fnum);

printf("Max value to reach: %f\n", FLT_MAX);
while(fnum < FLT_MAX) {
fnum *= 1.00000006; /* it works well for any value from 1.0000006 to 1.0000009 */
}
printf("The float is now:   %f\n\n", fnum);```
You need to use multiplying since adding wont work (Salem wrote why adding wont work).

First you increase the variable close to FLT_MAX by multiplying it with itself 34 times: 2 * 34 * 33 * 32... until 1. This is because you dont wanna be waiting long time before it reaches close to FLT_MAX value.

And then multiply that value with any number from 1.00000006 to 1.00000009 and you should get FLT_MAX value. Thats atleast how it worked for me.

Popular pages Recent additions