Hello everyone and welcome to the sixth lesson in swift programming brought to you by MadApper. my name is Paul Napier and in this lesson we will cover another two of the basic types: floats and doubles.
In the last lesson we covered numbers which had nothing after the decimal place. However, if we are going to work with calculations, we are going to constantly face situations where we have decimal places. In these instances we will use either floats or doubles:
let myFloat: Float = 1.025 let myDouble: Double = 1.025
As a definition, floats are floating point numbers which has a 32 bit representation and can have a precision of only 6 decimal places. A double is a floating point number that has a 64 bit representation and a precision of at least 15 decimal places, so when you have large numbers and require a higher degree of accuracy, you should use a double. When declaring these floating point numbers implicitly, the compiler will choose the floating point number most suitable to your system.
So if I type:
let floatingPoint = 1.01
Then start typing floatingPoint again, I can see in the code assistant that the compiler has declared it as a Double. This is important to note and may cause you issues if you did not realise this later on in your code.
Ok, so we have covered off the basics of numbers. In the next lesson we are going to talk briefly about type safety and casting..