Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 477
Nerethar Avatar asked Dec 04 '25 13:12

Nerethar


1 Answers

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

like image 165
Dietmar Kühl Avatar answered Dec 07 '25 03:12

Dietmar Kühl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!