# Unity Problem with log functions

## Recommended Posts

Hi, I'm using some code I found on this thread about using SDL_TTF with OpenGL. Part of the code was a maths function, which I can't make work. You'll find the original in that thread I linkified. Here is said function as I have it, in a header file I made:
int nextpowerof2(int x)
{

double logbase2 = log(x) / log(2); // This line is the culprit
return round(pow(2,ceil(logbase2)));

}

But the compiler (VS2005 Express Beta 2) kicks up a fuss with the following error, twice on the same line as referenced in the above snippet: error C2668: 'log' : ambiguous call to overloaded function 1> c:\program files\microsoft visual studio 8\vc\include\math.h(553): could be 'long double log(long double)' 1> c:\program files\microsoft visual studio 8\vc\include\math.h(505): or 'float log(float)' 1> c:\program files\microsoft visual studio 8\vc\include\math.h(114): or 'double log(double)' 1> while trying to match the argument list '(int)' The errors are being flagged in cmath and not my own header file. No amount of hacking and working around can stop that - I usually just end up with new errors. If I typecast log(x) to be (double)log(x) there's no change, but if I do:
double log(x)

I'm told double was unexpected (error C2062). I can't make it work, can anybody help me out? Thanks, ukdeveloper.

log((double)x)

##### Share on other sites
That works, thanks!

##### Share on other sites
If you are going to do it that way, try using the function frexp instead. Something like this:
    int nextpowerof2(int x)    {        int n;        frexp( (double)x, &n );        return n+1;    }
Or, it may be faster and more accurate to do it this way:
    int nextpowerof2( int x )    {        int n = 1;        while ( n < x )        {            n *= 2;        }        return n;    }

##### Share on other sites
The compiler doesn't know whether you want to promote 2 (an int) to a float or a double.
A MUCH MUCH more efficient way of doing what you require is:
!!!Warning: code below assumes 32-bit int's!!!
int nextpowerof2(int x){    x |= x >> 1;    x |= x >> 2;    x |= x >> 4;    x |= x >> 8;    x |= x >> 16;    return ++x;}
[smile]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627740
• Total Posts
2978884
• ### Similar Content

• By KARTHI
• By KARTHI
Currently I using makehuman to make a 3d models but I am not satisfied with that model. If any use this please reply about the quality and details of models.
🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
• By KARTHI
I planned to make a space game so I need a knowledge about 3d planet making so anyone help me to make it possible
🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
• By KARTHI
I planned to make a game like star citizen. In this game player can travel through the space ,explore the planets, Hunt the different creatures, drive the space ship, explore different regions in universe, destroy the space stations and also player can go anywhere in the universe and meet aliens and so on
Did you like this idea?
Please give me reply so that I can easily understand what you want.
🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

• Your home planet was attacked. Now you have to use your spaceship to battle the invaders. Powerful 3D arcade with outer space background. Very addictive. Good luck!