Tuesday, June 5, 2012

Math functions

I'm in the process of expanding my own math library, without STL, and as usual it's quite the learning experience. What has me racking my brain now is pow(float y, float x), which I need to properly compute exp(float x).

Integer powers are easy, but floats provide that extra bit of frustration because of the decimal place. Maybe it's just late, and it'll come to me in the morning ;)

The premise with an example number: pow(12.5, 4.205), how to do it? It breaks down into pow(12.5, 4) * pow(12.5, 0.205). So how do you get the second part? As it turns out, it's not too trivial to solve, at least by hand, or personally written algorithm.

As is outlined here: http://lamarotte2.blogspot.ca/2011/04/hand-calculation-of-fractional-decimal.html
Yikes... O_O!

So basically:
result,  value,  exponent
1.00 = 12.5 ^ 0.0
???? = 12.5 ^ 0.1
...
???? = 12.5 ^ 0.205 <--- we need to find this
...
3.54 = 12.5 ^ 0.5  <--- sqrt(12.5)
...
???? = 12.5 ^ 0.9
12.5 = 12.5 ^ 1.0

So we need to bounce back and forth performing square roots on numbers to find a result of 0.205. And use that number to then multiply it with (12.5 ^ 4)

And all of this is needed so I can solve exp(float n) without using STL. Yeesh, the goals I give myself sometimes ;)