Explain what floating point numbers are and what types of floating points are available in Swift


A floating point number represents a value with a decimal point. In Swift, there are two types of floating point numbers i.e. Float and Double. They both are very similar with some unique use cases.

Float

They represent 32-bit decimal numbers in the Swift language. When you want to store floating point numbers with small precision, Floats are the best use case.

Float has a precision of 6 to 9 decimal digits in floating point numbers which represent the 32-bit space in memory. The range of values that can be represented by a Float is approximately -3.4 x 10^38 to +3.4 x 10^38.

Double

They also represent the floating point numbers in the Swift language. They have 64-bit precision which is a larger range than Float numbers. But remember that the double numbers take up more space in memory. It is always recommended to use the Double until you have a specific reason to use Float.

Double has a precision of 15 to 17 decimal digits in floating point numbers. The range of values that can be represented by a Double is approximately -1.7 x 10^308 to +1.7 x 10^308.

Declaring Float Numbers

In Swift, you can use the Float keyword to declare the floating point numbers. Below are some examples to declare the floating point numbers using Float in Swift −

Example

let a: Float = 3.14
let b: Float = 3.14 as Float
let c: Float = 3.14 as Float32
print("a: \(a), b: \(b), c: \(c)")

Output

a: 3.14, b: 3.14, c: 3.14

Declaring Double Numbers

In a similar way, you can use the Double keyword to declare the floating point numbers. Below are some examples to declare the floating point numbers using Double in Swift −

Example

let a: Double = 3.14
let b: Double = 3.14 as Double
let c: Double = 3.14 as Float64
print("a: \(a), b: \(b), c: \(c)")

Output

a: 3.14, b: 3.14, c: 3.14

You can declare the floating point numbers using the Double keyword instead of Float unless if you have a specific reason to declare the values using Float. For example, if you want to store such floating numbers which are not required long precision, Float is a good option. It also saves space in memory.

Example

Here are some examples of using floating-point numbers in Swift −

// Declaring a Float
let myFloat: Float = 3.14
print("myFloat: \(myFloat)")

// Declaring a Double
let myDouble: Double = 3.14159265358979323846
print("myDouble: \(myDouble)")

// Using a Float in a mathematical operation
let pi: Float = 3.14
let radius: Float = 2.0
let area: Float = pi * (radius * radius)
print("area: \(area)")

// Using a Double in a mathematical operation
let piDouble: Double = 3.14159265358979323846
let radiusDouble: Double = 2.0
let areaDouble: Double = piDouble * (radiusDouble * radiusDouble)
print("areaDouble: \(areaDouble)")

Output

myFloat: 3.14
myDouble: 3.141592653589793
area: 12.56
areaDouble: 12.566370614359172

As you can see, the Float and Double types are used in the same way, but the Double type provides greater precision in the mathematical operations.

In some cases, you may need to convert a Float to a Double or vice versa. This can be done using the Double() or Float() constructors −

Example

let myFloat: Float = 3.14
let myDouble: Double = Double(myFloat)
print("myDouble:", myDouble)

let myDouble2: Double = 3.14
let myFloat2: Float = Float(myDouble2)
print("myFloat2:", myFloat2)

Output

myDouble: 3.140000104904175
myFloat2: 3.14

It's important to note that casting floating-point numbers may result in a loss of precision.

Conclusion

In swift, both floating point number types are used to represent decimal point numbers. Float represents the 32-bit precision numbers that can hold 6 to 9 decimal digits.

On the other hand, Double represents the 64-bit precision numbers that can hold 15 to 17 decimal points. Double has greater precision and a larger range than Float, but it also takes up more memory. It is recommended to use Double unless you have a specific reason to use Float.

In general, Float and Double are used in the same way, but the Double type provides greater precision in mathematical operations. When converting between Float and Double it's essential to note that casting floating-point numbers may result in a loss of accuracy.

Updated on: 28-Feb-2023

915 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements