I just wanted some information about this.
float n = 2.99944323200023f
What does the f at the end of the literal do? What is it called? Why is it used?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
findicates it’s a floating point literal, not a double literal (which it would implicitly be otherwise.) It hasn’t got a particular technical name that I know of – I tend to call it the “letter suffix” if I need to refer to it specifically, though that’s somewhat arbitrary!For instance:
You could of course do:
…which accomplishes near enough the same thing, but the F is a neater, more concise way of showing it.
Why was double chosen as the default rather than float? Well, these days the memory requirements of a double over a float aren’t an issue in 99% of cases, and the extra accuracy they provide is beneficial in a lot of cases – so you could argue that’s the sensible default.
Note that you can explicitly show a decimal literal as a double by putting a
dat the end also:…but because it’s a double value anyway, this has no effect. Some people might argue for it advocating it’s clearer what literal type you mean, but personally I think it just clutters code (unless perhaps you have a lot of float literals hanging around and you want to emphasise that this literal is indeed meant to be a double, and the omission of the f isn’t just a bug.)