Opened 6 months ago

Closed 5 months ago

# Integer division

Reported by: Owned by: Alecsis1 Jpm 3.3.17.0 AutoIt None

### Description

Hello!
As well-known, division operation always returns double even if both operands are integer, e.g.

```Local \$v = 10/2
ConsoleWrite(VarGetType(\$v)) ; we've got double instead of integer
```

Imho there may be useful to have special math operator for integer division, for example \$i % \$j or smth like that. Or else, special function like Div(\$i, \$j) in addition to existing Mod(\$i, \$j)
In such case we would be sure to obtain integer result, e.g. 10/3=3 instead of 3.33333…
Thank you for attention!
PS Sorry for my weak English (

### comment:1 follow-up: ↓ 2 Changed 6 months ago by Jos

You mean like the existing Floor() function?

### comment:2 in reply to: ↑ 1 ; follow-up: ↓ 3 Changed 6 months ago by anonymous

You mean like the existing Floor() function?

Yes, something similar but implemented as a built-in operation. Imho it would be rather convenient.

```; "may-be" built-in operation
\$k = \$i % j ; instead of (or in addition to) Floor()
; or else builtin function
\$k = Div(\$i, \$j) ; also set @extended to Mod(\$i, \$j)
```

Anyway I don't insist on my offer, it's just a thought :)

### comment:3 in reply to: ↑ 2 Changed 6 months ago by Jos

Yes, something similar but implemented as a built-in operation. Imho it would be rather convenient.

Floor() is buildin, so please explain what the issue is with using that? Is it not working correctly or do you want another outcome that Floor() produces?

Version 1, edited 6 months ago by Jos (previous) (next) (diff)

### comment:4 Changed 6 months ago by Jos

• Resolution set to Rejected
• Status changed from new to closed

Closed for now unless an answer comes that justifies opening it again.

### comment:5 Changed 6 months ago by jchd18

Floor() isn't the correct solution to the request.
For instance, Floor(-10/3) yields -4 which is mathematically correct but certainly not what a naive user would expect in this context.

The OP is expecting something "like" the Euclidean division. You supply numerator N (aka dividend) and divisor D, Euclidean division returns the quotient Q and the remainder R such as N = Q * D + R

Disney life is simple but real world isn't! The question now is: "Which Euclidean division"?
Translate this into: things are easy while N ≥ 0 and D > 0 but not all {N, D} are such.

Try Int() and Mod() on the {N, D} tuples below:
{-11, -2}, {-11, 2}, {11, -2}, {11, 2}
You get four distinct (yet correct) answers: {5, -1}, {-5, -1}, {-5, 1}, {5, 1}.

In short if you accept signed Q and R, then Int() and Mod() are your tools over ℤxℤ*, but if you insist on R being non-negative, then you must also accept seemingly off-by-one values for Q and varying values modulo(q) for R when at least one of N or D is negative.

Finally, since Int(3, 0) yields inf (meaning infinity) and since both Mod(3, 0) and Int(0 / 0) yield nan(ind), meaning Not-A-Number(Indeterminate), one must also provide a route for these cases.

Note that I choose not to return Q as return value and R as @extended because @extended is integral type and limited to 32-bit. Q is set ByRef and returned for convenience.

```Local \$a = [ _
[-11, -2], _
[-11, 2], _
[11, -2], _
[11, 2], _
[10, 3], _
[10, -3], _
[10, 0], _
[0, 0] _
]
Local \$n, \$d, \$q, \$r
For \$i = 0 To UBound(\$a) - 1
\$n = \$a[\$i][0]
\$d = \$a[\$i][1]
_Div(\$n, \$d, \$q, \$r)
ConsoleWrite(\$q & @TAB & \$r & @LF)
Next

Func _Div(ByRef \$x, ByRef \$y, ByRef \$q, ByRef \$r)
\$q = \$x / \$y
If \$y <> 0 Then
\$q = Int(\$q)
Else
SetError(1)
EndIf
\$r = Mod(\$x, \$y)
Return \$q
EndFunc
```

Not that my remarks justify reopening the topic.

Last edited 6 months ago by jchd18 (previous) (diff)

### comment:6 Changed 5 months ago by Jpm

• Resolution Rejected deleted
• Status changed from closed to reopened

### comment:7 Changed 5 months ago by Jpm

• Owner set to Jpm
• Status changed from reopened to assigned

### comment:8 Changed 5 months ago by Jpm

• Milestone set to 3.3.17.0
• Resolution set to Completed
• Status changed from assigned to closed

Added by revision [12995] in version: 3.3.17.0

• In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.