Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute fmod in C#?

Tags:

c#

math

For given floating point numbers x and a, I would like to compute r (and n) such that x = a*n + r . In C/C++ this function is called fmod. However I do not see a convenient function in .NET. Math.DivRem is only for integers ...

like image 774
Danvil Avatar asked Apr 22 '10 11:04

Danvil


People also ask

How is fmod calculated?

Formula of fmod:fmod= numerator - t*denominator.

What is fmod function?

Description. The fmod() function calculates the floating-point remainder of x/y. The absolute value of the result is always less than the absolute value of y. The result will have the same sign as x.

What library is fmod in C?

C library function - fmod() The C library function double fmod(double x, double y) returns the remainder of x divided by y.

What is fmod in math?

Definition and Usage. The math. fmod() method returns the remainder (modulo) of x/y.


2 Answers

I think you can just use % for floats as well. r = x % a

"Arithmetic operators (C# reference)":

All numeric types have predefined modulus operators.

like image 175
Andreas Brinck Avatar answered Oct 20 '22 17:10

Andreas Brinck


I think you are looking for System.Math.IeeeRemainder

like image 31
Daniel Elliott Avatar answered Oct 20 '22 16:10

Daniel Elliott