Hey guys! I think I must be going crazy...
gives me 0.0000. Why? How can I get the answer?Code:int main() { float test = 48 / 512; printf("%f", test); return (0); }
Hey guys! I think I must be going crazy...
gives me 0.0000. Why? How can I get the answer?Code:int main() { float test = 48 / 512; printf("%f", test); return (0); }
48 / 512 is less than 0. Both 48 and 512 are integers so the whole division is done on integers and the precision is lopped off leaving you with 0. Change one or both of the numbers to float and it'll work.
Code:int main() { float test = 48.0f / 512.0f; printf("%f", test); return (0); }
You don't need to add the f at the end of the value, a decimal point is enough to make
it work. Infact if you miss out the decimal point it won't compile on my machine!!
Code:int main() { float test = 48f/512f; printf("%f", test); return (0); }So I am not sure what the f does,Code:invalid sufix 'f' on integer constant,
1.0 is a double. 1.0f is a float.
Leaving it out will do integer division, hence giving you 0. That is no error or warning.
If you add decimals, you get a double, but some compilers won't warn about that.
So again, as others stated, it makes the number a float. It needs to be 1.0f, not 1f.So I am not sure what the f does,
And for once, you used "int main" and "return 0". Congratulations! Must have been hard on you.
But that was beyond the point. The point was that if you didn't do it, it would be integer division.
Of course. A float is a floating point number, after all.It only make it a float if it has a decimal point, otherwise it just gives you a compiler error.
Yes, but you could have removed them, to better suit your "tastes."Not really, I am quite good at cut and paste.
The f changes a floating point number to a float, it won't change an integer to a float.
A double is a floating point number.
My 'taste' is just to copy what is already there, I won't change something if it compiles
and works.
I don't really see the point in having an f there at all, it seems rather pointless.
Last edited by esbo; 02-27-2008 at 06:43 PM.
Because it is a very bad habit to get into. If you wish to use float then specify the f otherwise do not specify the f. It also informs other programmers that you are using float's and not doubles. There is a big difference between a double and a float. Just because 'it works' does not mean it's correct.The f changes a floating point number to a float, it won't change an integer to a float.
A double is a floating point number.
My 'taste' is just to copy what is already there, I won't change something if it compiles
and works.
I don't really see the point in having an f there at all, it seems rather pointless.
This:
This means nothing to me except that the person who programmed it either was very tired that day or doesn't understand floating point values. Did they want 4.0f or is it a typo?Code:float x = 4f;
This:
Means that x is a float and y is a double.Code:float x = 4.0f; double y = 4.0;
So you ask why use the f? Here is an example:
What do you suppose happens in this assignment?Code:float x = some_angle * 180.0 / 3.14159f
Last edited by VirtualAce; 02-27-2008 at 07:10 PM.
>> Means that x is a float and y is a double.
Maybe, but it's an issue in precision: in Bubba's example, we're using a floating-point constant. Had he left off the trailing f, we would have done double floating point division, and the answer would have been rounded to fit in a float variable.
As you should know, the quality in answers is quite different when finer precision matters. So, it's at least fairly important to avoid conversion issues while you're using constants like pi, or some other number.
Also I have been getting into bad habits doing this.
In 'real life' I would never write:-
I would write:-Code:float x = 4.0; double y = 4.0;
Means that x is a float, y is a doubleCode:float x = 4; double y = 4;
And never in a million years (I hope) would I write
Code:float x = 4.0f; double y = 4.0;
Last edited by esbo; 02-27-2008 at 08:58 PM.
float x = 4;
double y = 4;
Nothing wrong with promotion (as long as it is compatible). But when you convert to a lesser data type, you might lose your data.
>> And exactly what is wrong with [rounding]?
>> what point it is your are trying to make?
It is completely reasonable to avoid possible loss of data.
Last edited by whiteflags; 02-27-2008 at 09:01 PM.