0:00

[MUSIC]

So, the next thing which I want to cover are numbers.

All right, so numbers in general are either integers or floating points.

So integers are numbers that don't have a decimal point.

You could also say no fraction or whole numbers.

All right, 5, 7, 9, 250, those are all integers.

Now, one thing which is tricky, computers don't store data in those kind of

numbers or as we would look at them as almost like strings.

Computers store data in bits and bytes.

So integers are then stored in either integer 8, 16, 32, or 64 bit.

All right? And so, if you look at, for example,

Int64, you can actually calculate Int64.max.

So, this is if you print out the maximum of Int64,

what you get basically is 2 to the power of 63.

What you need to take into account and I show that on the next slide,

on the code actually, is we also having a positive and negative.

We don't have unsigned integers, we have signed integers.

1:13

One little concern here, so if this is the maximum value which you can

have as an Int64, basically you have a 64 bit.

If you will declare a variable, variable i has the type Int32,

then you basically have only 32 bits to represent that number, and

you can only represent with 32 bits a much smaller number.

All right, so if you would then assign that big value, you would get an error.

So you basically overrun the range of that integer.

If you work with integers as well as with routes you

can use the following operators.

So operator equal is an assignment, right, be careful with that.

So equal is an assignment.

You can do a plus or a minus, right, so you can add or subtract, you can divide.

Plus plus, you increment by one.

Minus minus you decrement by one.

Plus equal, so 5 += 7 is 12, so plus equal is a shortcut, basically.

And minus equal.

And then percent is a division modular.

So like say 10 % 3 is 3, right?

So, sorry, 10 % 3 is 1.

So there's only 1 left in the end.

Be careful with division.

So when you do something like 11 divided by 3, what you get as a result is 3.

A little bit unexpected.

Background there is if you would do this whole thing as a floating point operation,

you would get something like 10 divided by 3 is 3.66667.

And you would expect maybe that 11 by 3 would then

be rounded up to 4 correctly, but it's not, it's truncated.

So that's something you just need to be aware of.

3:00

So the next thing to look at is floating points, so floating points or doubles.

So computers store them in mantissa and exponent, like regular math, right?

So what you get, basically,

is like something like a 1.23456 to the power of 10, 10 to the power of 5.

Right, times 10 to the power of 5.

So computers would store the information like that, but not exactly.

So what you need to take into account is, instead of using 10 as a base,

computers use 2 as a base.

3:29

So Swift uses floating point with 32-bit values or double with 64-bit values.

And we could do something similar to what I did on the previous slide,

show you the maximum of what you can reach with there.

But what you're really looking for here is precision.

So in the internals basically, it compiles the code and

then calculates it back to the form, which I show up there, with a base of two.

3:54

And so what you get from there,

without getting too deep into it, is that many mathematical numbers can actually

not be really stored 100% accurate in that format.

That can be somewhat tricky.

Like, the computer, for example, would take the number 1.2 and

store it as something like 1.2 and then as many bits as you have and

then at the end a 1, all right?

Or as 1.19999999.

So that can be difficult and

certain things the equal operator might actually show that something is not equal,

even though when you look at the number, it's like hey, it's really equal.

All right, so you need to be aware of that.

So what that means is if you would do scientific math,

you would actually use a math library.

That's the same actually also with other languages like in C# or

in Java you would use math libraries, right?

Because you don't get the absolute finest precision in this case and

you can get actually surprises.

So yeah, I'll show you here some code examples.

So first thing here, integer variables, right?

So I just showed you we have different ways of declaring an integer.

So if I declare a variable as an integer, like here for example,

I have a constant a : Int16 = 10.

All right.

So what I do here is, this constant now is integer 16,

and integer 16 has a certain maximum value.

Right, if I show that, here I can add here a line, say,

okay, what is my maximum of integer 16?

Then all I can reach is actually 32,767.

That's my maximum.

If I look at the minimum, my minimum value,

which I can reach in that case is minus 32,768.

All right, so that would be an integer 16.

Integer 32 has a bigger number, right?

Minimum, and maximum of integer 32, until we reach integer 64.

All right, so integer 64 is a fairly large number in this case.

And let's operate a little bit on that.

So if I take actually two of these and

say, okay, let's say let c = a + b,

all right, if I would do that, I get an error.

6:40

So the error which I get is that I cannot apply the plus operator if I

use a type integer 16 and an integer 32.

So I need to do something to be able actually to assign those correctly.

Now one thing which you notice here, so I delete that one here.

So if I have c and I want to assign a plus b,

what I need to do is, I need to make sure that the type is compatible.

So my b is an integer 32.

One thing to find out is, so in this case, if I take my a and

make it an integer, integer will be an integer 32, then I can make my assignment.

7:31

If I look at floating point variables next.

So if I declare here a constant d : Double 1.1 or

constant f floating point 1.1.

So what happens is, the precision might be different, right?

So if I divide 2 by d, which is a double, then I get values in this precision,

which you see on the left, on the right side.

And if you divide it by floating point, then we get a different value here.

All right, so the precision is very important when you write your code,

to take that into account.

If you declare a variable implicitly, so

then implicitly what you will get is actually automatically a double.

You can prove that actually by if I declare here a double as 1.1, and

then divide 2 by that double, I get the same as if I declare here,

if I divide 2 by the double.

Right, so in this case, per default it

gets actually inferred as a double.

9:10

So that's another part where you need to be aware of.

Division, so like I showed you the list of operators, right?

So in this case, it's 12.4/5, so

regular division, right, gives you 2.48, so

that actually turns out to be not an infinite number.

And then, the mod operator, the percent, so 12.4 % 5 will give you a 2.4,

because what it really gives you is the leftover, right, so

it's the 2.4 after you took 2 times 5, 10 off.

[MUSIC]