C#:Possible Loss of Precision

From Progzoo
Revision as of 18:42, 29 March 2009 by Andr3w (Talk | contribs) (New page: {{Simple Types}} <p> A <code>double</code> can store more decimal places than a <code>float</code>. This means that if convert a <code>double</code> to a <code>float</code> we will lose <i...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A double can store more decimal places than a float. This means that if convert a double to a float we will lose precision - i.e. the number will be less precise.

Possible loss of precision

We cannot even compile a program that might involve a loss of precision. Attempting to (implicitly) convert a double to a float will cause a compile error such as:

Demo.java:4: possible loss of precision
found   : double
required: float

We should do one of the following:

  • Explicitly cast from double to float:
float d = (float)3.8;
  • Use f to indicate that the literal is a

float not a double

[Font] [Default] [Show] [Resize] [History] [Profile]