# Checking to see if @Min is either 1 more or 1 less then Mod(x, 5) = 0

Go to solution Solved by SadBunny,

## Recommended Posts

I am looking to create a check to see if the @Min is plus or minus 1 from Mod(\$x, 5) = 0. So if the @Min was say 14, then I want to know that passed, and if the @Min was 15 or 16 that would also pass. So for every multilple of 5, I want to check if it is 1 minute before, right at a multiple of 5, or 1 minute after.

This works - I assume for 5 minutes after the hour, but how do I do it for every 5 mins?

```If Mod(@MIN, 5) = '0' Or Mod(@MIN, 6) = '0' Or Mod(@MIN, 4) = '0' Then
If Mod(@MIN, 4) = '0' Then \$iMin = @MIN + 1 ; @min = 4
If Mod(@MIN, 5) = '0' Then \$iMin = @MIN
If Mod(@MIN, 6) = '0' Then \$iMin = @MIN - 1 ; @min = 6
EndIf```

.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

Mod doesn't work the way you're using it.

Mod divides the first number by the second number and tells you if there's a remainder, if the return from the function is 0 it's telling you that the first number is evenly divisible by the second number. If the result is anything other than 0 then it's telling you that there's a remainder left over. It does not tell you that the number ends in 5, 6, or 4. Mod(@Min, 5) would tell you if @Min is a multiple of 5 or not, and will work for every 5 minutes in an hour. It will also work for 00 minutes, but every other number used in the second parameter will also return 0 so that's not going to help much.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

Mod doesn't work the way you're using it.

Mod divides the first number by the second number and tells you if there's a remainder, if the return from the function is 0 it's telling you that the first number is evenly divisible by the second number. If the result is anything other than 0 then it's telling you that there's a remainder left over. It does not tell you that the number ends in 5, 6, or 4. Mod(@Min, 5) would tell you if @Min is a multiple of 5 or not, and will work for every 5 minutes in an hour. It will also work for 00 minutes, but every other number used in the second parameter will also return 0 so that's not going to help much.

Understand, and if I put all conditions in for Mod(), meanging 4, 5, 6, 9, 10, 11, 14, 15, 16,ect. then it would work, but I would hope that there is an easier way?

EDIT, or I guess I could just not use Mod() and put conditions for each of the @Min I am looking for, I will look at doing that and see if it works for my issue.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

Something like this?

```For \$i = 0 To 59

ConsoleWrite(\$i & " : " & _checker(\$i) & @CRLF)
Next

Func _checker(\$nr)

\$x = Mod(\$nr, 5)
Return (\$x == 0 Or \$x == 1 Or \$x == 4)
EndFunc   ;==>_checker```

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

I came up with something almost the same SadBunny.

```For \$I = 0 To 59
\$Min = Mod(\$I, 5)
If \$Min = 1 Or \$Min = 4 Or \$Min = 0 Then
ConsoleWrite("Min is correct " & \$Min & @TAB & \$I & @CRLF)
Else
ConsoleWrite("Min isn't correct " & @CRLF)
EndIf
Next```

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

• Solution

Note that <any integer> mod <n> is anywhere between 0 .. (n-1). So another way to put it is:

```Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)

\$modulo = Mod(\$value, \$n)
Return (\$modulo <= \$range Or \$modulo >= \$n-\$range)
EndFunc   ;==>_checker```

With this function you could do your task by saying _is...(@Min, 1, 5). But if you decide tomorrow you'd rather check for an interval of 10 minutes with a range of 2 minutes before and after, just do _is...(@Min, 2, 10).

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

Note that <any integer> mod <n> is anywhere between 0 .. (n-1). So another way to put it is:

```Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)

\$modulo = Mod(\$value, \$n)
Return (\$modulo <= \$range Or \$modulo >= \$n-\$range)
EndFunc   ;==>_checker```

With this function you could do your task by saying _is...(@Min, 1, 5). But if you decide tomorrow you'd rather check for an interval of 10 minutes with a range of 2 minutes before and after, just do _is...(@Min, 2, 10).

I like this one, as I have no clue what it does, but it does work...thanks to all.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

One question, I am going to return array of the true, as well as the value. How can I return the multiple number, meaning if the value was 6, how can I return 5?

EDIT So something like this:

```Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)
; With this function you could do your task by saying _is...(@Min, 1, 5).
; But if you decide tomorrow you'd rather check for an interval of 10 minutes with a range of 2 minutes before and after, just do _is...(@Min, 2, 10).
Local \$modulo = Mod(\$value, \$n)
If (\$modulo <= \$range Or \$modulo >= \$n - \$range) Then
Else
Return (\$modulo <= \$range Or \$modulo >= \$n - \$range)
EndIf

EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

Well if you're writing it like that, you can change the

```Else
Return (\$modulo <= \$range Or \$modulo >= \$n - \$range)
EndIf```

to:

```EndIf
Return False```

... as your current code reaches the else block if and only if that same truth formula already returned False. No need to do it again.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

But... Instead of returning an array, which you then have to read out again in your calling code, why not just have the function return -1 (or any other negative value) in case of failure? Like this...

```Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)
Local \$modulo = Mod(\$value, \$n)
Return ((\$modulo <= \$range Or \$modulo >= \$n - \$range) ? \$value : -1)
EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

I think this will work? It might not be the cleanest, but I am doing a search on those minutes being multiple of 5 with the variance.

```Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)
; With this function you could do your task by saying _is...(@Min, 1, 5).
; But if you decide tomorrow you'd rather check for an interval of 10 minutes with a range of 2 minutes before and after, just do _is...(@Min, 2, 10).
Local \$modulo = Mod(\$value, \$n)
If (\$modulo <= \$range Or \$modulo >= \$n - \$range) Then
If \$modulo <= \$range Then

IniWrite('p:\uip\testingtime.ini', @MDAY, \$modulo, \$range)
If Mod(\$value, 5) = 0 Then \$aAnswer[1] = \$value
If Mod(\$value + 1, 5) = 0 Then \$aAnswer[1] = \$value + 1
If Mod(\$value - 1, 5) = 0 Then \$aAnswer[1] = \$value - 1
ConsoleWrite('TRUE1 ' & \$aAnswer[1] & @CRLF)

ElseIf \$modulo >= \$n - \$range Then

IniWrite('p:\uip\testingtime.ini', @MDAY, \$modulo, \$n - \$range)
If Mod(\$value, 5) = 0 Then \$aAnswer[1] = \$value
If Mod(\$value + 1, 5) = 0 Then \$aAnswer[1] = \$value + 1
If Mod(\$value - 1, 5) = 0 Then \$aAnswer[1] = \$value - 1
ConsoleWrite('TRUE2 ' & \$aAnswer[1] & @CRLF)
EndIf

Else
Return False
EndIf

EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

nitekram,

Nice, but should you not be setting \$aAnswer[0] to false at some point...

```consoleWrite('!Truth table' & @CRLF)

for \$1 = 1 to 60
ConsoleWrite(stringformat('%-10i %-10i %-10i %-10i %-10s', \$1, 2, 5, _
_isValueWithinRangeFromMultipleOfN(\$1, 2, 5)[1], _
_isValueWithinRangeFromMultipleOfN(\$1, 2, 5)[0]) & @CRLF)
next

Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)
; With this function you could do your task by saying _is...(@Min, 1, 5).
; But if you decide tomorrow you'd rather check for an interval of 10 minutes with a range of 2 minutes before and after, just do _is...(@Min, 2, 10).
Local \$modulo = Mod(\$value, \$n)
If (\$modulo <= \$range Or \$modulo >= \$n - \$range) Then
If \$modulo <= \$range Then

IniWrite('p:\uip\testingtime.ini', @MDAY, \$modulo, \$range)
If Mod(\$value, 5) = 0 Then \$aAnswer[1] = \$value
If Mod(\$value + 1, 5) = 0 Then \$aAnswer[1] = \$value + 1
If Mod(\$value - 1, 5) = 0 Then \$aAnswer[1] = \$value - 1
;ConsoleWrite('TRUE1 ' & \$aAnswer[1] & @CRLF)

ElseIf \$modulo >= \$n - \$range Then

IniWrite('p:\uip\testingtime.ini', @MDAY, \$modulo, \$n - \$range)
If Mod(\$value, 5) = 0 Then \$aAnswer[1] = \$value
If Mod(\$value + 1, 5) = 0 Then \$aAnswer[1] = \$value + 1
If Mod(\$value - 1, 5) = 0 Then \$aAnswer[1] = \$value - 1
;ConsoleWrite('TRUE2 ' & \$aAnswer[1] & @CRLF)
EndIf

Else
Return False
EndIf

EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

kylomas

