Sign in to follow this  
gp_s

what is f?

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 this post


Link to post
Share on other sites
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.

Share this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this