Search code examples
delphiinteger-overflowdelphi-5floor

Math.Floor overflow in delphi 5


I'm working on an old school Delphi 5 application and I've noticed something about the Math.Floor function.

I have an expression that I would like to evaluate the Floor of

expression

But when I calculate the floor

Floor

I end up with a value that is clearly not correct, presumably due to integer overflow.

I thought of using the modulo operator as suggested here, but it looks like mod isn't applicable for these operands.

There's got to be a way ...


Solution

  • Thanks to David A's suggestion:

    function CardinalFloor(X: Extended): Cardinal;
    begin
      Assert(X>=0);
      Result := Cardinal(Trunc(X));
    end;
    

    is one possible work around