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.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | Real numbers can be converted to integers in many ways. Here's a comparison some of them. | |
| 2 | 2 | ||
| 3 | 3 | ``` pascal | |
| 4 | 4 | program ComparisonOfApproachesForRoundingToInteger(input,output); | |
| 5 | 5 | // Comparison of approaches for rounding to an integer | |
| 6 | 6 | ||
| 7 | 7 | uses SysUtils,Math; | |
| 8 | 8 | ||
| 9 | 9 | function RoundISO7185(x:real):integer; | |
| 10 | 10 | // If x is positive or zero, round(x) shall be equivalent to trunc(x+0 .5) ; | |
| 11 | 11 | // otherwise, round(x) shall be equivalent to trunc(x-0.5) | |
| 12 | 12 | begin | |
| 13 | 13 | If x >= 0 then result := trunc(x+0.5) | |
| 14 | 14 | else result := trunc(x-0.5); | |
| 15 | 15 | end; | |
| 16 | 16 | ||
| 17 | 17 | var | |
| 18 | 18 | r1,r2,r3,r4:real; | |
| 19 | 19 | fmt : string; | |
| 20 | 20 | begin | |
| 21 | 21 | r1 := 1.3; | |
| 22 | 22 | r2 := 1.6; | |
| 23 | 23 | r3 := 3.5; | |
| 24 | 24 | r4 := 4.5; | |
| 25 | 25 | fmt := '%10s%10s%10s%10s%10s%10s%10s'; | |
| 26 | 26 | WriteLn(format(fmt,['Value','Write:x:0','Trunc','Round','ISO7185','Floor','Ceil'])); | |
| 27 | 27 | fmt := '%10d%10d%10d%10d%10d'; | |
| 28 | 28 | write (r1:10:1,r1:10:0); | |
| 29 | 29 | WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)])); | |
| 30 | 30 | write (r2:10:1,r2:10:0); | |
| 31 | 31 | WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)])); | |
| 32 | 32 | write (r3:10:1,r3:10:0); | |
| 33 | 33 | WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)])); | |
| 34 | 34 | write (r4:10:1,r4:10:0); | |
| 35 | 35 | WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)])); | |
| 36 | 36 | r1 := r1 *-1; | |
| 37 | 37 | r2 := r2 *-1; | |
| 38 | 38 | r3 := r3 *-1; | |
| 39 | 39 | r4 := r4 *-1; | |
| 40 | 40 | write (r1:10:1,r1:10:0); | |
| 41 | 41 | WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)])); | |
| 42 | 42 | write (r2:10:1,r2:10:0); | |
| 43 | 43 | WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)])); | |
| 44 | 44 | write (r3:10:1,r3:10:0); | |
| 45 | 45 | WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)])); | |
| 46 | 46 | write (r4:10:1,r4:10:0); | |
| 47 | 47 | WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)])); | |
| 48 | 48 | Readln; | |
| 49 | 49 | end. | |
| 50 | 50 | ``` | |
| 51 | 51 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 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 | + | ``` | |
| 62 | 64 | ||
| 63 | 65 | ## See also | |
| 64 | 66 | ||
| 65 | 67 | - [[Round|round]] | |
| 66 | 68 | - [[Trunc|trunc]] - round towards zero | |
| 67 | 69 | - [`math.ceil`](https://www.freepascal.org/docs-html/rtl/math/ceil.html) - round up | |
| 68 | 70 | - [`math.floor`](https://www.freepascal.org/docs-html/rtl/math/floor.html) - round down | |
| 69 | 71 | ||
| 70 | 72 | - [[Format function]] | |
| 71 | 73 | ||
| 72 | 74 | --- | |
| 73 | 75 | *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).* |
