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

What is the difference between a Float and a Double?

Are they both used in the same way?

hey float - "32 bit" is smaller than double.

double -"double precision 64-bit " it allows you to work with large numbers.

double can be twice smaller or twice large than float.

Is there an advantage for using a float over a double if it is 32 bit? Or are you safe to always use a double?

2 Answers

Let me cite the Apple docs: Floating-Point Numbers here:

  • Double represents a 64-bit floating-point number, has a precision of at least 15 decimal digits
  • Float represents a 32-bit floating-point number, can have a precision as little as 6 decimal digits

In situations where either type would be appropriate, Double is preferred.

Swift also follows that convention by inferring floating point numbers as Double by default.

// pi is inferred to be of type Double
let pi = 3.14159

Hope that helps :)

i would think your are safe with any type you choose, floats should be sufficient in most cases. my professor told me to use doubles when dealing with an astronomically large numbers.