Example:
float timeRemaining = 0.58f;
Why is the f is required at the end of this number?
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.
Your declaration of a float contains two parts:
timeRemainingis of typefloat.0.58to this variable.The problem occurs in part 2.
The right-hand side is evaluated on its own. According to the C# specification, a number containing a decimal point that doesn’t have a suffix is interpreted as a
double.So we now have a
doublevalue that we want to assign to a variable of typefloat. In order to do this, there must be an implicit conversion fromdoubletofloat. There is no such conversion, because you may (and in this case do) lose information in the conversion.The reason is that the value used by the compiler isn’t really 0.58, but the floating-point value closest to 0.58, which is 0.57999999999999978655962351581366… for
doubleand exactly 0.579999946057796478271484375 forfloat.Strictly speaking, the
fis not required. You can avoid having to use thefsuffix by casting the value to afloat: