[Solved] Why does my Modulo fail ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

[Solved] Why does my Modulo fail ?

07 Jan 2022, 13:51

Anyone know why my Modulo formula isn't working :?:

Code: Select all

Loop % ( dr := 6 ) * 2
	res .= Mod( A_Index-1, dr )+1 " <> " ( dd := A_Index-1 )-( dd/dr*dr )+1 "`n"

msgbox % res
Result should be:
1 <> 1
2 <> 2
3 <> 3
4 <> 4
5 <> 5
6 <> 6
1 <> 1
2 <> 2
3 <> 3
4 <> 4
5 <> 5
6 <> 6

:eh:
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Why does my Modulo fail ?

07 Jan 2022, 14:21

2-1 - (1÷6×6)+1 = ?

3-1 - (2÷6×6)+1 = ?

Code: Select all

Run, calc
n - n + 1 = 1
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Why does my Modulo fail ?

07 Jan 2022, 14:24

Another way to look at it:

Starting right side:
( dd := A_Index-1 )-( dd/dr*dr )+1

Since dd/dr*dr is dd because we are dividing by dr then multiplying by dr (i.e., they cancel each other), it reduces to:

( dd := A_Index-1 )-( dd )+1

and dd - dd is 0 so it reduces to:
1


Note that dd/dr*dr is not dd/(dr*dr) because they are evaluated from left to right since multiplication and division are at the same order of operations. Is that perhaps the contention point?
teadrinker
Posts: 4368
Joined: 29 Mar 2015, 09:41
Contact:

Re: Why does my Modulo fail ?

07 Jan 2022, 14:30

Perhaps you meant this:

Code: Select all

Loop % ( dr := 6 ) * 2
   res .= Mod( A_Index-1, dr )+1 " <> " ( dd := A_Index-1 )-( dd//dr*dr )+1 "`n"

msgbox % res
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Why does my Modulo fail ?

07 Jan 2022, 14:32

Ah I figured it out. Had to floor divide the dividend by the divisor ( dd := A_Index-1 )-( dd//dr*dr )+1

Thanks for responses tho guys ;)

edit: @teadrinker must have got it while you were posting :lol: ty :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton and 130 guests