Author Topic: Rounding or flooring  (Read 1981 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Rounding or flooring
« on: June 04, 2007, 10:32:32 am »
It seems that the Round and RoundTo functions round down, or take the floor (i think its refered to as that, I don't know), but should round according if it is >= .5 or  < .5. A Floor function should round down.
Sry if its not called floor, i can't remember off the top of my head.
« Last Edit: June 04, 2007, 10:41:49 am by DorkeyDear »

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Rounding or flooring
« Reply #1 on: June 04, 2007, 12:19:02 pm »
It seems that the Round and RoundTo functions round down, or take the floor (i think its refered to as that, I don't know), but should round according if it is >= .5 or  < .5. A Floor function should round down.
Sry if its not called floor, i can't remember off the top of my head.

The round functions don't round down, they round to even, as per the IEEE754 floating point specification (Statistically, this results in better distribution of data). Rounding down is floor, rounding up is ceiling, rounding as you want it is either, depending on how you round numbers < 0, either round towards infinite(same as ceiling) or round away from 0.

I'll talk to EnEsCe about possibly changing this.
« Last Edit: June 04, 2007, 12:20:38 pm by chrisgbk »

Offline rhpot03

  • Major(1)
  • Posts: 7
Re: Rounding or flooring
« Reply #2 on: June 06, 2007, 09:46:41 pm »
Code: [Select]
Function RoundDown(num:Extended) : Integer;
begin
  Result := round(num - 0.4999999);
end;
and similarly...
Code: [Select]
Function RoundUp(num:Extended) : Integer;
begin
  Result := round(num + 0.4999999);
end;

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Rounding or flooring
« Reply #3 on: June 06, 2007, 11:05:35 pm »
Code: [Select]
Function RoundDown(num:Extended) : Integer;
begin
  Result := round(num - 0.4999999);
end;
and similarly...
Code: [Select]
Function RoundUp(num:Extended) : Integer;
begin
  Result := round(num + 0.4999999);
end;

These are not guaranteed to work; as an example, using RoundUp with 2.0000001 will result in an answer of 2; similarily using RoundDown with 3.9999999 will result in an answer of 4.


Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Rounding or flooring
« Reply #4 on: June 06, 2007, 11:30:02 pm »
Code: [Select]
function RoundDown(num:Extended): Integer;
begin
    Result := round(num);
    if Result > num then
        Result := Result - 1;
end;

Code: [Select]
function RoundUp(num:Extended): Integer;
begin
    Result := round(num);
    if Result < num then
        Result := Result + 1;
end;

I think this could work.
urraka

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Rounding or flooring
« Reply #5 on: June 07, 2007, 10:54:16 pm »
Code: [Select]
function RoundDown(num:Extended): Integer;
begin
    Result := round(num);
    if Result > num then
        Result := Result - 1;
end;

Code: [Select]
function RoundUp(num:Extended): Integer;
begin
    Result := round(num);
    if Result < num then
        Result := Result + 1;
end;

I think this could work.

rounddown(2.4) = 2
rounddown(2.5) = 2
rounddown(2.6) = 3 - 1 = 2
rounddown(3.4) = 3
rounddown(3.5) = 4 - 1 = 3
rounddown(3.6) = 4 - 1 = 3

roundup(2.4) = 2 + 1 = 3
roundup(2.5) = 2 + 1 = 3
roundup(2.6) = 3
roundup(3.4) = 3 + 1 = 4
roundup(3.5) = 4
roundup(3.6) = 4

Yep, correct.

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Rounding or flooring
« Reply #6 on: June 12, 2007, 03:20:24 pm »
The logic is correct but it doesn't work =/
There is a type mismatch error, I guess you can't compare Extended with Integer, and as I'm not a Pascal (or Delphi?) programmer I don't know how to convert it to match types.
urraka

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: Rounding or flooring
« Reply #7 on: June 12, 2007, 09:47:41 pm »
Code: [Select]
strtoint(floattostr(<ExtendedVariableHere>));

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Rounding or flooring
« Reply #8 on: June 12, 2007, 10:20:09 pm »
This would be the code then:

Code: [Select]
function RoundDown(num:Extended): Integer;
begin
    Result := round(num);
    if StrToFloat(IntToStr(Result)) > num then
        Result := Result - 1;
end;

Code: [Select]
function RoundUp(num:Extended): Integer;
begin
    Result := round(num);
    if StrToFloat(IntToStr(Result)) < num then
        Result := Result + 1;
end;

Float can be compared with Extended i tested it.
urraka