What represents Math.IEEERemainder(x,y) in C++?
3 Answers
-
2+1. Answers to the effect of "use the modulus operator" miss the point that modulus is not the same operation as fmod. – John Feminella Dec 28 '09 at 23:21
-
It takes 18 characters to express in C# what C++ can say in four. It's like an allegory. – Crashworks Dec 28 '09 at 23:34
-
1@Crashworks: Indeed. C# uses 18 characters to clearly express intent, while C++ mumbles what sounds like a curse. – Michael Petrotta Dec 28 '09 at 23:39
-
Except that `Remainder` expresses what the function does much better than `fmod`. EDIT: Ninja'd by Michael. – GManNickG Dec 28 '09 at 23:40
-
1`fmod` is a different function with different numerical properties and applications. It’s not enough to point at it without explaining the differences (and we have `std::remainder`now anyway). – Davis Herring Mar 05 '19 at 01:59
In the C++11 standard library, std::remainder(x,y)
, is now the equivalent function of C#'s Math.IEEERemainder(x,y)
in C++.
From: http://en.cppreference.com/w/cpp/numeric/math/remainder
Computes the IEEE remainder of the floating point division operation
x/y
The IEEE remainder is x–(round(x/y)*y)
Whereas, the fmod
remainder is x - trunc(x/y)*y
, which can lead to different answers such as was raised in this question: Why am I getting a different result from std::fmod and std::remainder
If you really want to get the IEEE-defined remainder, you need std::remainder(x,y)
(or define your own function if you can't use C++11)
Although marked as the answer, SLak's answer is not correct.
Yes both calculate a modulo, but they differ in how signs are treated. (ie. John Feminella is correct). An implementation (and links in to the MSDN documentation) can be found here:
Is Math.IEEERemainder(x,y) equivalent to x%y?
Note that the IEEERemainder() function implements modulo in an IEEE standard manner - rather than what K&R thought of back in the early 70s.