7/4 = 1 and (-7)/4 = -1 : But I want operator truncating to the left (on the number line)

Hello,

I want an operation, OP, so that for example OP: (7,4) → 1, and OP: (-7,4) → -2 (i.e. “truncation to the left on the number line”).

I can invent such a function, but is it a built-in function doing this?

reinert

This is unclear. Is this a function taking two parameters separated by a comma, or a single parameter of a fixed/floating point type ?
If so use the dot as a decimal separator, it’s confusing the hell outta me. What you write in unintelligible.

The truncation you describe looks like the 'Floor attribute for floating points:

S’Floor denotes a function with the following specification:
function S’Floor (X : T) return T
The function yields the value X, that is, the largest (most positive) integral value less than
or equal to X. When X is zero, the result has the sign of X; a zero result otherwise has a
positive sign.

So Float’Floor(6.5) = 6.0 and Float’Floor(-6.5) = - 7. Tested.

The question is about integer (not float). It is no problem to invent such a function, which I asked for - but I was looking for a built-in one which I may overlook (?). Both infix and prefix notation are fine for me :slight_smile:

ah! So you meant the result of the division 7/4 !
No, if you want a float, convert in float then divide. integral division will only give you the integral quotient.

I think you have to write your own function. Have you looked at mod and rem?

Stupid attempt:

function test1(A,B : Integer) return Integer is
(declare L : constant Integer := abs (A*B); begin (A + L)/B - L/B);

or maybe:

function test1(A,B : Integer) return Integer is
(declare L : constant Integer := abs (A*B); begin (A + L)/B - (abs A));

Just for fun…

There are mod and rem operators “for every specific integer type T”:
http://www.ada-auth.org/standards/22rm/html/RM-4-5-5.html#I3081

I made this summary about this question for several common programming languages:
http://www.opimedia.be/DS/languages/comparative/divisions/divisions.html

2 Likes

Nice table! You might consider adding the mod results next to the remainder results (maybe change the column header for Remainder to REM to shorten that column). It looks great either way.

rem in Ada corresponds to the % operator for most of other programming languages.

Yep yep, I’m aware, I was just saying that one of the things most people mess up is modulus results vs remainder results, so it might be handy to show the modulus results of the examples next to the remainder results, so folks can visualize the difference.

No biggie if not interested though, just wanted to suggest it was all.