Kinderswift 5: Make Me a Double

This time, we review integers, and look at the Double type.

Transcript

Welcome back to kinder swift. This lesson we will look at numbers that have decimal points, which are known as Doubles. To start, let’s make a new playground. Click File>New>playground.
Name the new playground Kinderswift5Playground and save the file.

Remove what is already there so we have a fresh page.

To review the integers, or Int type as we will call it, add the following code to the playground. Pause the video if you need to.

//Int Type 
let myInteger = 4
var intResult = 0

intResult = myInteger + 5
intResult = myInteger - 5
intResult = myInteger * 5
intResult = 5 / myInteger 
intResult = myInteger / 5

We assigned 4 to a constant integer called myInteger. We then initialized to zero a variable called intResult.

Just like before, we added five to myInteger‘s value of four and got 9. We subtracted four from five and got -1. We multiplied 5 and got 20.

As we discussed last time we have some problems with integer division. Five divided by myInteger’s value of 4 equals one. Four divided by five equals zero.

Most of the time we want a decimal value so that 5/4 = 1.25 and 4/5 = 0.8.

Variables and constants have types. Up to now we have only worked with the Type Int. There are other types. One of the most frequent ones used for mathematics and graphics is Double. There are two ways to declare a double, one of which we’ll cover in our next lesson. The easy way is just add a decimal point to our initial value in a constant or variable. So for example type this out.

//Double Type
let myDouble = 4.0
var doubleResult = 0.0

In the results column, we get 4.0 and 0.0. The decimal point tells us this is a Double. These are now declared doubles.
Math works the same. Try adding:

doubleResult = myDouble + 5.0

We get 9.0. Now try subtraction:

doubleResult = myDouble - 5.0

We get -1.0. Now multiplication:

doubleResult = myDouble * 5.0

we get 20.0. And now for the good part. try these two division

doubleResult = myDouble / 5.0
doubleResult = 5.0 / myDouble

Now 4/5 = 0.8 and 5/4 equals 1.25.
When working with literal doubles you can write any number with a decimal point of course, so you can calculate these:

doubleResult = 2.4/3.0
doubleResult = 3.141/2.71

Doubles will allow increments as well. try:

doubleResult += 3.0

What you can’t do is mix types. YOu cant do any of these operations with both a Int and a double. type this:

//Can't mix types:
doubleResult = myDouble + myInteger

And you will get an error. We may want to add Integers and Doubles t times, so the next lesson will tackle that problem.
I’m

One Reply to “Kinderswift 5: Make Me a Double”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s