what is f?
Well this seems like a really noob question and its not exactly the easiest thing to google for but in Visual Studio I see this used in a few projects and I dont know what it is. It seems to be something to do with floating point numbers. For example:
Dim number As Single = 1.0f
so what is f?
f signifies the number is a float, not a double. A double stores 64-bits of precision and a float stores 32-bit.
Quote:Original post by NegativeGeForce
f signifies the number is a float, not a double. A double stores 64-bits of precision and a float stores 32-bit.
That's partially correct. A double is guaranteed to be at least the size of a float, but it does not have to be greater than a float. Plus, a float does not have to be 32 bits.
Just remember that a float is usually, but not always, 32 bits. A double is usually64 bits.
But yes, placing an f after the number tells the compiler that the number is a float and not a double, because without the f, the compiler by default handles it as a double.
Quote:Original post by MikeTacularThe original poster posted a VB snippet. If that's VB.NET then Single is always 32-bits, and Double always 64-bits.Quote:Original post by NegativeGeForce
f signifies the number is a float, not a double. A double stores 64-bits of precision and a float stores 32-bit.
That's partially correct. A double is guaranteed to be at least the size of a float, but it does not have to be greater than a float. Plus, a float does not have to be 32 bits.
Just remember that a float is usually, but not always, 32 bits. A double is usually64 bits.
But yes, placing an f after the number tells the compiler that the number is a float and not a double, because without the f, the compiler by default handles it as a double.
Is it necessary to use the f even when you're assigning a value to a float, like this:
float num = 3.1;
In what circumstances would you need to add the f?
float num = 3.1;
In what circumstances would you need to add the f?
You need to use the f to tell the compiler that your constant is to be created as float. If you leave the f, but put a comma like you did, it is a double.
This snippet should actually give you a warning about precision loss.
This snippet should actually give you a warning about precision loss.
So what are the exact implications of using the f and not using it? Also in which scenarios would you use it and not use it?
Also are you saying you lose precision by not including the f? Isnt a double more precise then a single? or is it that the f makes it store a number like 1.00000 instead of just 1?
Also are you saying you lose precision by not including the f? Isnt a double more precise then a single? or is it that the f makes it store a number like 1.00000 instead of just 1?
float and double have different precision. double has about twice the precision as float does.
Now since you dim'd as float anyway there's no use in providing a double constant, the compiler will convert it anyway (and there's a precision loss coming in). Say you'd set pi to the constant with 10 digits. float won't be able to store all the digits. double can. The compiler is issuing the warning so that you are aware of that.
You can of course use double's throughout as well, no problem with that. It's a tradeoff you can choose: Smaller in size, but less precision (float) or bigger in size and more precision.
Most of the time it depends on where you want to use the numbers.
Now since you dim'd as float anyway there's no use in providing a double constant, the compiler will convert it anyway (and there's a precision loss coming in). Say you'd set pi to the constant with 10 digits. float won't be able to store all the digits. double can. The compiler is issuing the warning so that you are aware of that.
You can of course use double's throughout as well, no problem with that. It's a tradeoff you can choose: Smaller in size, but less precision (float) or bigger in size and more precision.
Most of the time it depends on where you want to use the numbers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement