Sign in Join
Wiki copytemporary

Comparison of approaches for rounding to an integer

Real numbers can be converted to integers in many ways. Here's a comparison some of them.

pascal
program ComparisonOfApproachesForRoundingToInteger(input,output);
// Comparison of approaches for rounding to an integer

uses  SysUtils,Math;

function RoundISO7185(x:real):integer;
// If x is positive or zero, round(x) shall be equivalent to trunc(x+0 .5) ;
// otherwise, round(x) shall be equivalent to trunc(x-0.5)
begin
  If x >= 0 then result := trunc(x+0.5)
    else result := trunc(x-0.5);
end;

var
  r1,r2,r3,r4:real;
  fmt : string;
begin
  r1 := 1.3;
  r2 := 1.6;
  r3 := 3.5;
  r4 := 4.5;
  fmt := '%10s%10s%10s%10s%10s%10s%10s';
  WriteLn(format(fmt,['Value','Write:x:0','Trunc','Round','ISO7185','Floor','Ceil']));
  fmt := '%10d%10d%10d%10d%10d';
  write (r1:10:1,r1:10:0);
  WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)]));
  write (r2:10:1,r2:10:0);
  WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)]));
  write (r3:10:1,r3:10:0);
  WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)]));
  write (r4:10:1,r4:10:0);
  WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)]));
  r1 := r1 *-1;
  r2 := r2 *-1;
  r3 := r3 *-1;
  r4 := r4 *-1;
  write (r1:10:1,r1:10:0);
  WriteLn(format(fmt,[Trunc(r1),Round(r1),RoundISO7185(r1),floor(r1),ceil(r1)]));
  write (r2:10:1,r2:10:0);
  WriteLn(format(fmt,[Trunc(r2),Round(r2),RoundISO7185(r2),floor(r2),ceil(r2)]));
  write (r3:10:1,r3:10:0);
  WriteLn(format(fmt,[Trunc(r3),Round(r3),RoundISO7185(r3),floor(r3),ceil(r3)]));
  write (r4:10:1,r4:10:0);
  WriteLn(format(fmt,[Trunc(r4),Round(r4),RoundISO7185(r4),floor(r4),ceil(r4)]));
  Readln;
end.
text
 Output:
 Value Write:x:0     Trunc     Round   ISO7185     Floor      Ceil
   1.3         1         1         1         1         1         2
   1.6         2         1         2         2         1         2
   3.5         4         3         4         4         3         4
   4.5         5         4         4         5         4         5
  -1.3        -1        -1        -1        -1        -2        -1
  -1.6        -2        -1        -2        -2        -2        -1
  -3.5        -4        -3        -4        -4        -4        -3
  -4.5        -5        -4        -4        -5        -5        -4

See also


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.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2019–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
DjzepiCreated page
Kai Burghardtresolve Category: Pages using deprecated enclose attributes (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.