> ## Content Index
> Fetch the complete content index at: https://developerinsider.co/llms.txt
> Use this file to discover other available public pages before exploring further.

# Float by Example - Swift Programming Language
- URL: https://developerinsider.co/float-by-example-swift-programming-language/
- Published: 2017-08-31T11:05:20.000Z
- Updated: 2022-08-05T04:44:35.000Z
- Author: Guest
- Tags: Swift, iOS, Swift4

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

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

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

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

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

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

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

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

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

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

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

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

```

#### 4.3 minimum(*:*:)

- Returns the lesser of the two given values.

```swift
print(Double.minimum(10.0, -25.0))
//prints -25.0

```

## 5\. Finding the Sign and Magnitude

#### 5.1 magnitude

- The magnitude of this value.

```swift
let num1 = -259.0001
print(num1.magnitude)
//prints 259.0001

```

#### 5.2 sign

- The sign of the floating-point value.

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

```swift
let num2 = 0.23
print(num2.ulp)
//prints 2.77555756156289e-17

```

#### 6.2 significand

- The significand of the floating-point value.

```swift
let num3 = 9.91
print(num3.significand)
//prints 1.23875

```

#### 6.3 exponent

- The exponent of the floating-point value.

```swift
let num3 = 9.91
print(num3.exponent)
//prints 3

```

#### 6.4 nextUp

- The least representable value that compares greater than this value.

```swift
let num4 = 10.0
print(num4.nextUp)
//prints 10.0

```

#### 6.5 nextDown

- The greatest representable value that compares less than this value.

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

```swift
print(num4.binade)
//prints 8.0

```

## 7\. Accessing Numeric Constants

#### 7.1 pi

- The mathematical constant pi.

```swift
print(Double.pi)
//prints 3.14159265358979

```

#### 7.2 infinity

- Positive infinity.

```swift
let x1 = Double.greatestFiniteMagnitude
let y1 = x1 * 2
print(y1)
//prints inf

```

#### 7.3 greatestFiniteMagnitude

- The greatest finite number representable by this type.

```swift
let x1 = Double.greatestFiniteMagnitude
print(x1)
//prints 1.79769313486232e+308

```

#### 7.4 nan

- A quiet NaN (“not a number”).

```swift
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.nan)
//prints false

```

#### 7.5 signalingNaN

- A signaling NaN (“not a number”).

```swift
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.signalingNaN)
//prints false

```

#### 7.6 ulpOfOne

- The unit in the last place of 1.0.

```swift
let x1 = Double.greatestFiniteMagnitude
print(x1 > Double.ulpOfOne)
//prints true

```

#### 7.7 leastNormalMagnitude

- The least positive normal number.

```swift
let x1 = Double.greatestFiniteMagnitude
print (x1 < Double.leastNormalMagnitude)
//prints false

```

#### 7.8 leastNonzeroMagnitude

- The least positive number.

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

```swift
let num5 = 3000.00000
print(num5.bitPattern)
//prints 4658815484840378368

```

#### 8.2 significandBitPattern

- The raw encoding of the value’s significand field.

```swift
let num5 = 3000.00000
print(num5.significandBitPattern)
//prints 2093470139285504

```

#### 8.3 exponentBitPattern

- The raw encoding of the value’s exponent field.

```swift
let num5 = 3000.00000
print(num5.exponentBitPattern)
//prints 1034

```

#### 8.4 significandWidth

- The number of bits required to represent the value’s significand.

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

```swift
let num5 = 3000.00000
print(num5.isZero)
//prints false

```

#### 9.2 isSubnormal

- A Boolean value indicating whether the instance is subnormal.

```swift
let num5 = 3000.00000
print(num5.isSubnormal)
//prints false

```

## 10\. Describing a Float

#### 10.1 description

- A textual representation of the value.

```swift
let num5 = 3000.00000
print(num5.description)
//prints 3000.0

```

#### 10.2 debugDescription

- A textual representation of the value, suitable for debugging.

```swift
let num5 = 3000.00000
print(num5.debugDescription)
//prints 3000.0

```

#### 10.3 customMirror

- A mirror that reflects the Float instance.

```swift
let num5 = 3000.00000
print(num5.customMirror)
//prints Mirror for Double

```

#### 10.4 hashValue

- The number’s hash value.

```swift
print(num5.hashValue)
//prints 4658815484840378368

```

**Next** \- [Double by Example](https://developerinsider.co/double-by-example-swift-programming-language/)

---

#### You can download the swift playground of all above examples from [Here](https://github.com/developerinsider/developer-insider-content/blob/master/POST/iOS/Swift/Playgrounds/Float.playground.zip?raw=true&ref=developerinsider.co)

---