0

This often returns NAN ("Not A Number") depending on input:

#define PI 3.1415f

GLfloat sineEaseIn(GLfloat ratio) {
 return 1.0f-cosf(ratio * (PI / 2.0f));
}

I tried making PI a few digits smaller to see if that would help. No dice. Then I thought it might be a datatype mismatch, but float and glfloat seem to be equivalent:

gl.h

typedef float           GLfloat;

math.h

extern float  cosf( float );

Is this a casting issue?

John Feminella
  • 303,634
  • 46
  • 339
  • 357
user360092
  • 87
  • 8
  • There doesn't seem to be anything wrong with your casting. Could it be that ratio might be NAN or INF to start with? – Claus Broch Jun 11 '10 at 09:56
  • What are some example inputs that cause `NaN` when you don't expect them to? – John Feminella Jun 11 '10 at 09:56
  • Just as a side note, you don't need define `PI` (or pi/2) yourself. You can use `M_PI_2` defined in `` (preferably with either a cast to float or the `f` suffix appended to avoid unnecessary conversions at runtime). – Stephen Canon Jul 28 '10 at 15:50

1 Answers1

2

I suspect that one of the following is afoot:

  • your input value to ratio may not be what you expect it to be, and ratio itself is possibly NaN
  • the cosf that you're calling isn't the one in math.h

Otherwise, there doesn't appear to be anything wrong with your expression.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • Thanks for your help! I was off-by-one with my enum that was selecting the easing function, so I was basically debugging the wrong easing function. This is the bad one and I know why now: GLfloat circularEaseInOut(GLfloat ratio) { return ((ratio *= 2) < 1) ? -0.5*(sqrtf(1-ratio*ratio)-1) : 0.5*(sqrtf(1-(ratio-=2)*ratio)+1); } I changed those numbers to floats and everything works now! Thanks for your help! I would probably still be chasing that typecasting idea down the rabbit hole. – user360092 Jun 11 '10 at 10:36