06c_roundingFunctions

Download Report

Transcript 06c_roundingFunctions

Rounding Functions
Round UP, round DOWN, round with
respect to 0.5 … how and why?
1.
round(), floor(), ceil()
2.
Lots of examples.
1
1. Rounding functions

Many built-in functions allow to round up and down
fractional numbers to whole numbers.

The most common are:
round() rounds with respect to 0.5
floor() rounds towards negative infinity.
ceil() rounds towards positive infinity.
2
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y=ceil(x)
y =
y =
y =
y =
y =
y =
y =
y =
y =
y =
46
2.434
y =
2
1
rand
y =
0 or 1…
it depends
0
-2.434
y =
?
-45.754
y =
?
-∞
3
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y=ceil(x)
y =
y =
y =
y =
y =
y =
y =
y =
y =
y =
46
2.434
y =
2
1
rand
y =
0 or 1…
it depends
0
-2.434
y =
-2
-45.754
y =
-46
-∞
4
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y =
46
2.434
y =
rand
y =
y =
-2.434
y =
y =
y =
y =
y =
?
y =
-46
-∞
y =
0
-2
-45.754
y =
2
0 or 1…
it depends
0
y =
45
2
1
y=ceil(x)
y =
?
0
Rounds towards -∞
5
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y =
46
2.434
y =
rand
y =
y =
-2.434
y =
y =
y =
y =
y =
-3
y =
-46
-∞
y =
0
-2
-45.754
y =
2
0 or 1…
it depends
0
y =
45
2
1
y=ceil(x)
y =
-46
0
Rounds towards -∞
6
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y =
46
2.434
y =
rand
y =
y =
-2.434
1
y =
-3
y =
-46
-∞
y =
y =
y =
3
0
-2
-45.754
y =
y =
y =
46
2
0 or 1…
it depends
0
y =
45
2
1
y=ceil(x)
?
y =
-46
Rounds towards +∞
?
0
7
Rounding functions, example
+∞
x=
45.754
y=round(x)
y =
y=floor(x)
y =
46
2.434
y =
rand
y =
y =
-2.434
y =
y =
y =
1
y =
-3
y =
-46
3
0
-2
-45.754
y =
y =
y =
46
2
0 or 1…
it depends
0
y =
45
2
1
y=ceil(x)
-2
y =
-46
-45
-∞
8
Ex1. Combining rand with ceil
9
Ex1. combining rand with ceil

MATLAB generates a random number, manipulates it
with arithmetic, then rounds it!
startPlayer = ceil(rand*totalPlayer);

rand*totalPlayer

ceil(rand*totalPlayer) gives
gave
-1
0
1
2
totalPlayer
-1
0
1
2
totalPlayer
Thus, the random value for startPlayer is a whole number
between 1 and the total number of players!
10
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?


0
1
2
10
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
From 0 to -10, that’s -10 moved over.
-10
0
11
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?



0
1
2
10
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
From 0 to -10, that’s -10 moved over.
-10
0
Step by step:

0 < rand < 1
%start with 1 random value
12
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?



0
1
2
10
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
From 0 to -10, that’s -10 moved over.
-10
0
Step by step:


0 < rand < 1
0 < rand*21 < 21
%start with 1 random value
%stretch the interval
13
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?



0
1
2
10
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
From 0 to -10, that’s -10 moved over.
-10
0
Step by step:



0 < rand < 1
0 < rand*21 < 21
0 ≤ floor(rand*21) ≤ 20
%start with 1 random value
%stretch the interval
%all whole numbers now
14
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?



0
1
2
10
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
From 0 to -10, that’s -10 moved over.
-10
0
Step by step:




0 < rand < 1
0 < rand*21 < 21
0 ≤ floor(rand*21) ≤ 20
-10 ≤ floor(rand*21)-10 ≤ 10
%start with 1 random value
%stretch the interval
%all whole numbers now
%move the interval
15
Ex2. Generate a random value
between -10 and 10 inclusive

Some ideas:

How stretched should the interval be?



1
2
10
From 0 to -10, that’s -10 moved over.
-10
0
Step by step:





0
From -10 to 10, that’s 21 values (include the 0).
How much should it “moved over”?

-1
0 < rand < 1
0 < rand*21 < 21
0 ≤ floor(rand*21) ≤ 20
-10 ≤ floor(rand*21)-10 ≤ 10
%start with 1 random value
%stretch the interval
%all whole numbers now
%move the interval
Final command:
value = floor(rand * 21) – 10;
16
Ex3. Random events

Suppose you are to develop a software that allows (or
not) a rocket to takeoff based on a series of safety
decisions. Every decision to continue relies on sensors
placed on the rocket.




Is the propellant loaded?
Are the doors locked?
Is there lightning in the area? …
How does a programmer ‘test’ such events without
having a rocket?
17
Ex3. First criteria, lightning
% check for lightning sensor
if no lightning
% proceed to next decision
else
% abort countdown
end
18
Ex3. Second criteria, doors
% check for lightning sensor
if no lightning
% check doors locked/secure sensor
if all doors secured
% check next decision
else
% abort
end
else
% abort countdown
end
19
Ex3. First criteria, coded.
% check for lightning sensor
lightning_sensor = round(rand);
if lightning_sensor == 1 %1 means no lightning
% check doors locked/secure sensor
if all doors secured
% check next decision
else
% abort
end
else % a zero means “abort countdown”
disp(‘Lightning in area. Launch aborted.’);
end
20
Ex3. Second criteria, coded.
% check for lightning sensor
lightning_sensor = round(rand);
if lightning_sensor == 1 %no lightning
% check doors locked/secure sensor
doors_sensor = floor(rand*2);
if doors_sensor == 1 % 1 means all doors secured
% check next decision
< to be continued here… >
else
% abort
disp(‘Doors not secured. Launch aborted.’);
end
else % abort countdown
disp(‘Lightning in area. Launch aborted.’);
end
21
Ex3. Eventually, replace by …
% check for lightning sensor
lightning_sensor = round(rand);
if lightning_sensor == 1 %no lightning
% check doors locked/secure sensor
doors_sensor = floor(rand*2);
if doors_sensor == 1 % 1 means all doors secured
% check next decision
< to be continued here… >
else
% abort
disp(‘Doors not secured. Launch aborted.’);
end
else % abort countdown
disp(‘Lightning in area. Launch aborted.’);
end
22
Wrapping Up





round(), floor(), and ceil() are all built-in
functions that make fractional numbers become whole
numbers.
round() rounds towards the nearest integer.
floor() rounds down towards negative infinity.
ceil() rounds up towards positive infinity.
Combine the rounding functions with regular arithmetic
operations to produce any combination of whole
numbers and decimal numbers wanted!
23