Public Group

# what is f?

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
f signifies the number is a float, not a double. A double stores 64-bits of precision and a float stores 32-bit.

##### Share on other sites
Quote:
 Original post by NegativeGeForcef 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.

##### Share on other sites
Quote:
Original post by MikeTacular
Quote:
 Original post by NegativeGeForcef 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.
The original poster posted a VB snippet. If that's VB.NET then Single is always 32-bits, and Double always 64-bits.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
11
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002304
×