edit: your math might be off by 1 also...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

I have no idea why you make that function so complex and redundant while it's actually simple enough to use an almost-oneliner

- Why are you set on working with an array as a return value while it's possible with an int?

- Why are you duplicating the exact same code in an if-else for cases \$modulo >= \$n-\$range and \$modulo <= \$range, while the work is the exact same in both cases and you already know that at least one of them is true (because you're inside the first If)?

/edit - Why are you forcing (potentially, i.e. if the minute is not in the accepted ranges) 4 addition and 6 modulo operations while 1 is enough?

- Why are you iniwriting anyway from within a function that has no other work than performing a simple check? That's questionable code flow

I guess it all comes down to taste, but my suggestion would be to rethink the whole thing (and then accept my suggestions )

At the end of the day it doesn't matter, to each is own If it works for you it works for you. Have a good one!

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

nitekram,

Nice, but should you not be setting \$aAnswer[0] to false at some point...

Not necessarily, his code now either returns

either an array with the first element set to True and the second element set to \$value

or False.

It could work, provided that you know that really well from the calling code. Problem is you can't treat the function as a boolean any more, as the truth value of a filled array is also False (which I did not expect but just found out; if you dimension and fill an array and then write "If \$array then <do affirmative>" it will still fail )

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

nitekram,

The modulo routine needs to account for op1 being < op2 like...

```ConsoleWrite('!Truth table' & @CRLF)

for \$1 = 1 to 60
ConsoleWrite(stringformat(' \$value = %-10i \$result = %-10i', \$1, _isValueWithinRangeFromMultipleOfN(\$1, 2, 10)) & @CRLF)
next

Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)

; account for value less than iteration number
if \$value < \$n then return (((\$n - \$value) > \$range) ? -1 : \$value)

Local \$modulo = Mod(\$value, \$n)
Return (\$modulo <= \$range or \$n - \$modulo <= \$range) ? \$value : -1

EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

nitekram,

The modulo routine needs to account for op1 being < op2 like...

```ConsoleWrite('!Truth table' & @CRLF)

for \$1 = 1 to 60
ConsoleWrite(stringformat(' \$value = %-10i \$result = %-10i', \$1, _isValueWithinRangeFromMultipleOfN(\$1, 2, 10)) & @CRLF)
next

Func _isValueWithinRangeFromMultipleOfN(\$value, \$range, \$n)

; account for value less than iteration number
if \$value < \$n then return (((\$n - \$value) > \$range) ? -1 : \$value)

Local \$modulo = Mod(\$value, \$n)
Return (\$modulo <= \$range or \$n - \$modulo <= \$range) ? \$value : -1

EndFunc   ;==>_isValueWithinRangeFromMultipleOfN```

Why would you break the algorithm for the integers smaller than \$n? (In this case, the first few minutes of the hour? Not to mention ALL negative numbers?) /me puzzled.

Btw, minutes go from 0 to 59. Luckily, with the modulo technique, it also works for all representable integers

This is becoming a weird thread. Don't know if it's me.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

@SadBunny - I'm not, it is for values smaller than the interval.  Run my code with the 0 test commented out and you will see it.

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

@SadBunny - I'm not, it is for values smaller than the interval.  Run my code with the 0 test commented out and you will see it.

I have run it with your addition commented out, I wrote the code  (Edit: also I ran it from 0 to 59 instead of from 1 to 60 ) What I don't understand is why would you want to exclude all numbers smaller than \$n * 1? OP wants to get a True if the current minute is divisible by 5, with a tolerance of 1 minute. Your addition will break that for every whole hour and the first and fourth minute past the whole hour. (Or, in terms of my algorithm, all minutes of the hour between 0 and the first instance of the chosen interval, so with your example minutes 0 to 9, while minute 0, 1, 2, 8 and 9 should clearly also trigger.)

Why would you want to do that?

P.S. Remember that 0 is just as much a multiple of N as any other multiple of N.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

I wrote the code

Not quite, my formula is slightly different but the net result is the same.

Why would you want to do that?

'cause I'm cross-eyed...You're right, the formula stands on it's own, my mistake...

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

Not quite, my formula is slightly different but the net result is the same.

'cause I'm cross-eyed...You're right, the formula stands on it's own, my mistake...

I am too, I didn't see that you slightly rewrote my thingy. Ok, mystery solved

Roses are FF0000, violets are 0000FF... All my base are belong to you.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...