Hello I am getting strange results for the fmod function in math.h. float f, g; for (f = 0.0; f < 4.0 * PI; f += (5.0 * PI / 180.0)) { g = fmod(f, (2.0 * PI)); printf("%f %f\r\n", f * 180.0 / PI, g * 180.0 / PI); } For first sweep from 0.0 to 2 * PI, I get zero for remainder, for second sweep from 2 * PI to 4 * PI if get sensible numbers. Compared in Visual C++ and first sweep produces remainders as expected. Is this a bug in fmod? Thanks!
Yes, in the C51 library is a problem with the fmod function. Use the following implementation instead:
float fmod ( float x, float y ) { long l; if (y == 0) return (0); l = (long) (x / y); x -= (((float) l) * y); return (x); }
"Use the following implementation instead..." OR, reconsider whether you should really be doing floating-point on an 8051 at all! Have you considered fixed-point?