math: x^y

BlitzMax Forums/BlitzMax Programming/math: x^y

Hi,

is there a math function which can do power of (x^y)?

I know i can make my own function but it would be much nicer if this would be supported by a built in function and i couldn't find it in the docs...


Greetings,

taumel

Local i, j

i = 4
j = 5

Print	i^j           ' 4^5 = 1024


Oh man sorry...

Silly me! This happens when you're switching between different tools too fast... :O\


Greetings,

taumel

Heh. Trust me, you're not alone.

I always suspected that there have to be other dumb persons beside of me around... ;O)


Greetings,

taumel

yeah like me ;)

dont fret m8 we all do that at times, and just miss the answer even tho its in front of our face, i think its genetic to all programmers.

Correct me if I'm wrong, but I thought Mark had said that x^y was unoptomized at the moment, and so x*x*x*x is faster than x^4. I don't know if a loop adds more overhead than the exponetial function, but if you are using large powers of y, you might want to consider a loop instead...

well, if its x^2 then you should turn it manually to x*x, but if its x^2.5 then you should leave it like it is.

Try these:

X ^ 2.5 = X * X * \

X ^ 2.1 = X * X * `

X ^ 1.9 = X * x

Let us know your results :)

Ya x^2 is really slow in blitzmax, I wish it would get optimized because its annoying, and sometimes would run slower to put the same variable twice. Like (player\x-player2\x)^2 versus (player\x-player2\x)*(player\x-player2\x)

this is prob the cause of the speed hit:
x:int = 5


x^x:
24.999999999999996

x * x:
25


even if you are asking for the Square of in Integer, the ^ operator returns a Double.

the * operator appears to work with whatever number type you give it

bah, ^ is still so slow? sad :-(
Luckily I didnt have to use it. Anyway sqr() is quite fast and you can always code your own power operator, at least for integer powers.