Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,730 --> 00:00:10,470
Before we go too much further into the course let's quickly talk about rounding and how to avoid some
2
00:00:10,470 --> 00:00:12,060
rounding errors.
3
00:00:12,060 --> 00:00:16,770
We've got a combat scene here where damage is being taken damage is being dealt and you can see that
4
00:00:16,770 --> 00:00:18,480
the values are being rounded.
5
00:00:18,480 --> 00:00:23,970
However you may also notice the values aren't being rounded quite the way you might expect.
6
00:00:24,150 --> 00:00:28,830
So we're probably already familiar with the traditional way that we round a number where say we have
7
00:00:28,830 --> 00:00:32,150
a number two point seventy three for example.
8
00:00:32,250 --> 00:00:34,440
And we want around that to one decimal place.
9
00:00:34,440 --> 00:00:40,710
Well we look at the next number after our cutoff and we say that if that number is greater than or equal
10
00:00:40,710 --> 00:00:44,620
to 5 then the number before it is increased by 1.
11
00:00:44,730 --> 00:00:48,060
And if it's less than five then we just leave that number as is.
12
00:00:48,060 --> 00:00:52,020
So two point seventy three would be rounded to two point seven.
13
00:00:52,020 --> 00:00:57,320
Or if we were going to the nearest whole number then the seven is greater than five.
14
00:00:57,330 --> 00:01:01,810
So the two point seven would round to three nice and simple.
15
00:01:01,920 --> 00:01:05,850
One thing to watch out for is when the number that needs to change is a nine.
16
00:01:05,850 --> 00:01:11,700
Let's take an extreme example of nine point nine nine and try rounding that to one decimal place.
17
00:01:11,700 --> 00:01:14,790
Well this would cause this nine here to equal 10.
18
00:01:14,820 --> 00:01:18,160
Now if we can't just put 10 in that column.
19
00:01:18,240 --> 00:01:21,350
So we need to put in a zero and carry the one.
20
00:01:21,540 --> 00:01:24,900
We then move over to the next number on the add that one we carried.
21
00:01:24,900 --> 00:01:27,690
And that would give us 10 again.
22
00:01:27,720 --> 00:01:31,960
And so in this example nine point nine nine would round to ten point zero.
23
00:01:32,720 --> 00:01:37,080
It's important to note that whenever you need to give an answer to a certain level of precision in this
24
00:01:37,080 --> 00:01:43,800
case one decimal place you need to keep those extra digits in there even if they are just zeros.
25
00:01:43,800 --> 00:01:48,300
So in this case the answer is ten point zero not just 10.
26
00:01:48,300 --> 00:01:52,550
It seems quite pedantic but it can become important in certain circumstances.
27
00:01:52,560 --> 00:01:58,730
So it's worth remembering another option would be to take something called the floor of a number.
28
00:01:58,800 --> 00:02:03,570
And what this essentially does is always rounds the number down to the nearest whole integer.
29
00:02:03,810 --> 00:02:08,940
So regardless of what decimal places we have we essentially ignore them and round down there in our
30
00:02:08,940 --> 00:02:11,600
example of two point seven three over here.
31
00:02:11,700 --> 00:02:18,970
The floor would be to the opposite of this would be something called the ceiling of a number.
32
00:02:18,970 --> 00:02:26,650
And what this does is always rounds the number up so again with our example of two point seven three
33
00:02:26,800 --> 00:02:29,810
this would round up to three like it would normally.
34
00:02:29,970 --> 00:02:37,410
But if we have another number say three point two this would round up to four and for the eagle eyed
35
00:02:37,410 --> 00:02:41,490
among you you may know that that was what was actually happening in that combat clip at the start of
36
00:02:41,490 --> 00:02:45,280
the video so why do we care about all of this.
37
00:02:45,280 --> 00:02:47,380
Why don't we just round normally and be done with it.
38
00:02:47,830 --> 00:02:51,320
Well let's say we have an enemy who is attacking our player.
39
00:02:51,430 --> 00:02:57,340
This enemy is very weak compared to our player and only manages to inflict zero point six to points
40
00:02:57,340 --> 00:03:01,090
of damage after some other modifiers have been added to it.
41
00:03:01,480 --> 00:03:06,700
If we choose to round normally then this would do one point of damage but if it were any weaker then
42
00:03:06,760 --> 00:03:12,430
it would probably do none at all or we could choose to be generous to our player and take the floor
43
00:03:12,430 --> 00:03:13,600
of the number.
44
00:03:13,600 --> 00:03:17,370
In essence we're saying the damage is always rounded in the player's favor.
45
00:03:17,650 --> 00:03:20,090
And in this case it would do no damage at all.
46
00:03:20,970 --> 00:03:26,730
Or we could do the opposite and be a little more harsh and always round the damage up by taking the
47
00:03:26,730 --> 00:03:31,740
ceiling of the number an enemy would always be able to inflict some small amount of damage no matter
48
00:03:31,770 --> 00:03:34,710
how weak it is in comparison to our player.
49
00:03:34,810 --> 00:03:39,510
And this means that even the lowest level monster can kill our hero if they're on their absolute last
50
00:03:39,510 --> 00:03:45,330
legs with all that said one thing to note at this point is that whenever you're doing a calculation
51
00:03:45,330 --> 00:03:49,480
of any kind you should always perform the rounding last.
52
00:03:49,500 --> 00:03:54,180
Otherwise you can start to introduce small errors and may compound over time.
53
00:03:54,210 --> 00:04:04,100
For example let's take 5.1 times four point seven and then add 2.5 to that if we do that sum then it
54
00:04:04,100 --> 00:04:06,920
gives us twenty six point four seven.
55
00:04:07,040 --> 00:04:10,000
And if we round that then we get 26.
56
00:04:10,010 --> 00:04:16,700
However if we round those numbers first say some function is returning a rounded version of the calculation
57
00:04:17,270 --> 00:04:22,250
we get five times five plus three and that equals twenty eight.
58
00:04:23,150 --> 00:04:26,390
And as you can see the end results are quite different.
59
00:04:27,320 --> 00:04:32,300
So be very cautious when you're doing these sorts of thing to make sure these errors don't stack up
60
00:04:32,300 --> 00:04:37,790
over time because this can lead to all sorts of weird and wonderful and unexpected behaviors in your
61
00:04:37,790 --> 00:04:40,730
game with all of that out the way.
62
00:04:40,730 --> 00:04:44,330
Let's look at how we might implement this in code.
63
00:04:44,330 --> 00:04:50,780
Most modern languages have some sort of math library that you can access and the syntax is fairly similar
64
00:04:50,780 --> 00:04:51,810
between each one.
65
00:04:52,850 --> 00:04:56,330
So for rounding we can use math dot round.
66
00:04:56,510 --> 00:05:02,540
And then in the parentheses you'd include the number you want to round and the level of precision that
67
00:05:02,540 --> 00:05:04,070
you're looking for.
68
00:05:04,070 --> 00:05:12,990
So this would be say 1 for one decimal place or five for five decimal places etc. the floor and the
69
00:05:12,990 --> 00:05:18,840
ceiling are very similar to this except we don't need to specify any level of precision since we're
70
00:05:18,840 --> 00:05:27,190
always just rounding to the nearest whole number so we have math dot floor passing in our number and
71
00:05:27,190 --> 00:05:31,200
we have math dot seal passing in our number.
72
00:05:31,270 --> 00:05:38,050
Notice though that the method for sealing has been abbreviated to seal here CEI L and that's usually
73
00:05:38,050 --> 00:05:39,280
the case across the board.
74
00:05:39,310 --> 00:05:41,570
So it's worth keeping in mind.
75
00:05:41,710 --> 00:05:42,060
Okay.
76
00:05:42,070 --> 00:05:46,900
So hopefully that clears up how rounding works and how to avoid some of the common pitfalls you might
77
00:05:46,900 --> 00:05:48,190
come across.
78
00:05:48,280 --> 00:05:53,620
We haven't covered rounding with significant figures yet but we'll cover that when we get on to scientific
79
00:05:53,620 --> 00:05:56,900
notation finally.
80
00:05:56,920 --> 00:05:58,650
It's time for your challenge.
81
00:05:58,720 --> 00:06:04,900
So I'd like you to take the number six point two eight three and I'd like you to find the flaw of this
82
00:06:04,900 --> 00:06:05,940
number.
83
00:06:05,950 --> 00:06:13,390
I'd also like you'd find the ceiling of the number and I'd like you to find the number two 1 and 2 decimal
84
00:06:13,390 --> 00:06:14,170
places.
85
00:06:14,170 --> 00:06:19,140
So once you've got your answer pop it in the community forum and I'll see you in the next lecture.
9131
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.