Oxygene: / and div operators

Regarding the documentation (https://docs.elementscompiler.com/Oxygene/Expressions/ArithmeticAndLogicalExpressions/#arithmetic-operators) it is not clear what the difference is (and I think that it must be clarified in the documentation).

What I understand:

  • “/” makes a rounded division
  • “div” makes a truncated division (takes integer part of the division, without rounding).

But, for the rounded division: is it rounded towards zero or rounded towards infinity?
This makes a huge difference for negative numbers: in the first case “-3 / 2” returns -1, in the second one, it returns -2.

Note: doing a test, both (-3 / 2) and (-3 div 2) return -1. So why there is a difference in the description of the two operators in the documentation?

IIRC Both operators behave the same way int regular Oxygene mode, with the operands deciding the result type (eg integer/interg -> integer, integer/double -> double). In Delphi Compatibility Mode, / will always create a double, while div (iirc) always creates an integer — eseetally they behave the same way they do in Delphi.

it should be rounded according to proper math rules. ice 2.4 -> 3,. 2.5 -> 3

If only there were a way to find out :wink:

So, the documentation must not be different between the two operators:

Not outside of Delphi Compatibility no. I’ll review.

Thanks, logged as bugs://84528

So the way this is done:

In Delphi mode:

  • / always does float div; ie 10 / 3 = 3.33333333
  • div always does int divide, and requires integers on left and right side.

in non Delphi mode:

  • / and DIV work the same:
    • If there’s a float on the left or right side, it’s a float
    • Else it’s an integer
1 Like

But my question was about rounding when doing integer division.
And why the documentation is not the same for both operators.

The documentation doesn’t say that: “Divide” only, not a remark for Delphi compatibility mode, as for the “/” operator.

Yes. Just gave summary though. As for the actual rounding, it rounds towards zero. We will look into fixing the docs of course.

That’s what bugs://84528 was logged for, FTR, I just wanted Carlo’s final feedback before deciding whether to doc what it does now vs changing anything compiler side.

bugs://84528 got closed with status nochangereq.