Double by Example - Swift Programming Language

Overview

  • Double represents a 64-bit floating-point number.
  • Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits.
  • Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.If you combine integer and floating-point literals in an expression, a type of Double will be inferred.

1. Converting Floating-Point Values

1.1 init(_:)

  • Creates a new instance initialized to the given value.
let x: Double = 21.25
let y = Double(x)
print(x, y)
//prints 21.25 21.25

1.2 init(sign:exponent:significand:)

  • Creates a new value from the given sign, exponent, and significand.
let z = Double(sign: .plus, exponent: -2, significand: 1.5)
print(z)
//prints 0.375

1.3 init(signOf:magnitudeOf:)

  • Creates a new floating-point value using the sign of one value and the magnitude of another.
let a = -21.5
let b = 305.15
let c = Double(signOf: a, magnitudeOf: b)
print(c)
//prints -305.15

2. Performing Calculations

2.1 Floating-Point Operators for Double

  • Perform arithmetic and bitwise operations or compare values.
    Framework
print(a + b)  
//prints 283.65

print(a - b)  
//prints -326.65

print(a * b)  
//prints -6560.725

print(a / b)  
//prints -0.0704571522202196

a += b  
print(a)
//prints 283.65


a -= b  
print(a)
//prints -21.5

a *= b  
print(a)
//prints -6560.725

a /= b  
print(a)
//prints -21.5

2.2 squareRoot()

  • Returns the square root of the value, rounded to a representable value.
func hypotenuse(_ a: Double, _ b: Double) -> Double {  
    return (a * a + b * b).squareRoot()
}

let (dx, dy) = (3.0, 4.0)  
let distance = hypotenuse(dx, dy)  
print(distance) 
//prints 5.0 

2.3 remainder(dividingBy:)

  • Returns the remainder of this value divided by the given value.
let q = (x / 0.75).rounded(.toNearestOrEven)  
let r = x.remainder(dividingBy: 0.75)  
print(q,r)  
//prints 28.0 0.25

2.4 negate()

  • Replaces this value with its additive inverse.
var k = 21.003  
k.negate()  
print(k)
//prints -21.003  

3. Querying a Double's State

3.1 isZero

  • A Boolean value indicating whether the instance is equal to zero.
let m = -0.0
print(m.isZero)
//prints true

3.2 isFinite

  • A Boolean value indicating whether this instance is finite.
let m = -0.0
print(m.isFinite)
//prints true

3.3 isInfinite

  • A Boolean value indicating whether the instance is infinite.
let m = -0.0
print(m.isInfinite)
//prints false

3.4 isNaN

  • A Boolean value indicating whether the instance is NaN (“not a number”).
let x1 = 0.0
let y1 = x1 * .infinity
print(x1 == Double.nan)
//prints false
print(y1 == Double.nan)
//prints false

3.5 isSignalingNaN

  • A Boolean value indicating whether the instance is a signaling NaN.
let x1 = 0.0
let y1 = x1 * .infinity
print(x1.isSignalingNaN)
//prints false
print(y1.isSignalingNaN)
//prints false

3.6 isNormal

  • A Boolean value indicating whether this instance is normal.
let x1 = 0.0
let y1 = x1 * .infinity
print(x1.isNormal)
//prints false
print(y1.isNormal)
//prints false

3.7 isSubnormal

  • A Boolean value indicating whether the instance is subnormal.
let x1 = 0.0
let y1 = x1 * .infinity
print(x1.isSubnormal)
//prints false
print(y1.isSubnormal)
//prints false

3.8 isCanonical

  • A Boolean value indicating whether the instance’s representation is in the canonical form.
let x1 = 0.0
let y1 = x1 * .infinity
print(x1.isCanonical)
//prints true
print(y1.isCanonical)
//prints true

3.9 floatingPointClass

  • The classification of this value.
let x1 = 0.0
let y1 = x1 * .infinity
print(x1.floatingPointClass)
//prints positiveZero
print(y1.floatingPointClass)
//prints quietNaN

4. Rounding

4.1 rounded()

  • Returns this value rounded to an integral value using “schoolbook rounding.”
let m1: Double = 6.5  
print(m1.rounded(.toNearestOrAwayFromZero))  
//prints 7.0
print(m1.rounded(.towardZero))  
//prints 6.0
print(m1.rounded(.up))  
//prints 7.0
print(m1.rounded(.down))  
//prints 6.0

4.2 round()

  • Rounds this value to an integral value using “schoolbook rounding.”
var j = 5.2  
j.round()  
print(j)  
//prints 5.0

var h = 5.5  
h.round()  
print(h)  
//prints 6.0

var g = -5.5  
g.round()  
print(g)  
//prints -6.0

5. Comparing Doubles

5.1 Floating-Point Operators for Double - comparison

  • Perform arithmetic and bitwise operations or compare values.
print(a > b)
//prints false 

print(a < b)  
//prints true

print(a <= b)  
//prints true

print(a >= b)  
//prints false

print(a == b)  
//prints false

5.2 isEqual(to:)

  • Returns a Boolean value indicating whether this instance is equal to the given value.
var a = -21.5
var b = 305.15
if(a.isEqual(to: b)){
   print(a)
}else{
print(b)
}
//prints 305.15

