fmod, fmodf
Visual Studio 2010
Calculates the floating-point remainder.
double fmod( double x, double y ); float fmod( float x, float y ); // C++ only long double fmod( long double x, long double y ); // C++ only float fmodf( float x, float y );
fmod returns the floating-point remainder of x / y. If the value of y is 0.0, fmod returns a quiet NaN. For information about representation of a quiet NaN by the printf family, see printf.
The fmod function calculates the floating-point remainder f of x / y such that x = i * y + f, where i is an integer, f has the same sign as x, and the absolute value of f is less than the absolute value of y.
C++ allows overloading, so you can call overloads of fmod. In a C program, fmod always takes two doubles and returns a double.
|
Function |
Required header |
|---|---|
|
fmod , fmodf |
<math.h> |
For additional compatibility information, see Compatibility in the Introduction.
.NET equivalent
In porting a C++ project to C#, I discovered that IEEERemainder, the supposed the ".NET Equivalent" mentioned above, is not really equivalent at all. Specifically, as best as I can tell, fmod() would need to be implemented in .NET as follows:
Hope this helps someone else out there.
private double fmod(double x, double y)
{
double remainder = Math.IEEERemainder(x, y);
if (!Double.IsNaN(remainder) && remainder != 0.0)
{
if (x >= 0.0)
{
if (remainder < 0.0)
remainder += Math.Abs(y);
}
else // x < 0.0
{
if (remainder > 0.0)
remainder -= Math.Abs(y);
}
}
return remainder;
}
Hope this helps someone else out there.
- 7/23/2010
- lowello
- 8/1/2010
- Thomas Lee