Reputation: 326
Recently I found that there is ambiguity in results of C++ fmod function and its equivalent Math.IEEERemainder in Vb.net
If we calculate fmod(4.1887902053333335 / 6.283185307, 1.0) in C++ we get, 0.6666666677277 while Math.IEEERemainder(4.1887902053333335 / 6.283185307, 1.0) in Vb.net results in -0.33333333322723 thus we find complete difference in results seriously affects output.
I am currently working on a project which has several mathematical operations including sine , hyperbolic cosine, modulus etc. which is originally in C++ and I am tasked to convert it in Vb.net.
While most of the code can simply be pulled in and out of online converters on web these mathematical ambiguities remain hidden damaging the result.
Does anyone here know of such known differences especially for Math class in Vb.net with respect to C++?
Upvotes: 4
Views: 179
Reputation: 10931
Remember that VB.NET uses "Bankers' rounding" if you convert floating point numbers to integral numbers, so CInt(3.5)
is (int)round(3.5)
, both 4
, not just (int)3.5
, which is CInt(Math.Floor(3.5))
, both 3
.
Upvotes: 0
Reputation: 41331
The doc on Math.IEEERemainder says the following:
The IEEERemainder method is not the same as the modulus operator. Although both return the remainder after division, the formulas they use are different. The formula for the IEEERemainder method is:
IEEERemainder = dividend - (divisor * Math.Round(dividend / divisor))
In contrast, the formula for the modulus operator is:
Modulus = (Math.Abs(dividend) - (Math.Abs(divisor) * (Math.Floor(Math.Abs(dividend) / Math.Abs(divisor))))) * Math.Sign(dividend)
So, it's just a different function.
Upvotes: 5