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