Jump to content

Recommended Posts

Posted (edited)

I whipped up this little function and thought maybe someone could use it.

Basically how it works is it has one mandatory argument, and two optional ones.

$x = The number to check (mandatory)

$PowerOf = The number to get the nearest power of. (default = 2)

$behavior = The behavior takes three arguments in string form, "round", "floor", "ceiling", case insensitve.

What it does is it takes the number you give it and gives you the nearest power of the number you supply. For example

If you call _NearestPower( 31 ) It will return 32, because 32 is the nearest power of two to 31. If you set behavior to floor however, it will round down and return the next lowest power of two, 16. If you set PowerOf to 3 and behavior to ceiling and give it 31, it returns 81, because 81 is the next highest power of 3, and so forth.

Func _NearestPower( $x, $Powerof = 2, $behavior = "round" )
    Switch StringLower( $behavior )
        Case "round"
            Return $Powerof^round(log($x)/Log($Powerof))
        Case "floor"
            Return $Powerof^floor(log($x)/Log($Powerof))
        Case "ceiling"
            Return $Powerof^ceiling(log($x)/Log($Powerof))
    EndSwitch
EndFunc
Edited by insignia96

Visit my website to see all my finished releases!Releases here:UDFs:GUI ResizingColor List (Web Colors)GUIFade_NearestPower

Posted (edited)

As for the case-sensitivity issue, i figured it'd be better coding practice to equalize it

Mmm, well from a readability point of view you might have a point. But at the same time the code suggests that switch is case sensitive which is incorrect/misleading information.

In general I do a similar thing when using a number thats used in a string process. ($sFileName & '_' & string($iNumber))

A other general way of looking at it is. If a text/strings declaration is lowercase only, case don't matter. As in when case would matter you generally end up with a text/string declaration that contains some mixed case text. (I case of a exception ... you can always drop in a comment/remark about it.)

Thats all. (your code, you decide. :x )

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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
  • Recently Browsing   0 members

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