Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Properties Computed Properties

What is a float?

class Temperature { var celsius: Float = 0.0 }

1 Answer

Jake Adams
Jake Adams
1,608 Points

There are 2 types of floating points in Swift: Float and Double (both are numbers with decimals)

  • Float is a 32-bit representation of a number
  • Double is a 64-bit representation

It's important to note that floating points are simply approximations of real numbers. The benefit to floating points is they can represent much larger numbers that an Integer, but they are not without their pitfalls.

For instance, the constant declared below has a value of 1, even though we know it should be more.

let iOnlyEqualOneWTH = Float(1) + Float(0.000000000000001)

As a side note, it's always best to represent currency values using Double as that gives us twice the precision and a much smaller chance of a floating point math error.

Floating point: http://www.webopedia.com/TERM/F/floating_point_number.html

Apple Docs: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html