|
In some cases we are not interested in digits after the decimal point.
Such as the weight of a steak being 0,52 or 0.53 pound or
the width of a door being 30.85 or 30.86 inches.
In these cases we round he number to the relevant amount of digits.
The rounding rule (to the nearest integer) is:
values of x.0 to x.499... become x after rounding
values of x.5 to x.999... become x+1.
Examples:
2.34 becomes 2
2.57 becomes 3
Instead of rounding to the nearest integer we may round as well to any multiple (0.1, 10, 20, 1000..).
Nobody is interested if a football stadium counts 43739 or 43740 spectators.
We use to round this number to the nearest multiple of 1000, which makes more sense.
Rounding to the neares multiple of 1000:
Divide the number by 1000, which yields 43.739.
Round to nearest integer : 44.
Multiply by 1000, the answer is 44000.
Rounding down to nearest multiple.
This is called truncation.
Someones age is truncated to whole years.
An age of 57.9 years is rounded down to 57 years.
Examples of truncation:
23.9 becomes 23
23.4 becomes 23.
Rounding down 2345 at multiples of 15:
2345 / 15 = 156.333 .
Delete digits right of decimal point: 156.
Answer is 156 * 15 = 2340.
Rounding upwards:
23.79 becomes 24
23.01 becomes 24
Rounding up 98765 to a multiple of 25:
98765 / 25 = 3950.6
Roundig up to integer: 3951
Answer is 3951 * 25 = 98775.
Rounding and the Delphi programming language.
Delphi (and other programming languages) have operators and functions for the rounding of numbers.
operator div divides integers and the quotient is also integer.
operator mod divides integers and the result is the remainder.
function round rounds a floating point value to the nearest integer.
function trunc rounds a floating point value down to the nearest integer.
Examples:
11 div 3 = 3.
26 div 8 = 3.
22 mod 5 = 2.
33 mod 11 = 0.
34 mod 7 = 6.
round(56.12) = 56
round(99.5) = 100
trunc(60.77) = 60
trunc(0.765) = 0
Note:
N div m has results 0,1,2....etc.
N mod m has results 0,1,2,..,m-1.
This may cause confusion because in many cases we count 1,2,3,.. instead of 0,1,2,3,...
Problem:
When landslides are feared, the 945 residents of a mountain village have to be evacuated.
Busses are used that give place to 65 people.
Question:
How many buses (rides) are needed?
945 / 65 = 14,54
Of course this number must be rounded upward, so 15 is the answer.
Question:
Which person will be placed in which bus?
Busses are assigned a number B = 1,2,3...
Persons are assiged a number as well: P = 1,2,3...
Person P uses bus B = ((P-1) div 65) + 1.
Question:
The seats in a bus are numbered S = 1,2,3,...
Persons are placed sequential to their personal number.
Which seat is used by person P?
S = ((P-1) mod 65) + 1.
Question:
Which person has seat S of bus B?
P = (B-1)*65 + S.
Summary
Number A is de rounding of N to the multiple of m.
Integer N:
Nearest: A = ((N + m/2) div m) * m .... (if m is multiple of 2)
Down: A = (N div m) * m.
Up: A = ((N + m-1) div m) * m.
Floating point number N:
Nearest: A = round(N/m) * m
Or: A = trunc(N/m + 0.5) * m.
Down: A = trunc(N/m) * m.
Or A = round(N/m-0.5) * m.
Up: A = round(N/m + 0.5) * m.
Or A = trunc(N/m + 0,9999) * m.
|
|