# Could this be done better ?

• 07-10-2004
big146
Could this be done better ?
I was wondering if there was any one in here that could tell me if this code could be done better or faster. Take a look.
Code:

```void VectorAngles( const float *forward, float *angles ) {         float        tmp, yaw, pitch;                 if (forward[1] == 0 && forward[0] == 0)         {                 yaw = 0;                 if (forward[2] > 0)                         pitch = 90.0;                 else                         pitch = 270.0;         }         else         {                 yaw = (atan2f(forward[1], forward[0]) * 57.3f);                                         if (yaw < 0) yaw += 360.0;                 tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);                                 pitch = (atan2f(forward[2], tmp) * 57.3f);                                 }                 angles[0] = pitch;                 angles[1] = yaw;                 angles[2] = 0; }```
• 07-11-2004
MortalMonkey
You might want to let the functions take the floats independently, or even use structs, so you don't accidentally pass an undersized pointer/array and get GPFs. Also, if you name them appropriately (IE *X, *Y, *Z, P, Y), it will be much easier to see what you're doing in terms of vector maths.

Secondly, you could just write
Code:

`if (!forward[1] && !forward[0])`
Code:

`if (forward[1] == 0 && forward[0] == 0)`
• 07-11-2004
Erhard Henkes
This function works with pointers and changes array elements outside its scope. In big programs this is not desired. Perhaps you should use classes and references (C++ style).

Some people need faster sqrt-function than in <cmath>. Just try FastSQRT(...):
Code:

```#include <iostream> #include <conio.h> #include <cmath> #include <ctime> // clock_t, clock(), ... using namespace std; float FastSQRT( float r ) {     // Null und negative Zahlen abfangen     //if( r == 0.0f ) return 0.0f;     if( r <  0.0f ) r = -r    ;       // Startwert festlegen     float val = r;     float halfval = 0.5f * r;     unsigned long *ptr = (unsigned long*)&r;     *ptr = ( 0xbe6f0000 - *ptr ) >> 1;     float temp = r;     // Zwei Schritte reichen     temp *= 1.5f - temp * temp * halfval;     temp *= 1.5f - temp * temp * halfval;     //temp *= 1.5f - temp * temp * halfval; //höhere Präzision        // Rückgabe     return val*temp; } int main() {   double x;   cout << "Bitte Zahl eingeben fuer Wurzelziehen: ";   cin  >> x;   cout << FastSQRT(x) << endl;   cout << sqrt(x)    << endl << endl;       // Zeitmessung   const int NMAX = 20;   int max;   cout << "Anzahl Schleifendurchlaeufe? ";   cin >> max;   clock_t t1,t2;   double ts, tm=0;   double a;   cout << endl;   for(int n=0; n<NMAX; ++n)   {       t1 = clock(); // Start       for( int i=0; i<max;i++)       {         a = FastSQRT(x); //zu messende Aktion 1       }       t2 = clock(); // Ende       ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); // Zeit in Sekunden.       cout << "Zeit Aktion 1: " << ts << " sec" << endl;       tm += ts; // Das ist das Gleiche wie tm = tm + ts        }   tm/=NMAX; // Das ist das Gleiche wie tm = tm / NMAX   cout << "Durchschnitts-Zeit bei Aktion 1: " << tm << " sec" << endl;   tm=0;   cout << endl;   for(int n=0; n<NMAX; ++n)   {       t1 = clock();       for( int i=0; i<max;i++)       {         a = sqrt(x); //zu messende Aktion 2       }       t2 = clock();       ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC);       cout << "Zeit bei Aktion 2: " << ts << " sec" << endl;       tm += ts;      }   tm/=NMAX;   cout << "Durchschnitts-Zeit bei Aktion 2: " << tm << " sec" << endl;   getch(); }```
• 07-11-2004
big146
Thks for the reply's. according to my output running the code u posted erhard, the sqrt functions is faster.
• 07-11-2004
Sang-drax
I think modern compilers use special intructions for sqrt(), making such hacks worthless.
• 07-14-2004
big146
What about using valarray for the code above?If yes, could someone show me how to implement it.
• 07-14-2004
okinrus
You should be able to create lookup tables for the atan2 calls.

You might also want to read this site. I done too much with valarrays. http://cs.calvin.edu/books/c++/intro.../Valarrays.pdf