tags:

views:

204

answers:

3

Does anybody know what the C# equivalent of fmod is? I'm trying to convert this line of code to C#.

$lon_rad = fmod(($long1+$dlon_rad + M_PI), 2*M_PI) - M_PI;

Here's what I have so far. I just need the fmod converted.

double lon_rad = ((lon1+dlon_rad + Math.PI), 2*Math.PI) - Math.PI;
A: 

Have you tried the % modulus operator?

AaronLS
fmod is totally different.
plinth
@plinth: "fmod — Returns the floating point remainder (modulo) of the division of the arguments" -- PHP Manual
R. Bemrose
@plinth "The modulus operator (%) computes the remainder after dividing its first operand by its second." -- C# manual
R. Bemrose
fmod works on floating point numbers - the fmod(10.0, 3.1) is different from (double)((int)10.0 % (int)3.1). "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."
plinth
@R. Bemrose, they're not the same. `fmod(5.7, 1.3)` gives `0.5`, while `5.7 % 1.3` gives `0`.
Ionuț G. Stan
@Ionut G. Stan: `double test = 5.7 % 1.3; Console.WriteLine(test.ToString();` prints `0.5`
R. Bemrose
@R. Bemrose. You are right. In c# it behaves differently.
Ionuț G. Stan
or rather, it would had I not omitted one of the paratheses (whoops)
R. Bemrose
Stan, you may have a casting issue. Because of the way C# will coerce types during arithmetic, sometimes you have to explicitely cast them and/or specify the types of numeric literals. Can you give a more complete code for how you tested "5.7 % 1.3 gives 0"
AaronLS
I stand corrected - msdn says that modulus is defined for all numeric types, and % for doubles *is* fmod.
plinth
+2  A: 

Use a P/Invoked call into the standard library (from here):

[DllImport("msvcrt.dll")]
static extern double fmod(double x, double y);
plinth
That works but I was hoping for a C# solution without having to import a DLL.
John
You will always have that dll.
plinth
CORRECTION: do not use this. Use the built-in modulus operator, it works correctly for all numeric types. Pinvoking to fmodf will add unnecessary overhead.
plinth
I changed the accepted answer to the one that uses the modulus operator. Thanks all.
John
"You will always have `msvcrt.dll`". Anyone heard of Mono? Silverlight?
MarkJ
+3  A: 

Other people have given the correct operator to use, but not the entire syntax, which would be:

double lon_rad = ((lon1+dlon_rad + Math.PI) % (2*Math.PI)) - Math.PI;
R. Bemrose
I would change the "2" to "2.0"
AaronLS
That's probably a good idea for optimization, but I'm sure that it would affect the results, as Math.PI is already a double.
R. Bemrose
True. I knew PI was a double, but I can never remember which direction it will coerce the operands whenever they are of different types so I eliminate any doubt by clarifying the numeric literals.
AaronLS
It always makes them the larger of the two types. It makes sense if you think about similar types. For instance, if I multiply a byte by an int, the result will be an int because there are int values outside the byte range.
R. Bemrose