# float unlimited increasing rotation or use a if

This topic is 2138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, i have a question :

suppose i am making a wheel rotating :

float rot = 0.0f;

// in loop

rot += fElapsedtime;

it works fine like this, but the number gets unlimited bigger and bigger.

Now what is better for CPU performance?, using a if that limits the rotation ? :

// in loop

rot += fElapsedtime;

if( rot > Pi )rot -= Pi;

What is the better choise and why ?

Edited by the incredible smoker

##### Share on other sites

What i usually do is something like this

float rot = 0.0f;

rot += somevalue * ElapsedTime;

while(rot >= 360.0f){

Rot -= 360,0f;

if(Rot < 0.0f)

Rot = 0.0f;

}

You should not worrie too much about performance for such simple things. You might loose 1/1000000000 of a second using this algorithm instead of a simpler one, so it's just not worth the trouble. This code handle all wrong/worst case scenario, and that's what matter. You might consider wraping that code to a function tho, if you plan to use it a lot.

The reason im using a while loop is, suppose the frame rate drop because of reason x and 3 seconds have elapsed since the last frame, this will still give a value between 0.0f and 360.0f. The if inside the loop is to protect value < 0.0 in case of a floating point precision error.

Edited by Vortez

##### Share on other sites

Could this also be achieved using float unrolledRot=fmod(absoluteRot, 360.0f)?

##### Share on other sites

I,m sorry i have no clue what you mean.

It also seems very CPU intensive, using a function like fmod ? ( i dont know what its for )

Normally i dont use functions like : sin() , cos() etc, fmod() looks something like that, very cpu intensive.

Note : i usually avoid divide to.

I,m asking to go with or without the if, and why ?

greetings

##### Share on other sites
To represent a rotation I prefer to use a unit-length vector instead of an angle. You can think of it as (cos(angle),sin(angle)). Now a particular rotation only has one possible representation so the issue doesn't exist. Whenever you are going to use the angle, chances are you are going to be computing its sine and cosine anyway, so it's not like we have complicated things.

If you are comfortable with complex numbers, it might be even better to represent the rotation as a complex number with modulus 1.

Complex rot(1.0f, 0.0f);

// in loop
rot *= exp(Complex(0.0f,fElapsedtime));

EDIT: You probably want to renormalize the rotation every so often, like this:
rot /= abs(rot);
Edited by Álvaro

##### Share on other sites

Hi there, seems very cpu intensive + i have no clue about complex, and why use exp, the rotation is linear right ? ( it rotates good in screen so i bet );

I mean : i just have a wheel or circlesaw rotating.

float rot = 0.0;

// in loop

{

rot += fElapsedtime;

// now i wanto know if its better to use this :

if( rot > Pi )rot -= Pi;

// otherwise the float rot will be increasing unlimited, i dont know how if affects the cpu;

}

So my question is :

What is more CPU intensive, having the if, or having the huge float numbers ?

Edited by the incredible smoker

##### Share on other sites

Thanks Waterlimon and Alvaro about the presision, i did not notice that yet, to be honest i have no clue whats the underlaying idea about float, i was just asking to be sure.

With this information i now know i need the "if", the "if" is faster then any function like fmod i,m sure.

Maybe you all dont worry much about CPU intensivity as i do, i like many things in screen, not just 1 circlesaw, maybe you all have a i7 instead of a celeron ?

By example : your better off with a "if" instead of a "min()" or "max()" in terms of CPU usage, i avoid everything to be honest.

greetings and thanks again

##### Share on other sites

With this information i now know i need the "if", the "if" is faster then any function like fmod i,m sure.

It's probably the other way around.

Modern CPUs are very complex to guess "what's faster" without context or actual profiling; but an 'if' requires a branch, and branch can involve pipeline stalls and cache misses (and if you're not targeting x86/x64 PCs, it also involves an LHS - Load-Hit-Store which is incredibly expensive).

fmod uses only math to do the same thing, and as such, can easily be pipelined (thus will run fast on most architectures).

Because the performance of the 'if' variant highly depends on branch predictors, its performance can't be evaluated without context (which means knowing the state of the branch predictor).

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 17
• 21
• 13
• 20