# _NumberRotate()

## Recommended Posts

I am trying to rotate a number. I wrote a UDF for it but it won't work properly all the time.

This should return 5 but it returns 6

```\$Number=5
\$Places=10
\$Min=1
\$Max=10
ClipPut( _NumberRotate(\$Number,\$Places,1,\$Min,\$Max) )

Func _NumberRotate(\$iNumber,\$iPlaces,\$iDir,\$iMin,\$iMax)
Local \$iReturn,\$iStep,\$iCounter
Select
Case \$iDir=1
For \$iCounter=1 to \$iPlaces step 1
\$iReturn=\$iNumber+1
If \$iReturn>\$iMax then \$iReturn=\$iMin
If \$iReturn<\$iMin then \$iReturn=\$iMax
Next
Case \$iDir=-1
For \$iCounter=1 to \$iPlaces step 1
\$iReturn=\$iNumber-1
If \$iReturn>\$iMax then \$iReturn=\$iMin
If \$iReturn<\$iMin then \$iReturn=\$iMax
Next
EndSelect
Return \$iReturn
EndFunc```

This should return 1 and does.

```\$Number=2
\$Places=3
\$Min=1
\$Max=2
ClipPut( _NumberRotate(\$Number,\$Places,1,\$Min,\$Max) )

Func _NumberRotate(\$iNumber,\$iPlaces,\$iDir,\$iMin,\$iMax)
Local \$iReturn,\$iStep,\$iCounter
Select
Case \$iDir=1
For \$iCounter=1 to \$iPlaces step 1
\$iReturn=\$iNumber+1
If \$iReturn>\$iMax then \$iReturn=\$iMin
If \$iReturn<\$iMin then \$iReturn=\$iMax
Next
Case \$iDir=-1
For \$iCounter=1 to \$iPlaces step 1
\$iReturn=\$iNumber-1
If \$iReturn>\$iMax then \$iReturn=\$iMin
If \$iReturn<\$iMin then \$iReturn=\$iMax
Next
EndSelect
Return \$iReturn
EndFunc```

How come it works with some numbers but not others.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
##### Share on other sites

I am trying to rotate a number. I wrote a UDF for it but it won't work properly all the time.

Without being 100% sure of what you're trying to achieve, is this what you're after?

`; deprecated - please see my next post`

Alternatively you could do away with the \$iDir variable by simply specifying \$iPlaces as a negative number to imply negative rotation, and then this code should suffice:

`; deprecated - please see my next post`
Edited by LxP
##### Share on other sites

```Func _NumberRotate(\$iNumber, \$iPlaces, \$iDir, \$iMin, \$iMax)
Local \$iReturn = \$iNumber
Local \$iStep, \$iCounter
Select
Case \$iDir = 1
For \$iCounter = 1 To \$iPlaces Step 1
\$iReturn = \$iReturn + 1
If \$iReturn > \$iMax Then \$iReturn = \$iMin
If \$iReturn < \$iMin Then \$iReturn = \$iMax
Next
Case \$iDir = -1
For \$iCounter = 1 To \$iPlaces Step 1
\$iReturn = \$iReturn - 1
If \$iReturn > \$iMax Then \$iReturn = \$iMin
If \$iReturn < \$iMin Then \$iReturn = \$iMax
Next
EndSelect
Return \$iReturn
EndFunc  ;==>_NumberRotate```

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

Why not make life a little easier and combine \$iPlaces and \$iDir into 1 variable?

```Func _NumberRotate(\$iNumber, \$iAmount, \$iMin, \$iMax)
Local \$iRange = \$iMax - \$iMin + 1
\$iNumber = \$iNumber + \$iAmount
While \$iNumber > \$iMax
\$iNumber = \$iNumber - \$iRange
WEnd
While \$iNumber < \$iMin
\$iNumber = \$iNumber + \$iRange
WEnd
Return \$iNumber
EndFunc```
##### Share on other sites

Thanks all. I am still not sure which one to use.I will run a quick check to make sure they all return the same answers first and then see which one is faster i will go with the faster one as i will be rotating many numbers as part of my simple encryption routine.

-SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
##### Share on other sites

Coding at 3AM is not best practice! Here's that code of mine again in a form that will work for \$iMin <> 1:

```func _numberRotate(\$iNumber, \$iPlaces, \$iMin, \$iMax)

local \$range = \$iMax - \$iMin + 1
return mod(\$iNumber - \$iMin + \$iPlaces + 1, \$range) + \$iMin - 1

endFunc```

Or if you still want to use \$iDir:

```func _numberRotate(\$iNumber, \$iPlaces, \$iDir, \$iMin, \$iMax)

if (\$iDir < 0) then \$iPlaces = 0 - \$iPlaces
local \$range = \$iMax - \$iMin + 1
return mod(\$iNumber - \$iMin + \$iPlaces + 1, \$range) + \$iMin - 1

endFunc```

Regards,

Alex

##### Share on other sites

Maybe using Number() to get the numeric value if string is a number

Writing AutoIt scripts since

`_DateAdd("d", -2, _NowCalcDate())`
##### Share on other sites

Coding at 3AM is not best practice! Here's that code of mine again in a form that will work for \$iMin <> 1:

```func _numberRotate(\$iNumber, \$iPlaces, \$iMin, \$iMax)

local \$range = \$iMax - \$iMin + 1
return mod(\$iNumber - \$iMin + \$iPlaces + 1, \$range) + \$iMin - 1

endFunc```

Or if you still want to use \$iDir:

```func _numberRotate(\$iNumber, \$iPlaces, \$iDir, \$iMin, \$iMax)

if (\$iDir < 0) then \$iPlaces = 0 - \$iPlaces
local \$range = \$iMax - \$iMin + 1
return mod(\$iNumber - \$iMin + \$iPlaces + 1, \$range) + \$iMin - 1

endFunc```

Regards,

Alex

<{POST_SNAPBACK}>

thank you. I do a lot of my coding late at night too. It is nice and Quiet then. LOL.
HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
##### Share on other sites

I have made up my mind and decied to use the one BlindWig modified from GaFrosts post. This is becuase I seem to be having problems with the one LxP made. Thanks All.

-SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
##### Share on other sites

I have made up my mind and decied to use the one BlindWig modified from GaFrosts post. This is becuase I seem to be having problems with the one LxP made.

Just so I can figure out where I went wrong and if you don't mind spending the time, could you please post a couple examples where my code didn't work for you? I certainly don't want to be sharing bad code!

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...