Skip to main content
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.

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:
You can evaluate these states using built-in boolean properties:

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.

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.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More