Skip to main content
The double keyword in C# is a built-in value type that represents a double-precision, 64-bit floating-point number. It serves as a language alias for the .NET System.Double struct and strictly adheres to the IEEE 754 standard for binary floating-point arithmetic.

Technical Specifications

  • Memory Size: 64 bits (8 bytes)
  • Precision: 15 to 17 decimal digits
  • Minimum Value: -1.7976931348623157E+308 (double.MinValue)
  • Maximum Value: 1.7976931348623157E+308 (double.MaxValue)
  • Smallest Non-Zero Value: 4.9406564584124654E-324 (double.Epsilon)

Memory Layout (IEEE 754)

The 64 bits of a double are partitioned into three distinct components:
  1. Sign bit: 1 bit (determines positive or negative)
  2. Exponent: 11 bits (determines the magnitude)
  3. Mantissa (Significand): 52 bits (determines the precision)

Syntax and Literals

By default, any real numeric literal on the right side of an assignment containing a decimal point is treated as a double by the C# compiler. You can also explicitly denote a double literal using the d or D suffix, or utilize scientific (exponential) notation using e or E.

Special Constants

The System.Double struct exposes several constant fields to handle edge cases in floating-point arithmetic, such as division by zero or undefined operations.

Type Conversions

Implicit Conversions: C# allows implicit conversions to double from any integral type (sbyte, byte, short, ushort, int, uint, long, ulong), as well as from float and char.
Explicit Conversions: Converting from double to integral types or the decimal type requires an explicit cast. Casting to an integral type truncates the value towards zero. Casting to decimal requires an explicit cast because double is a base-2 floating-point type with a massive range, whereas decimal is a base-10 floating-point type with a significantly smaller range. This conversion can result in precision loss or an OverflowException.

Precision and Comparison Pitfalls

Because double utilizes base-2 binary fractions, it cannot accurately represent certain base-10 fractional values (such as 0.1). This inherent limitation of binary floating-point arithmetic means that double should never be used for financial or monetary calculations; the decimal type must be used instead. Furthermore, due to these floating-point arithmetic inaccuracies, developers should avoid using the equality operator (==) to compare two double values. Instead, comparisons should check if the absolute difference between the two values is within a small, acceptable margin of error (tolerance).
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More