As answer well by @Keith Thompson, the C function works in radians and so a degrees to radian conversion is needed.
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
float angle_radians = angle_degrees * (float) (M_PI/180.0);
Yet rather that directly scale by pi/180.0
, code will get more precise answers, for angles outside a primary range, if code first does range reduction and then scales by pi/180.0
. This is because the scaling of pi/180.0
is inexact as machine pi float pi = 3.14159265;
is not exactly mathematical π. The radian range reduction, performed by cos()
, is in error as the computed radian value given to it is inexact to begin with. With degrees, range reduction can be done exactly.
// Benefit with angles outside -360° to +360°
float angle_degrees_reduce = fmodf(angle_degrees, 360.0f);
float angle_radians = angle_degrees_reduce * M_PI/180.0;
sind() is a sine example that performs even better by reducing to a narrower interval. With angles outside the -45° to +45° range, there is benefit.