Sign in Join
Wiki copytemporary

Comparison of approaches for rounding to an integer: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Real numbers can be converted to integers in many ways. Here's a comparison some of them.
22
33``` pascal
44program ComparisonOfApproachesForRoundingToInteger(input,output);
55// Comparison of approaches for rounding to an integer
66
77uses SysUtils,Math;
88
99function RoundISO7185(x:real):integer;
1010// If x is positive or zero, round(x) shall be equivalent to trunc(x+0 .5) ;
1111// otherwise, round(x) shall be equivalent to trunc(x-0.5)
1212begin
1313 If x >= 0 then result := trunc(x+0.5)
1414 else result := trunc(x-0.5);
1515end;
1616
1717var
1818 r1,r2,r3,r4:real;
1919 fmt : string;
2020begin
2121 r1 := 1.3;
2222 r2 := 1.6;
2323 r3 := 3.5;
2424 r4 := 4.5;
2525 fmt := '%10s%10s%10s%10s%10s%10s%10s';
2626 WriteLn(format(fmt,['Value','Write:x:0','Trunc','Round','ISO7185','Floor','Ceil']));
2727 fmt := '%10d%10d%10d%10d%10d';
2828 write (r1:10:1,r1:10:0);
2929 WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)]));
3030 write (r2:10:1,r2:10:0);
3131 WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)]));
3232 write (r3:10:1,r3:10:0);
3333 WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)]));
3434 write (r4:10:1,r4:10:0);
3535 WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)]));
3636 r1 := r1 *-1;
3737 r2 := r2 *-1;
3838 r3 := r3 *-1;
3939 r4 := r4 *-1;
4040 write (r1:10:1,r1:10:0);
4141 WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)]));
4242 write (r2:10:1,r2:10:0);
4343 WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)]));
4444 write (r3:10:1,r3:10:0);
4545 WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)]));
4646 write (r4:10:1,r4:10:0);
4747 WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)]));
4848 Readln;
4949end.
5050```
5151
52-` Output:`\
53-` Value Write:x:0     Trunc     Round   ISO7185     Floor      Ceil`\
54-`   1.3         1         1         1         1         1         2`\
55-`   1.6         2         1         2         2         1         2`\
56-`   3.5         4         3         4         4         3         4`\
57-`   4.5         5         4         4         5         4         5`\
58-`  -1.3        -1        -1        -1        -1        -2        -1`\
59-`  -1.6        -2        -1        -2        -2        -2        -1`\
60-`  -3.5        -4        -3        -4        -4        -4        -3`\
61-`  -4.5        -5        -4        -4        -5        -5        -4`
52+```text
53+ Output:
54+ Value Write:x:0 Trunc Round ISO7185 Floor Ceil
55+ 1.3 1 1 1 1 1 2
56+ 1.6 2 1 2 2 1 2
57+ 3.5 4 3 4 4 3 4
58+ 4.5 5 4 4 5 4 5
59+ -1.3 -1 -1 -1 -1 -2 -1
60+ -1.6 -2 -1 -2 -2 -2 -1
61+ -3.5 -4 -3 -4 -4 -4 -3
62+ -4.5 -5 -4 -4 -5 -5 -4
63+```
6264
6365## See also
6466
6567- [[Round|round]]
6668- [[Trunc|trunc]] - round towards zero
6769- [`math.ceil`](https://www.freepascal.org/docs-html/rtl/math/ceil.html) - round up
6870- [`math.floor`](https://www.freepascal.org/docs-html/rtl/math/floor.html) - round down
6971
7072- [[Format function]]
7173
7274---
7375*From the Free Pascal wiki, page "Comparison of approaches for rounding to an integer", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history