Home AMX User Forum NetLinx Studio

raise to the nth power

How can I with Netlinkx to raise to the nth power a number? thanks a lot
alessandro

Comments

  • DHawthorneDHawthorne Posts: 4,584
    There is no built-in function for it, you have to roll your own. If you are dealing with integer exponents, you could just use a FOR loop - if you need fractional, it gets messy. I have a routine I am using for that, but I have to warn you, it is a horrible CPU hog. I'll attach it; but be warned, it can take a long time to run, and nothing else in your code will run meanwhile. It could probably be tweaked; I stopped when I got it "good enough." The original source is cited in the comments.
  • mpullinmpullin Posts: 949
    eddymouse wrote: »
    How can I with Netlinkx to raise to the nth power a number? thanks a lot
    alessandro
    DHawthorne wrote: »
    There is no built-in function for it, you have to roll your own.
    Ooh, we have multi-quote now. Neat!

    Because I sometimes use bitwise math in my NetLinx programs, I wrote a function to find 2^n. Here it is:
    DEFINE_FUNCTION INTEGER POW2(INTEGER n){
        // given n, return 2^n
        if(n == 0) return 1;
        else return 2 * POW2(n-1);
    }
    
    If you need to raise other numbers to the n besides 2, you'll need to add another parameter.
Sign In or Register to comment.