Jump to content

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

thanks in advance.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
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
Link to comment
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

SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

Link to comment
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
Link to comment
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.
Link to comment
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

Link to comment
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.
Link to comment
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.
Link to comment
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! :)
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...