> ## 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.

# Kotlin Double

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`.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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:

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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`.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
val calculation = 0.1 + 0.2
println(calculation == 0.3) // false (calculation evaluates to 0.30000000000000004)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
