Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Properties Computed Properties

JA Achievement
JA Achievement
1,516 Points

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