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.

A Double in Kotlin is a built-in class representing a double-precision 64-bit IEEE 754 floating-point number. In Kotlin’s unified type system, there are no primitive types at the language level; everything is an object, allowing Double to possess member functions and properties. To maintain performance, the Kotlin compiler optimizes Double to the JVM primitive double at runtime whenever possible.

Technical Specifications

  • Memory Size: 64 bits (8 bytes)
  • Bit Layout: 1 sign bit, 11 exponent bits, and 52 significand (mantissa) bits.
  • Maximum Value: Double.MAX_VALUE (approx. 1.7976931348623157E308)
  • Minimum Non-Zero Value: Double.MIN_VALUE (approx. 4.9E-324)

Syntax and Instantiation

By default, the Kotlin compiler infers any fractional literal as a Double. Scientific notation is natively supported and also inferred as Double.
val inferredDouble = 3.14159          // Type inferred as Double
val explicitDouble: Double = 2.71828  // Explicit type declaration
val scientificDouble = 1.23e4         // Evaluates to 12300.0
To force a fractional literal to be a Float, an f or F suffix is required. Without it, the literal is strictly evaluated as a Double.

JVM Representation and Boxing

While Kotlin treats Double as a class, it compiles down to the Java primitive double to avoid memory overhead. However, if a Double is declared as nullable (Double?) or is used within a generic context (such as List<Double>), the compiler performs autoboxing, mapping it to the java.lang.Double reference type on the JVM.
val primitiveOptimized: Double = 5.5  // Compiled to JVM primitive 'double'
val boxedDouble: Double? = 5.5        // Compiled to JVM reference 'java.lang.Double'

Type Conversion

Kotlin enforces strict type safety and does not support implicit widening conversions. Types with smaller memory footprints or different representations (like Int or Float) cannot be implicitly assigned to a Double. Explicit conversion functions must be invoked.
val integerValue: Int = 42
// val invalidDouble: Double = integerValue // Compilation error: Type mismatch
val validDouble: Double = integerValue.toDouble() // Explicit conversion

Special IEEE 754 Values

The Double companion object provides constants for special floating-point states defined by the IEEE 754 standard:
val notANumber: Double = Double.NaN                // Result of undefined operations (e.g., 0.0 / 0.0)
val positiveInf: Double = Double.POSITIVE_INFINITY // Result of positive overflow (e.g., 1.0 / 0.0)
val negativeInf: Double = Double.NEGATIVE_INFINITY // Result of negative overflow (e.g., -1.0 / 0.0)

Equality and Comparison Rules

Equality evaluation for Double in Kotlin strictly depends on the static type of the operands being compared.

IEEE 754 Comparison

When the static type of the operands is Double or Double?, structural equality (==) performs standard IEEE 754 floating-point comparison. Under these rules, positive and negative zero are considered equal, and NaN is not equal to anything, including itself.
val posZero: Double = 0.0
val negZero: Double = -0.0
println(posZero == negZero) // true (IEEE 754 rules)

val nan1: Double? = Double.NaN
val nan2: Double? = Double.NaN
println(nan1 == nan2) // false (IEEE 754 rules apply even to Double?)

Total Order Comparison

When the static type of the operands is Any, Comparable, or a generic type parameter, Kotlin abandons IEEE 754 rules and applies total order comparison. This ensures predictable behavior when floating-point numbers are used as keys in maps or sorted in collections. Under total order rules, NaN is considered equal to itself and greater than POSITIVE_INFINITY, and -0.0 is considered strictly less than 0.0.
val anyNan1: Any = Double.NaN
val anyNan2: Any = Double.NaN
println(anyNan1 == anyNan2) // true (Total order rules)

val anyPosZero: Any = 0.0
val anyNegZero: Any = -0.0
println(anyPosZero == anyNegZero) // false (Total order rules: -0.0 < 0.0)

Precision Limitations

Because Double relies on base-2 fractions, it cannot perfectly represent all base-10 fractional values. This leads to standard floating-point arithmetic inaccuracies during evaluation.
val calculation = 0.1 + 0.2
println(calculation == 0.3) // false (calculation evaluates to 0.30000000000000004)
Master Kotlin with Deep Grasping Methodology!Learn More