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.

Double is a standard library structure in Swift that represents a 64-bit, double-precision, floating-point number. It conforms to the IEEE 754 specification for floating-point arithmetic and provides a precision of at least 15 decimal digits. Because of its higher precision compared to the 32-bit Float type, Swift designates Double as the default inferred type for all floating-point literals.

Syntax and Type Inference

When a floating-point literal is assigned to a variable or constant without an explicit type annotation, the Swift compiler automatically infers it as a Double.
// Implicit type inference
let inferredValue = 3.141592653589793 // Type is Double

// Explicit type annotation
let explicitValue: Double = 2.718281828459045

// Scientific notation literal
let scientificValue: Double = 1.25e-4 // Represents 0.000125

// Hexadecimal floating-point literal
let hexValue: Double = 0xFp2 // Represents 15 * 2^2 (60.0)

Technical Characteristics

  • Memory Footprint: 64 bits (8 bytes).
  • Significand Precision: 53 bits (approximately 15 to 17 decimal digits).
  • Exponent Range: 11 bits, allowing values from approximately ±2.23 × 10^-308 to ±1.79 × 10^308.
  • Empty Initializer: Invoking the empty initializer (Double()) evaluates to 0.0. Note that Swift enforces definite initialization; declaring a variable without an assignment (e.g., var x: Double) leaves it uninitialized, and attempting to access it will result in a compile-time error.

Special Values

Conforming to the IEEE 754 standard, Double includes static properties for representing non-standard numeric states:
let positiveInf: Double = .infinity      // Represents positive infinity
let negativeInf: Double = -.infinity     // Represents negative infinity
let notANumber: Double = .nan            // Represents an undefined result (Not a Number)
let zeroValue: Double = .zero            // Represents 0.0
let piValue: Double = .pi                // Represents the mathematical constant Pi
You can evaluate these states using built-in boolean properties:
let value: Double = 0.0 / 0.0

let isNaN = value.isNaN           // true
let isInfinite = value.isInfinite // false
let isFinite = value.isFinite     // false
let isZero = value.isZero         // false

Type Conversion

Swift generally requires explicit type conversion when interacting with different numeric types, such as Int or Float. However, as of Swift 5.5 (SE-0307), the compiler supports implicit conversion between Double and CGFloat, allowing them to be used interchangeably without explicit casting.
import CoreGraphics

let integerValue: Int = 42
let floatValue: Float = 9.99
let cgFloatValue: CGFloat = 15.5

// Explicit conversion required for Int and Float
let doubleFromInt = Double(integerValue)
let doubleFromFloat = Double(floatValue)

// Implicit conversion supported for CGFloat
let doubleFromCGFloat: Double = cgFloatValue
let cgFloatFromDouble: CGFloat = doubleFromInt

// Explicit conversion from Double to Int
let doubleValue: Double = 10.75
let truncatedInt = Int(doubleValue) // Evaluates to 10 (truncates toward zero)

Key Protocol Conformances

Double implements several core Swift protocols that dictate its behavior in the type system:
  • FloatingPoint & BinaryFloatingPoint: Provides the foundational floating-point operations, radix definitions, and IEEE 754 behaviors.
  • ExpressibleByFloatLiteral & ExpressibleByIntegerLiteral: Allows initialization directly from numeric literals.
  • Hashable & Equatable: Enables use as dictionary keys and allows equality comparisons (==, !=).
  • Comparable: Enables relational comparisons (<, >, <=, >=).
  • Codable: Allows serialization and deserialization to formats like JSON or Property Lists.
Master Swift with Deep Grasping Methodology!Learn More