Skip to main content
added 226 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97

Avoid double functions for float problems

OP's code promotes the float to a double, calls the double function and then narrows the double result to a float. Better to call a float function and skip the promotion and narrowing.

Review function calls. Examples:

// float prime = fmod(h / 60.f, 6);
// float x     = 1 - fabs(fmod(prime, 2) - 1);
float prime = fmodf(h / 60.f, 6);
float x     = 1 - fabsf(fmodf(prime, 2) - 1);

Tip: Save time and enable all warnings as a well enabled compiler would advise about this.

Consider integer math

The if (prime) tree does a lot of FP compares against integers values. Consider converting prime to an int to perform the multiple compares -perhaps even a switch(). Profile to see if this improved performance.

Avoid double functions for float problems

OP's code promotes the float to a double, calls the double function and then narrows the double result to a float. Better to call a float function and skip the promotion and narrowing.

Review function calls. Examples:

// float prime = fmod(h / 60.f, 6);
// float x     = 1 - fabs(fmod(prime, 2) - 1);
float prime = fmodf(h / 60.f, 6);
float x     = 1 - fabsf(fmodf(prime, 2) - 1);

Tip: Save time and enable all warnings as a well enabled compiler would advise about this.

Avoid double functions for float problems

OP's code promotes the float to a double, calls the double function and then narrows the double result to a float. Better to call a float function and skip the promotion and narrowing.

Review function calls. Examples:

// float prime = fmod(h / 60.f, 6);
// float x     = 1 - fabs(fmod(prime, 2) - 1);
float prime = fmodf(h / 60.f, 6);
float x     = 1 - fabsf(fmodf(prime, 2) - 1);

Tip: Save time and enable all warnings as a well enabled compiler would advise about this.

Consider integer math

The if (prime) tree does a lot of FP compares against integers values. Consider converting prime to an int to perform the multiple compares -perhaps even a switch(). Profile to see if this improved performance.

Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97

Avoid double functions for float problems

OP's code promotes the float to a double, calls the double function and then narrows the double result to a float. Better to call a float function and skip the promotion and narrowing.

Review function calls. Examples:

// float prime = fmod(h / 60.f, 6);
// float x     = 1 - fabs(fmod(prime, 2) - 1);
float prime = fmodf(h / 60.f, 6);
float x     = 1 - fabsf(fmodf(prime, 2) - 1);

Tip: Save time and enable all warnings as a well enabled compiler would advise about this.