Nerethar
Nerethar

Reputation: 347

Float in c++ smaller range than IEEE 754

I try to make the following division: 1/16777216, what is equal to 5.96046448e-8 but this:

printf("number: %f \n", 1.0f / 16777216.0f);      

allways gives me 0.00000 instead of the answer I would expect. I looked up the ranges, because I thought well, that might be a problem that float is simply to smal to handle such a number, but IEEE 754 states it to be ±1.18×10−38.

Am I missing something and thats why the result not the expected one?

Upvotes: 2

Views: 235

Answers (1)

Dietmar Kühl
Dietmar Kühl

Reputation: 153965

When using fixed formatting (%f) you get a format with a decimal point and up to 6 digits. Since the value you used rounds to a value smaller than 0.000001 it seems reasonable to have 0.000000 printed. You can either use more digits (I think using %.10f but I'm not that good at <stdio.h> format specifiers) or you change the format to use either scientific notation (%e) or the "better" of both options (%g).

Upvotes: 9

Related Questions