poniedziałek, 16 listopada 2015

When To Use Decimal, Double, and Float



Float
Category
Type Suffix
Precision
Approximate range
.NET Framework type
128
Numeric, decimal
M or m
 28-29 significant digits
 (-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
64
Numeric, floating-point
D or d
 15-16 digits
 ±5.0 × 10−324 to ±1.7 × 10308
32
Numeric, floating-point
F or f
 7 digits
 -3.4 × 1038to +3.4 × 1038

A floating-point (double, float) expression can contain the following sets of values:
  • Positive and negative zero.
  • Positive and negative infinity.
  • Not-a-Number value (NaN).
  • The finite set of nonzero values.
In default settings, Floats (System.Single) and doubles (System.Double) will never use overflow checking while Decimal (System.Decimal) will always use overflow checking.

The Decimal, Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type.

Decimals and Floats/Doubles cannot be compared without a cast whereas Floats and Doubles can.

Decimal
In case of financial applications it is better to use Decimal types because it gives you a high level of accuracy and easy to avoid rounding errors.
Decimals are much slower (up to 20X times in some tests) than a double/float.

Double
Double Types are probably the most normally used data type for real values, except handling money.

Float
It is used mostly in graphic libraries because very high demands for processing powers, also used situations that can endure rounding errors.
Floating binary point types are much faster to work with than decimals.


http://net-informations.com/q/faq/float.html
http://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net
http://jacobmsaylor.com/?p=1114

Brak komentarzy:

Prześlij komentarz