3

I was under the impression that it is legal and conventional to declare and initialize a floating point number in this format:

float someVariable = 12.502D;  (or M, F does not give a compiler error).

However I get a compiler error:

Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type.

There are three types of floating point numbers in C#, right?

  1. F or f for float. (7 significant digits)
  2. D or d for Double. (15 or 16 significant digits)
  3. M or m for Decimal. (28 or 29 significant digits)

To fix the compiler error I explicitly casted the assignment statement:

float SomeVariable = (float) 12.525D;

Did I do the right thing in this case? What is the conventional or correct way to do it declare and initialize a floating point variable that consists of a Double or a Decimal value?

3
  • 7
    Why not just use F as the compiler suggested? Then no cast is needed (implicitly nor explicitly)… Commented Jan 12, 2013 at 9:18
  • 2
    Or, if you want a double variable, declare it as such double SomeVariable = 12.525D; It just doesn't make sense to declare it as one type and use a literal of a different type. Commented Jan 12, 2013 at 9:21
  • FWIW, if you use decimal, then be aware that this is not a hardware supported type, i.e. everything is done in software, which makes it more accurate but not very fast. But like others said, why not 12.502f instead of 12.502d? Commented Jan 13, 2013 at 19:14

3 Answers 3

7

In plainer English, the default type assumed by the compiler for the text string 12.502 is double. A double is twice the size of a float and just like a quart can't fit in a pint, a double cannot be stored in a float unless you do a cast, which risks losing precision.

You could tell the compiler that 12.502 is actually a float which you do by adding an F or f suffix like this:

float someVariable = 12.502f;

Or:

double someVariable = 12.502;
Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you use:

float someVariable = 12.502f;

or

double someVariable = 12.502; //floating point literals are by default doubles

float is single precision floating point arithmetic, which is can't be converted to double implicitly in C#. In C# every cast that can potentially lost some information can't be implicit. float has 32 bits to store components of floating precision. double has 64 bits.

float can take values from 1.5 × 10^-45 to 3.4 × 10^38

double can take values from 5.0 × 10^-324 to 1.7 × 10^308

So you can see doubles can store much greater range of values. So if you convert from double to float you can potentially lost information.

3 Comments

You mean the other way around? :]
I don't know exactly if he want value of double or float
Thanks, I think I was misunderstanding the whole floating point concept. I was thinking that Decimal, and Double is a subset data type of float, but float, Decimal, and Double are all stand alone and independent data types. Thanks for the plain English explanation.
0

If you write float SomeVariable = (float) 12.525D;, then the decimal numeral is first converted to a double and then converted to a float. In rare cases, depending on the numeral, this double rounding changes the value slightly.

Writing the numeral with an F suffix avoids this.

(If numerals were uniformly distributed in some sense, this would happen about one time in 230, since there are 29 fewer bits in a float significand than in a double significand. The problem happens when the original numeral rounds, in double, to exactly the halfway point for a float rounding [one time in 229] but would, if rounded directly to float, round in the other direction from where that halfway point is rounded to [one time in two].)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.