# Good idea to define an "integer type" so that 7/4\*4 = 7?

**URL:** <https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308>\
**Category:** General\
**Created:** [October 10, 2024, 7:34am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308 "2024-10-10T07:34:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![reinert](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/r/46a35a/32.png) [@reinert](https://forum.ada-lang.io/u/reinert)\
**Post date:** [October 10, 2024, 7:34am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/1 "2024-10-10T07:34:19Z")

</div>

Sometimes I wish that, for example, 7/4\*4 = 7 (and not 7/4 = 4) to align with mathematical thinking. Would it be a good idea to define such a new “integer type”?

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [October 10, 2024, 7:57am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/2 "2024-10-10T07:57:33Z")

</div>

Given `A := 7 / 4`, it’s hard to see how `B := A * 4;` could yield 7. Or, are you looking at rational numbers? That would work …

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [October 10, 2024, 8:14am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/3 "2024-10-10T08:14:47Z")

</div>

I beg to disagree. In integer arithmetics (the Z set), 7/4 = 4.  
Fortunately, Ada separates cleanly the Z set and the R set, down to literals. Possibly it is the only language to do it right. Most if not all others do various type promotions (implicit conversions) to some degree.  
Just add “.0” to literals if you want to switch to the R set.

(Coincidentally, I have recently cleaned up the part of HAC that parses literals: [(scanner/parser) Better names and simplifications · zertovitch/hac@5d32ee4 · GitHub](https://github.com/zertovitch/hac/commit/5d32ee4))

---

<div class="post-metadata">

**Author:** ![reinert](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/r/46a35a/32.png) [@reinert](https://forum.ada-lang.io/u/reinert)\
**Post date:** [October 10, 2024, 12:00pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/4 "2024-10-10T12:00:24Z")

</div>

The code example below illustrates why I bring this “issue”. I made first a naive way to obtain the median of a sorted array and forgot about possible negative indexes:

```ada
with Ada.Text_Io; use Ada.Text_Io;
procedure test1 is
-- x : constant array(Integer range 1 .. 4) of Float := [1.0, 2.0, 3.0, 4.0]; 
   x : constant array(Integer range -3 .. 0) of Float := [1.0, 2.0, 3.0, 4.0];
   k : constant Integer := x'last - x'first;

-- i1 : constant Integer := k/2 + x'first;
   i1 : constant Integer := (x'first + x'last)/2; -- gives wrong result for negative indexes

   i2 : constant Integer := i1 + k mod 2;

   median1 : constant float := 0.5*(x(i1) + x(i2));
begin
   Put_Line("x: " & x'Image);
   Put_Line("i1 : " & i1'Image);
   Put_Line("i2 : " & i2'Image);
   Put_Line("median1 : " & median1'Image);
end test1;
``
Output:

x: 
[1.00000E+00, 2.00000E+00, 3.00000E+00, 4.00000E+00]
i1 : -1
i2 : 0
median1 : 3.50000E+00
```

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [October 10, 2024, 1:13pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/5 "2024-10-10T13:13:07Z")

</div>

> [@reinert](#):
>
> ```ada
> i1 : constant Integer := (x'first + x'last)/2; -- gives wrong result for negative indexes
> 
> ```

The result is correct (in terms of integer arithmetics) but not what you hope for: you want exactly what the 'Floor attribute does. So the obvious way is to do a short trip to real numbers:

`Integer (Float'Floor (Float (x'first + x'last) * 0.5))`

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [October 10, 2024, 1:21pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/6 "2024-10-10T13:21:58Z")

</div>

> [@zertovitch](#):
>
> In integer arithmetics (the Z set), 7/4 = 4.

Ummm… Surely I misunderstand you? In true integer arithmetic, division is an unusual operation that produces [a tuple as output, the quotient and the remainder](https://en.wikipedia.org/wiki/Euclidean_division); i.e., (1,3).

> [@reinert](#):
>
> Sometimes I wish that, for example, 7/4\*4 = 7 (and not 7/4 = 4) to align with mathematical thinking.

Sounds as if you want a [computer algebra system](https://en.wikipedia.org/wiki/Computer_algebra_system), also called symbolic algebra. That can be implemented in Ada, but an unfortunate reality is that symbolic arithmetic is far slower and much more memory intensive. Many algorithms are worst-case doubly-exponential.

> Would it be a good idea to define such a new “integer type”?

If you **really** need something like that, why not? but you’re about to go down a rabbit hole that I spent ~25 years of my life studying. 😁 Don’t get me wrong; it’s an **awesome** 🐇 🕳, but it gets complicated much more quickly than you’d expect.

But I’m not sure you **really** need it, as @zertovich points out.

---

<div class="post-metadata">

**Author:** ![reinert](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/r/46a35a/32.png) [@reinert](https://forum.ada-lang.io/u/reinert)\
**Post date:** [October 10, 2024, 2:46pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/7 "2024-10-10T14:46:38Z")

</div>

Yes, according to your reference ( [Euclidean division - Wikipedia](https://en.wikipedia.org/wiki/Euclidean_division) ) (-7)/3 would be -3, as I understand (q = -3, r = 2). I may try to stay away from your 🐇 🕳

---

<div class="post-metadata">

**Author:** ![flexibeast](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/flexibeast/32/399_2.png) [@flexibeast](https://forum.ada-lang.io/u/flexibeast)\
**Post date:** [October 10, 2024, 11:34pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/8 "2024-10-10T23:34:14Z")

</div>

> [@cantanima](#):
>
> Ummm… Surely I misunderstand you? In true integer arithmetic, division is an unusual operation that produces [a tuple as output, the quotient and the remainder](https://en.wikipedia.org/wiki/Euclidean_division); i.e., (1,3).

This might be a cross-language issue … Given what was written, i think it might be a reference to [modular arithmetic](https://en.wikipedia.org/wiki/Modular_arithmetic) over the integers, and to ℤ/4 in particular; but in that context, 7 ≡ 3 (mod 4), another ‘view’ of the Euclidean division operation you describe. So i’d be interested in @zertovitch elaborating on their remark.

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [October 11, 2024, 5:15am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/9 "2024-10-11T05:15:40Z")

</div>

I was alluding to the pair (quotient, remainder) where you discard the remainder.  
Modular arithmetics is yet another funny world where, for instance -1 = 9 (modulo 10).  
Ada provides this, by the way: `type Mod_10 is mod 10;`

---

<div class="post-metadata">

**Author:** ![reinert](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/r/46a35a/32.png) [@reinert](https://forum.ada-lang.io/u/reinert)\
**Post date:** [October 11, 2024, 9:45am UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/10 "2024-10-11T09:45:13Z")

</div>

Can someone explain me this:

```ada
   declare
     i : constant Integer := -2/4;
     x : constant Integer := (10)*(-2)/4;
   begin
     Put_Line("i : " & i'Image);
     Put_Line("x : " & x'Image);
   end;

```

Output:  
i : 0  
x : -5

Sorry for my confusion. I thought “/” is “(binding) stronger” than “\*” and expected to get x = 0.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [October 11, 2024, 1:46pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/11 "2024-10-11T13:46:18Z")

</div>

> [@reinert](#):
>
> I thought “/” is “(binding) stronger” than “\*” and expected to get x = 0.

The ARM (4.5) has this:

> multiplying\_operator ::= \* | / | **mod** | **rem**  
> …  
> For a sequence of operators of the same precedence level, the operators are associated with their operands in textual order from left to right. Parentheses can be used to impose specific associations.

That’s more or less in agreement with mathematical convention, at least as far as multiplication and division are concerned.

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [October 11, 2024, 3:06pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/12 "2024-10-11T15:06:59Z")

</div>

The one that I always forget is the and/or relationship in Ada:  
`logical_operator ::= and | or | xor`

In my head I expect `and` to have a higher precedence level than `or` but in Ada they seem to be on the same precedence level. GNAT also imposes some additional restrictions I couldn’t find in the ARM yet by forcing the use of parenthesis to clarify precedence between the two (instead of them being just optional)

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [October 11, 2024, 4:35pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/13 "2024-10-11T16:35:12Z")

</div>

> [@jere](#):
>
> GNAT also imposes some additional restrictions I couldn’t find in the ARM yet by forcing the use of parenthesis to clarify precedence between the two

This is implicit in the definition of _expression_ in [ARM 4.4](http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-4-4.html). See the example in 4.4(15/2):

```ada
(Cold and Sunny) or Warm -- expression (parentheses are required)

```

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [October 11, 2024, 5:35pm UTC](https://forum.ada-lang.io/t/good-idea-to-define-an-integer-type-so-that-7-4-4-7/1308/14 "2024-10-11T17:35:53Z")

</div>

> [@jere](#):
>
> In my head I expect `and` to have a higher precedence level than `or` but in Ada they seem to be on the same precedence level.

Which brings us to a really great, subtle point about Ada: the syntax here was designed to avoid errors by requiring parentheses around mixed `and`-`or` boolean-expressions. — When on multi-language projects, it’s EASY to get used to the precedence in that other language and come in and get bit by not switching; forcing parentheses avoids that problem altogether.
