Hey What's up everybody.
Mark Price read Zepps slopes dot com.
And today we're going to talk about numbers.
You're like wait a minute I thought we were doing programming what the heck are we doing.
Going back to like fifth grade math first grade.
Whenever you learn about numbers probably in kindergarten.
Yeah that's right.
Today's episode is brought to you by the number three.
OK.
So really though open up your tax code better and create a new playground if you're following along.
Otherwise you can just watch.
And we're going to call this one numbers and I'm going to create a new folder for a year.
It's always nice to organize your code and projects in the folders.
Swift and this one's going to be called numbers or save it here.
So numbers are a core foundational building block to any programming language.
And when you think about it you interact with numbers all the time in any application that you're using.
For instance if you go to Wells-Fargo dot com to log to your bank account you're going to see your bank
account balance and has numbers in it.
OK.
If you are using a workout app and one week you weighed like 150 pounds in the next week you weighed
170 pounds because who knows what you're doing with yourself.
You have to use numbers to show that to the user.
Numbers everywhere.
And it had to do math in order to calculate how many calories you consume in all those different things.
So numbers are everywhere and every application just like this is a string and there's words and applications
OK.
Use numbers and use words.
I know it sounds basic but those are foundational variable types in any language.
But there's different types of numbers so let's get rid of this string variable here and let's create
a new variable.
Some numbers.
Right now we don't just say bar some number you know equals number.
You know there's no what is the number you know there's no real number OK.
Well you have to do is actually create different types of numbers such as integers floats and doubles
and more.
So for instance if it were a person write a program for a person maybe this is one of those apps that
calc calculates and tracks calorie burning and things like that shows you how much junk food you're
eating you know.
So let's go ahead and say well we have to have a profile.
So we have an age right or not or ago a person has an age will say age 30 and let's say var weight.
Well that's two pounds.
That's how we roll in the US of A weight equals.
We're going to say 200.
So this person weighs 200 pounds OK.
Does anyone know what variable type this is.
OK.
Well these are actually called int or integers but in swift The key word is actually into like so.
So what we can do here is I can put a colon here and then I can put in the keyword and the variable
name.
And what we're doing right here is creating an explicitly declared type before we were creating a variable
with an inferred type meaning the variable is smart enough to know what you're putting in it.
And so it knows that it's and knows that it's an event.
For instance I can't say age equals peanuts.
I have to try and break the program that cannot assign value of type string to type int.
OK so you cannot put different data types inside of a variable and it was smart enough to know that
this was an int.
So this is the key word here.
This is an inferred or let's call this type inference.
That's the proper term type inference.
It inferred what type it was.
It's no different than this except it did automatically and by the way this is the recommended way in
swift if at all possible use type inference and you have to use an explicit declaration to do it.
Don't just do this because you think it looks nicer.
That's not the swift way with boys like this but this is an explicitly declared type.
OK.
So this isn't explicitly declared type.
Those are the key words and phrases right there explicitly declared because we actually wrote what type
it was.
And again the same thing here we can't say weight equals no way.
We can't can't spell either.
That's why we need to work with numbers.
OK.
So can't do it right.
It's the same exact thing no difference.
So pretty cool.
So we've got we've got some in here.
And what is an integer.
OK what is an integer.
Well let's go pull open the Swift book.
If you don't have it already and if you don't have it please get it.
Go to books.
This is super important.
Get this book if you don't have it.
I said go to books were already in books and then do a search for swift not Dora the Explorer.
Dora meets Pablo That's my personal reading just forget that you saw that.
And the swift program blinked.
How did that get on here.
Anyway it's with three editions the one that you want.
That's right.
Right here don't do the one with cocoa and object.
You do this right here and get that book and read through the entire thing.
I promise you won't you won't regret it and I'll make him much better programmer so integers are whole
numbers with no fractional component such as 42 and negative 23 integers are either signed positive
zero negative or unsigned positive or zero.
So if preside signed an unsigned injures and eight 16 32 and 64 bit forms and what that means is 64
bit 32 bit it's how many characters how many digits it can hold the actual value.
You know if someone had a trillion dollar bank account.
You know you're probably not going to use this energy and use something larger but Swift is smart enough
to do all that for you automatically just know that there are some different things going on under the
hood.
And so you can see some of the types here like you and eight OK and a 32 bit sign in terms of type into
32.
So it just depends.
It shows how much it holds.
Again you don't ever need to worry about this and pretty much in swift for the most part you're never
ever going to have to do anything else except this standard.
Ok but again you can you can do different things.
You know some of them you know and you could say you can't or is it you want eight 16 32 64.
Now you can do different things like that.
OK.
But you don't want to be careful.
You know you don't want to.
You don't want to do something that you can't you can't do because it will overflow the variable into
your literal overflows when stored and you it's too big too big of a number.
And so again don't play around with the zombie like oh I want to be more memory efficient and use smaller
integers.
Now don't do that.
Always always always use it.
OK.
Always always always use an integer.
I think actually this one is too big even for integer.
So in this case you probably have to use a data type that even longer like a long or something Swift
I think it's a double with that did the trick.
There you go doubles double precision double twice as long.
More or less.
Anyway there is another data type which allows you to have incredibly long values here.
OK.
Look this is just huge.
I don't even know what number that is.
OK so we have integer which you want to use at any given time.
If a number does not have decimal points and note nothing no dot you know whatever you want to use it.
OK.
Always always always use it if you need a long number.
Double is the favorite choice here.
OK.
So you can also double.
This is cool.
If you had something like let's say a bank account right.
So actually no we're doing what are we doing here.
We're doing an exercise Apso let's say Miles ran OK.
Our Miles ran and we ran.
Equals.
We've run in the whole week.
Fifty six point four five miles.
So what type is this.
This is actually a double.
That's pretty cool.
So by default swift uses doubles which has double the precision.
In fact let's Ballman our handy dandy book here like I didn't pay X amount of dollars for this course
or you can read a book to me.
I wanted you to say those words without looking at a book.
Does it make any sense.
No it's better to have it straight from the horse's mouth.
And I do this a lot.
Get used to it.
It is so important to read the documentation.
Like seriously if you go to your boss every single day like how do you do this how do you do this he's
going to fire you.
I wouldn't have an employee like that you've got to learn how to learn.
So always go back to the documentation.
I'm trying to train you here how to be a lifelong learner and programmer.
So where was I before I started lecturing you on things you don't even care about.
OK.
So we talk about integers now come floating point numbers floating point numbers or numbers with a fractional
component such as 3.1 for maybe you're thinking wait you just did double wide.
Why.
Why didn't we do a float with the decimals.
Well read this double as a precision of at least 15 decimal digits.
OK so 15 decimal digits.
Where is the precision of a flow can be as little as six decimal digits.
The appropriate floating point type to use depends on the nature and range of values you need to work
with in your code.
In situations where either type would be appropriate double is preferred.
OK so by default swift uses double.
OK as you can tell it's a 64 bit floating point number.
Very long a float cannot be as big and maybe you think it will should make the numbers I should say
memory by using a flow and have a double no switch just I optimized under the hood.
And it recommends that use double overflow and at a given time.
So don't use a float unless you absolutely have to.
Whereas in other languages float is the keyword in swift.
Always use double double double double.
OK.
So just to show you that this is a double by default.
If we do create a float let's say var pi equals three point one for k and then I say Miles Rand equals
PI and for pi.
Let's see what happens.
Actually this is going to work because the double the flow will convert to double what I meant to do
was pi equals milestone
actually works interchangeably news to me.
OK.
So this is definitely a float though.
Oh that's why.
Ok guys I'm not smoking crack I promise.
This is a it's because it was both a double and like tripping out.
Why is it not working.
OK.
So there you go.
OK.
So pi is a float.
Here we go.
Can assign value of type double to type float.
So clearly this is a double.
And clearly this is a float.
So the point I was trying to make is by default it always chooses a double when you use the decimal
points.
And so when you're creating your apps you're only going to really ever use and doubles for the most
part when you're working with numbers which is really cool.
And I encourage you to actually read a whole lot more in this swift book about wherever it went about
all these different things because they has incredibly valuable information incredibly valuable and
it's actually Britain written with the proper keywords because sometimes you may hear an instructor
or somebody talking about programming in general they're not using the right terms or summarizing it
Swift actually in those books they actually use the right keywords and they teach what's going on under
the hood in a lot of cases so those numbers right and you can do a lot of things with numbers.
And we talked in the earlier video about operators.
You're going to use what's called a rith.
Take my math.
Oh my gosh.
I'm sorry I just the arithmetic arithmetic.
All right.
Rith lactic arithmetic operators.
If you're new to the class and you're wondering like why does he why does he leave some of the stuff
in here like why do we make mistakes and things like that.
And what happened was in the beginning I didn't always do that.
I kind of cleaned up the code and made it look perfect.
Sounds like you're doing it so perfectly.
You're just so amazing.
I'm never going to be a programmer because you can't do it perfectly.
And I was it guys and gals.
I'm not doing it perfectly.
This is all edited.
So I started making videos that were real like how programmers really solve problems.
And then everyone's like I can do this.
Mark prizes he's not a robot he's a real person who has to figure things out and use his brain.
And that's why we do these things.
That's why these videos are so informal because I pretend that you're sitting here right next to me
in the class so I can actually teach and convey a message to you.
So again if you're new to all this this is how we do it.
All right dev slopes and it's exciting and people love it.
If you hate it I'm sorry I just love it.
OK.
So arithmetic operators you know and this is just like in math and in high school right.
Or grade school.
Right.
You know you get plus minus divide multiplication and things like that.
And so you can actually you can actually play around with numbers which is kind of cool.
One of my favorite ones is like doing the area of a shape or a rectangle.
So a bar area equals and let's say lengths of 15 times with 20.
OK.
And we just did multiplication 315 times 20 which is really cool.
And we can do addition as well.
Var some equals five plus six bar diff for difference equals 10 minus 3 32 10 minus 3.
And so we're doing different operations here which is really cool.
What about division.
Can we do division.
Well yes we can we can say let's say 12 divided by groups some division 12 divided by three is four.
OK.
But there's one more thing I want to show you.
OK.
What if you said bar divide and we said what is 12 divided by five.
Do you think it's going to be Hoopes this one equals 12 divided by five.
And again these are just container names were given them.
OK.
So wait a minute.
Twelve divided by five is not two words the remainder.
There's got to be a remainder well in swift and pretty much every other programming language division
or the arithmetic operator for division.
This backslash here is only going to do the division and only give you how many times it goes into a
number.
The remainder actually is not going to be provided to you.
So to get the remainder you actually have to use what's called the remainder operator or modulo or modulus.
I've heard people say all kinds of things in called the modulo operators commonly use or the remainder
operator that's what Swift calls the remainder operator.
So the remainder operator is this equals 12 percent sign 5.
OK so which is two.
There's two of a remainder OK writes of five goes in the 12 two times with the remainder of two.
In fact let's do 13 just to show you that these are different numbers here OK.
So so if I wanted to show the full problem OK I could do something like this.
Var result equals.
This is gonna be a string right.
So the result of 13 divided by five is OK.
We can use string Interpol action here OK to insert a variable in and we can say Div 1 and we'll say
dot for the decimal right with the remainder of.
Not really.
It's not really appropriate.
5 is with a remainder of Arrigo and then we'll put in the remainder.
So the result of 13 divided by five is to with a remainder of three.
OK.
And don't worry if you don't understand all this just yet.
I'm just inserting variables into my string.
Here's the print out to the screen.
So what happens with the remainder operator.
Is it going to do the division.
But instead of returning the first number it's going returned the remainder.
If there is a remainder and this is a great way to determine what numbers are even or odd even numbers
will have no remainder I say if I wanted to know if number was even or odd slow say bar random number
equals 13.
So we can say if random number modulo two equals zero.
So the double equal sign is this something is something true or false you know.
Or is this equal to that it's going to give you a boolean value.
So if random number modulo or remainder operator two equals zero OK you know will print.
This is an even number.
All right.
Else this is an odd number.
OK.
And so look it's printing up.
This is an odd number but what if we turn this to 12.
OK.
So this is an even number.
So this is something that is very commonly used to determine whether numbers are even or odd.
Is this remainder.
Operator.
So you've done some basic arithmetic.
We've talked about different types here like and double and low.
You can use negative numbers you can use positive numbers.
You can do all kinds of fun things.
All right.
And by the way this right here this is a very operator kind of like what we've talked about in the past.
It's operating on one target here making a negative number here.
Just so you know we'll well tip and trick right there.
And so don't worry too much about the ifs analysis you don't understand that just yet or the string
into position that we used here the point of this lesson is to talk about numbers you can do math operations
and of course order of operations are all followed in swift as well as most other programming languages
so I can save our results.
Two equals let's say 15 times.
Five plus seven divided by three.
All right.
And if I want a five and seven to be done first and then I wanted the result of this to be done next
I could do something like that.
So just like you do in math like in high school the order of operations you know still applies the same
that you would do in math in high school and you can use parentheses just like you would do in high
school math to get the results that you are looking for.
And again this is very foundational.
Every app you've ever used uses math like this when you're swiping your card at a bank and it wants
to deduct money it's got to use math to take money out of your bank account things like that.
So you have now used a basic foundational thing that you can use for the rest of your life as a programmer
and in every house that you'll ever build.
So that's it.
Numbers mark up slopes dot com.
See you next.