Floating-Point: Double vs Float
Choose between Double and Float and handle precision.
Floating-Point: Double vs Float is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Floating-Point Types
Swift has two main floating-point types: Double (64-bit) and Float (32-bit). They represent numbers with fractional parts.
let d: Double = 3.14159
let f: Float = 3.14159
print(d, f)Double Is the Default
When you write a decimal literal without a type annotation, Swift infers Double. Double offers more precision than Float.
let value = 2.5
print(type(of: value))Precision Difference
Double stores about 15 significant decimal digits; Float only about 6 or 7. The same literal can print differently.
let d: Double = 0.1234567890123456
let f: Float = 0.1234567890123456
print(d)
print(f)The pi Constant
Both types provide .pi. Double.pi is more precise than Float.pi.
print(Double.pi)
print(Float.pi)Floating-Point Arithmetic
You can add, subtract, multiply, and divide floating-point numbers. Division keeps the fractional result.
let radius = 2.0
let area = Double.pi * radius * radius
print("Area:", area)Rounding Methods
Floating-point values offer .rounded(), plus variants like .rounded(.down) and .rounded(.up).
let x = 2.7
print(x.rounded())
print(x.rounded(.down))
print(x.rounded(.up))Floor, Ceil, and Truncation
Rounding rules let you floor toward negative infinity, ceil toward positive infinity, or truncate toward zero.
let n = -2.3
print(n.rounded(.down))
print(n.rounded(.up))
print(n.rounded(.towardZero))Floating-Point Imprecision
Because of binary representation, some decimals cannot be stored exactly. 0.1 + 0.2 is famously not exactly 0.3.
let sum = 0.1 + 0.2
print(sum)
print(sum == 0.3)Comparing Floats Safely
Instead of exact equality, compare floating-point numbers within a small tolerance (epsilon).
let sum = 0.1 + 0.2
let close = abs(sum - 0.3) < 0.0001
print("Close enough:", close)Special Float Values
Floating-point types can represent infinity and nan (not a number). Check with .isInfinite and .isNaN.
let inf = Double.infinity
let notNum = Double.nan
print(inf.isInfinite)
print(notNum.isNaN)Choosing Double or Float
Prefer Double for accuracy in general math. Use Float only when memory or specific hardware (like graphics) demands a 32-bit value.
let preciseAvg: Double = (10.0 + 3.0) / 4.0
print(preciseAvg)Quick Check
Recall which type Swift infers for decimal literals.
Recap: Double vs Float
You learned that Double (64-bit, the default) is more precise than Float (32-bit), that both offer .pi and rounding methods, and that floating-point math is imprecise so you should compare with a tolerance rather than ==.
let a = 0.1 + 0.2
print("Raw:", a)
print("Rounded:", a.rounded())
print("Within tolerance of 0.3:", abs(a - 0.3) < 1e-9)Frequently asked questions
Is the “Floating-Point: Double vs Float” lesson free?
Yes — the full text of “Floating-Point: Double vs Float” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Floating-Point: Double vs Float”?
Choose between Double and Float and handle precision. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Floating-Point: Double vs Float” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integer Types and Their Ranges
- Floating-Point: Double vs Float
- Explicit Numeric Conversions
- Numeric Literals and Underscores