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);
}
Printable View
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.Quote:
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.Quote:
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."Quote:
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.
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.Quote:
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
>> 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;
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.
Again not too clear.
Anyway, for starters, having looked up the matter in my (rather old I might add) book on
C it states that division is never performed on a float, at least not without converting it
to a double first so putting an f there would seem to be a waste of yours, and possible
the compilers time.
And even if it did do the division on floats why go for a less accurate result?
Because again, you're cramming the result into a smaller variable with a great deal less precision. There is only so much rounding you can do until the value is just completely wrong. It depends what's important, etc.
I mean if you think the compiler cares you assign something really precise, you're wrong:
Rounding obviously happened (out of necessity in this case) but you shouldn't make it a complete surprise: you might decide, for a time, to treat doubles like floats, and floats like doubles, and all that rounding takes it's toll on the overall precision of your final answer [/Regis]. You can't just extend pricision back to what it once was after you've messed with it.Code:#include <stdio.h>
/** my calculators answer for pi: much larger than any float or double **/
#define PI_DB 3.1415926535897932384626433832795
#define PI_FL 3.1415926535897932384626433832795f
int main ()
{
double pi = PI_FL;
printf( "PI_FL = %1.6f\n", PI_FL );
printf( "PI_DB = %1.15g\n", PI_DB );
printf( "pi (promoted): = %1.10g\n", pi );
return 0;
}
And evidently it's not easy to keep track of: Plenty of people aren't aware that point-anything is double by the compiler. That's why people can be finicky about decimal numbers, because they have to be when it matters. Fortunately, you don't see much of these issues creeping into financial software, though it might be easy for accountants to lay blame. :D
And yet this is correct and all of the examples you (esbo) have shown are incorrect. Why do you think it is wrong to specify floats as 1.0f and doubles as 1.0? Second, where did you learn this as being right:Quote:
And never in a million years (I hope) would I write
Code:
float x = 4.0f;
double y = 4.0;
And how does that differ from:Code:float x = 4;
double y = 4;
The last example shows integral data types being assigned integral values. The first two examples show floating point data types (float and double) being assigned integral values. Sure the compiler will convert but this will bite you someday if you continue to rely on the compiler for the conversion. It not only obfuscates the code, forces a conversion where perhaps none is needed if all vars are already using the same type, but also shows a bit of a lack of understanding about floating point data types. Take this sample:Code:int x = 4;
unsigned int x = 4
unsigned long x = 4;
__int64 x = 4;
Sample 1
Sample 2Code:float x = 4;
float y = 6;
float total = x + y;
Sample 3Code:float x = 4.0;
float y = 6;
float total = x + y;
Sample 4Code:float x = 4.0f;
float y = 6.0f;
float total = x + y;
Samples 1 and 2 are sloppy and inconsistent. There are times when variable types are not identical and casting is necessary but forcing a cast where none is needed is a bad practice.Code:double x = 4.0;
double y = 6.0;
double total = x + y;
If you write any type of modern 3D graphics code using OpenGL or Direct3D and you do all your calculations in double precision and then stuff that into a float.....what you get on screen will be very different from what you expect. If you fail to maintain your data types or toss them around sloppily your code will certainly crash and bring down the entire app. It only takes several matrix multiplies to accumulate a large floating point error even when data types are consistent much less when a lot of casting is taking place.
I tend to specify floats using 1.f for example. Do people even know that is also valid syntax?
Nope. Interesting. I don't think I'll switch though.Quote:
I tend to specify floats using 1.f for example. Do people even know that is also valid syntax?
Well aside from all the other stuff in this thread I can honestly say I can never
even recall seeing the 'f' notation used apart from on this site.
Is it a relatively new 'development'?
Maybe I just got out whilst the going, was good so to speak :)
Or at least slightly better anyway.....
No, it's pretty old.Quote:
Well aside from all the other stuff in this thread I can honestly say I can never
even recall seeing the 'f' notation used apart from on this site.
Is it a relatively new 'development'?
Well I don't think I will ever use it, I have never had a problem with floats so far
and I can't see me ever having to use it in the future, it is surplus to requirements
as far as I am concerned.
I think it might have been introduced in ANSI C, those standards commities loved
introducing loads of stuff you never needed to use. Bit like the RS232 connector,
25 pins for serial communications - what a joke, I doubt anyone every used more then
5.
Well, you aren't forced to use floats, but if you do, use .f and even if you don't use it, even if you just give advance to someone, tell them to use .f of they use floats.