Float by Example - Swift Programming Language

Overview

  • Floating-point numbers are numbers with a fractional component, such as 3.14159, 0.1, and -273.15.
  • Float represents a 32-bit floating-point number.

1. Converting Floating-Point Values

1.1 init(_:)

  • Creates a new instance that approximates the given value.
let x: Double = 21.25  
let y = Float(x)  
print(y)  
//prints 21.25

1.2 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

1.3 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

2. Performing Calculations

2.1 Floating-Point Operators for Float

  • Perform arithmetic and bitwise operations or compare values.
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  
k.negate()  
print(k)  
//prints -21

3. Rounding

3.1 rounded()

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

3.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 -5.0

4. Comparing Floats

4.1 comparison

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

4.2 maximum(::)

Returns the greater of the two given values.

print(Double.maximum(10.0, -25.0))  
//prints 10

4.3 minimum(::)

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

5. Finding the Sign and Magnitude

5.1 magnitude

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

5.2 sign

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

6. Querying a Float

6.1 ulp

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

6.2 significand

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

6.3 exponent

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

6.4 nextUp

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

6.5 nextDown

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

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

7. Accessing Numeric Constants

7.1 pi

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

7.2 infinity

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

7.3 greatestFiniteMagnitude

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

7.4 nan

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

7.5 signalingNaN

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

7.6 ulpOfOne

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

7.7 leastNormalMagnitude

  • The least positive normal number.
let x1 = Double.greatestFiniteMagnitude  
print (x1 < Double.leastNormalMagnitude)  
//prints false

7.8 leastNonzeroMagnitude

  • The least positive number.
let x1 = Double.greatestFiniteMagnitude  
print (x1 < Double.leastNonzeroMagnitude)  
//prints 4.94065645841247e-324

8. Working with Binary Representation

8.1 bitPattern

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

8.2 significandBitPattern

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

8.3 exponentBitPattern

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

8.4 significandWidth

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

9. Querying a Float's State

9.1 isZero

  • A Boolean value indicating whether the instance is equal to zero.
let num5 = 3000.00000  
print(num5.isZero)  
//prints false

9.2 isSubnormal

  • A Boolean value indicating whether the instance is subnormal.
let num5 = 3000.00000  
print(num5.isSubnormal)  
//prints false

10. Describing a Float

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

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




Related Article