Jump to content

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


nitekram
 Share

Go to solution Solved by SadBunny,

Recommended Posts

 

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

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

 

I did most of that code for testing, and to figure out exactly what your code was doing. I wish that I could think your way, as I probably would not be asking these types of questions. It takes me a while to understand abstract concepts, but once I get them, I get them - still not 100% sure on your code, but got this as a step in the right direction?

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), $iReturn
    If ($modulo <= $range Or $modulo >= $n - $range) Then
        If Mod($value, 5) = 0 Then $iReturn = $value
        If Mod($value + 1, 5) = 0 Then $iReturn = $value + 1
        If Mod($value - 1, 5) = 0 Then $iReturn = $value - 1
        Return $iReturn
    Else
        Return -1
    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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Link to comment
Share on other sites

That code is far more complex than it needs to be.

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

Link to comment
Share on other sites

How about 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), $iReturn
    If ($modulo <= $range Or $modulo >= $n - $range) Then
        If $modulo = 0 Then $iReturn = $value
        If $modulo + 1 = 0 Then $iReturn = $value + 1
        If $modulo - 1 = 0 Then $iReturn = $value - 1
        Return $iReturn
    Else
        Return -1
    EndIf
EndFunc   ;==>_isValueWithinRangeFromMultipleOfN

.

EDIT

Or it this better?

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), $iReturn = -1
    If ($modulo <= $range Or $modulo >= $n - $range) Then
        If $modulo = 0 Then $iReturn = $value
        If $modulo + 1 = 0 Then $iReturn = $value + 1
        If $modulo - 1 = 0 Then $iReturn = $value - 1
    EndIf
    
    Return $iReturn
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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Link to comment
Share on other sites

Assuming you want to adjust to the nearest 5 minute interval, because your code is hard to interpret and contains bugs, I imagine you are looking for something that does this.

For $i = 0 To 59
    ConsoleWrite(_AdjustToFixedInterval($i, 1, 5, 60) & @LF)
Next

Func _AdjustToFixedInterval($iValue, $iRange, $iInterval, $iMode)
    If $iRange > $iInterval /2 Then Return SetError(1) ; overlap
    Local $modulo = Mod($iValue, $iInterval)
    If $modulo <= $iRange Then Return $iValue - $modulo
    If $modulo >= $iInterval - $iRange Then Return Mod($iValue - $modulo + $iInterval, $iMode)
    Return -1
EndFunc   ;==>_AdjustToFixedInterval
Edited by czardas
Link to comment
Share on other sites

 

Assuming you want to adjust to the nearest 5 minute interval, because your code is hard to interpret and contains bugs, I imagine you are looking for something that does this.

For $i = 0 To 59
    ConsoleWrite(_AdjustToFixedInterval($i, 1, 5, 60) & @LF)
Next

Func _AdjustToFixedInterval($iValue, $iRange, $iInterval, $iMode)
    If $iRange > $iInterval /2 Then Return SetError(1) ; overlap
    Local $modulo = Mod($iValue, $iInterval)
    If $modulo <= $iRange Then Return $iValue - $modulo
    If $modulo >= $iInterval - $iRange Then Return Mod($iValue - $modulo + $iInterval, $iMode)
    Return -1
EndFunc   ;==>_AdjustToFixedInterval

I am looking at this code, and though I understand some of it, I am not sure what the last parameters value does to the code - $iMode seems to not do anything, as when I have changed that value to 1, 100, 10, 60 there is no change in the output?

.

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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Link to comment
Share on other sites

Change it to 55 minutes in the hour and you'll see a difference. Also look closer at changing the value to 10. The results are wrong.

;

For $i = 0 To 54 ; 55 minute hour (max input = 54)
    ConsoleWrite(_AdjustToFixedInterval($i, 1, 5, 55) & @LF) ; Altered $iMode to 55
Next

Func _AdjustToFixedInterval($iValue, $iRange, $iInterval, $iMode)
    If $iRange > $iInterval /2 Then Return SetError(1) ; overlap
    Local $modulo = Mod($iValue, $iInterval)
    If $modulo <= $iRange Then Return $iValue - $modulo
    If $modulo >= $iInterval - $iRange Then Return Mod($iValue - $modulo + $iInterval, $iMode)
    Return -1
EndFunc   ;==>_AdjustToFixedInterval

;

Intended as an example, this is very specific to the code you posted, with which you are using @Min. That means the input value will always be below 60. A more universal function would require additional error checks to ensure that the input is not equal to or greater than the mode value, that the mode value is divisible by the interval, that all input values are positive numbers and the mode is greater than 1. I might have overlooked something.

Basically the additional parameter converts the return value 60 to zero, since all the above criteria are met in my >previous example.

Edited by czardas
Link to comment
Share on other sites

I see now that there is a difference with those two numbers - and some show up as wrong. Is $iMode in your code the amount of minutes, if not what would be the definition of that variable?

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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Link to comment
Share on other sites

Yes it's the number of minutes in an hour. The adjustment of the result from 59 to 60 should probably return a valid (zero) number of minutes past the hour, and 60 is not a valid number of minutes past the hour. If you wanted to return 60 and deal with the overflow outside the function, it is an easy modification to make as shown below.

With only three parameters the last return value becomes 60, which really should be zero because it's the start of the next hour. The best solution will depend on exactly what you are intending to do.

For $i = 0 To 59
    ConsoleWrite(_AdjustToFixedInterval($i, 1, 5) & @LF)
Next

Func _AdjustToFixedInterval($iValue, $iRange, $iInterval)
    If $iRange > $iInterval /2 Then Return SetError(1) ; overlap
    Local $modulo = Mod($iValue, $iInterval)
    If $modulo <= $iRange Then Return $iValue - $modulo
    If $modulo >= $iInterval - $iRange Then Return $iValue - $modulo + $iInterval
    Return -1
EndFunc   ;==>_AdjustToFixedInterval
Edited by czardas
Link to comment
Share on other sites

Thanks for everyone's help.

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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...