5.3 isLess(than:)

  • Returns a Boolean value indicating whether this instance is less than the given value.
var a = -21.5
var b = 305.15
if(a.isLess(than: b)){
   print(a)
}else{
print(b)
}
//prints -21.5

5.4 isLessThanOrEqualTo(_:)

  • Returns a Boolean value indicating whether this instance is less than or equal to the given value.
var a = -21.5
var b = 305.15
if(a.isLessThanOrEqualTo(b)){
   print(a)
}else{
print(b)
}
//prints -21.5

5.5 isTotallyOrdered(belowOrEqualTo:)

  • Returns a Boolean value indicating whether this instance should precede or tie positions with the given value in an ascending sort.
var a = -21.5
var b = 305.15
if(a.isTotallyOrdered(belowOrEqualTo: b)){
   print(a)
}else{
print(b)
}
//prints -21.5

5.6 minimum(::)

  • Returns the lesser of the two given values.
print(Double.minimum(10.0, -25.0))
//prints -25.0

5.7 maximum(::)

  • Returns the lesser of the two given values.
print(Double.maximum(10.0, -25.0))
//prints 10.0

5.8 minimumMagnitude(::)

  • Returns the value with lesser magnitude.
print(Double.minimumMagnitude(-10.0, 2.0))
//prints 2.0

5.9 maximumMagnitude(::)

  • Returns the value with greater magnitude.
print(Double.maximumMagnitude(-10.0, 2.0))
//prints -10.0

6. Finding the Sign and Magnitude

6.1 magnitude

  • The magnitude of this value.
let num1 = -259.0001  
print(num1.magnitude)  
//prints 259.0001

6.2 sign

  • The sign of the floating-point value.
let num1 = -259.0001  
print(num1.sign)  
//prints minus

7. Querying a Double

7.1 ulp

  • The unit in the last place of this value.
let num2 = 0.23
print(num2.ulp)
//prints 2.77555756156289e-17

7.2 significand

  • The significand of the floating-point value.
let num3 = 9.91
print(num3.significand)
//prints 1.23875

7.3 exponent

  • The exponent of the floating-point value.
let num3 = 9.91
print(num3.exponent)
//prints 3

7.4 nextUp

  • The least representable value that compares greater than this value.
let num4 = 10.0
print(num4.nextUp)
//prints 10.0

7.5 nextDown

  • The greatest representable value that compares less than this value.
let num4 = 10.0
print(num4.nextDown)
//prints 10.0

7.6 binade

  • The floating-point value with the same sign and exponent as this value, but with a significand of 1.0.
print(num4.binade)
//prints 8.0

8. Accessing Numeric Constants

8.1 pi

  • The mathematical constant pi.
print(Double.pi)
//prints 3.14159265358979

8.2 infinity

  • Positive infinity.
let x1 = Double.greatestFiniteMagnitude
let y1 = x1 * 2
print(y1)
//prints nan

8.3 greatestFiniteMagnitude

  • The greatest finite number representable by this type.
let x1 = Double.greatestFiniteMagnitude
print(x1)
//prints 0.0

8.4 nan

  • A quiet NaN (“not a number”).
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.nan)
//prints false

8.5 signalingNaN

  • A signaling NaN (“not a number”).
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.signalingNaN)
//prints false

8.6 ulpOfOne

  • The unit in the last place of 1.0.
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.ulpOfOne)
//prints false

9. Working with Binary Representation

9.1 bitPattern

  • The bit pattern of the value’s encoding.
let num5 = 3000.00000
print(num5.bitPattern)
//prints 4658815484840378368

9.2 significandBitPattern

  • The raw encoding of the value’s significand field.
let num5 = 3000.00000
print(num5.significandBitPattern)
//prints 2093470139285504

9.3 exponentBitPattern

  • The raw encoding of the value’s exponent field.
let num5 = 3000.00000
print(num5.exponentBitPattern)
//prints 1034

9.4 significandWidth

  • The number of bits required to represent the value’s significand.
let num5 = 3000.00000
print(num5.significandWidth)
//prints 8

10. Describing a Double

10.1 description

  • A textual representation of the value.
let num5 = 3000.00000
print(num5.description)
//prints 3000.0

10.2 debugDescription

  • A textual representation of the value, suitable for debugging.
let num5 = 3000.00000
print(num5.debugDescription)
//prints 3000.0

10.3 customMirror

  • A mirror that reflects the Float instance.
let num5 = 3000.00000
print(num5.customMirror)
//prints Mirror for Double

10.4 hashValue

  • The number’s hash value.
print(num5.hashValue)
//prints 4658815484840378368

11. Infrequently Used Functionality

11.1 distance(to:)

  • Returns the distance from this value to the specified value.
let x2 = 21.5
let d2 = x2.distance(to: 15.0)
print(d2)
//prints -6.5

11.2 advanced(by:)

Returns a new value advanced by the given distance.

let x2 = 21.5
let d2 = -6.5
print(x2.advanced(by: d2))
//prints 15.0

11.3 init(floatLiteral:)

  • Creates a new value from the given floating-point literal.
let x3: Double = 21.25
print(x3)
//prints 21.25

Next - String by Example


You can download the swift playground of all above examples from Here



You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.