Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
// Implicit literal inference
double defaultLiteral = 3.14159;

// Explicit suffix
double explicitLiteral = 3.14159d;
double explicitLiteralUpper = 3.14159D;

// Scientific notation
double scientificNotation = 1.5e-4; // Evaluates to 0.00015
double scientificNotationUpper = 2.5E3; // Evaluates to 2500.0

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.
// Represents a value that is not a number (e.g., result of 0.0 / 0.0)
double nanValue = double.NaN;

// Represents positive infinity (e.g., result of 1.0 / 0.0)
double posInfinity = double.PositiveInfinity;

// Represents negative infinity (e.g., result of -1.0 / 0.0)
double negInfinity = double.NegativeInfinity;

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.
int integerVal = 42;
double implicitFromInt = integerVal; 

float floatVal = 3.14f;
double implicitFromFloat = floatVal;
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.
double originalValue = 9.99;

// Requires explicit cast; fractional part is truncated
int truncatedInt = (int)originalValue; // Evaluates to 9

// Requires explicit cast due to base-2 to base-10 conversion and range differences
decimal decimalValue = (decimal)originalValue; 

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).
double a = 0.1 * 3.0;
double b = 0.3;

// Incorrect: Evaluates to false due to floating-point imprecision (0.30000000000000004 != 0.3)
bool isEq = (a == b); 

// Correct: Compares the absolute difference against a small tolerance
double tolerance = 1e-10;
bool isEffectivelyEq = Math.Abs(a - b) < tolerance; // Evaluates to true
Master C# with Deep Grasping Methodology!Learn More