Modify

Opened 3 years ago

Closed 3 years ago

#3960 closed Feature Request (Completed)

Integer division

Reported by: Alecsis1 Owned by: J-Paul Mesnage
Milestone: 3.3.17.0 Component: AutoIt
Version: Severity: None
Keywords: Cc:

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 (

Attachments (0)

Change History (8)

comment:1 by Jos, 3 years ago

You mean like the existing Floor() function?

in reply to:  1 ; comment:2 by anonymous, 3 years ago

Replying to Jos:

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 :)

in reply to:  2 comment:3 by Jos, 3 years ago

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 3 years ago by Jos (previous) (next) (diff)

comment:4 by Jos, 3 years ago

Resolution: Rejected
Status: newclosed

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

comment:5 by jchd18, 3 years ago

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 3 years ago by jchd18 (previous) (diff)

comment:6 by J-Paul Mesnage, 3 years ago

Resolution: Rejected
Status: closedreopened

comment:7 by J-Paul Mesnage, 3 years ago

Owner: set to J-Paul Mesnage
Status: reopenedassigned

comment:8 by J-Paul Mesnage, 3 years ago

Milestone: 3.3.17.0
Resolution: Completed
Status: assignedclosed

Added by revision [12995] in version: 3.3.17.0

Modify Ticket

Action
as closed The owner will remain J-Paul Mesnage.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.