Junior
What is the difference between the data types Double and Float?
sobes.tech AI
Answer from AI
Double and Float are data types for working with floating-point numbers, differing in precision and size.
| Type | Size (bits) | Approximate Precision |
|---|---|---|
| Float | 32 | 6 decimal digits |
| Double | 64 | 15 decimal digits |
Double has a larger size and higher precision, allowing it to represent a wider range of values with less error. Float takes up less memory but is less precise. By default, most Swift functions working with floating-point numbers use Double. Using Float is justified when memory savings are critical and high precision is not required.
Example:
// Example of using Double
let doubleValue: Double = 3.1415926535
print(doubleValue)
// Example of using Float
let floatValue: Float = 3.1415926535 // Limited precision
print(floatValue)