Is there any function like floorMod in Java? #67953
Replies: 3 comments 7 replies
-
floorMod is a function whereas % is an operator. Functions are part of frameworks/libraries which require importing while operators are built into the compiler and can be used anywhere. As far as being useful, it is subjective since not everyone has the same requirements. I recommend you check out the existing Math class and use the methods in it to create your own methods which suit your requirements. Also, it is worth mentioning that the Java function you linked calculates the modulus of two integers and the C# operator you referenced is the remainder operator, they do not work the same and will yield different results. Additional links: |
Beta Was this translation helpful? Give feedback.
-
There can be multiple options for rounding of division when any operand is negative. Currently there is only one option implemented in .NET, which is rounding quotient towards 0 and making remainder the same sign of dividend.
It depends. Notice that the result of integer The rule of .NET is also the rule of x86 IDIV instruction. |
Beta Was this translation helpful? Give feedback.
-
For those unfamiliar
The C spec then gives an example that the IEEE 754 compliant is equivalent to: double fmod(double x, double y)
{
double result;
result = remainder(fabs(x), (y = fabs(y)));
if (signbit(result)) result += y;
return copysign(result, x);
}
C# defines
These definitions are notably very close to each other with the difference being in how rounding occurs. There is a related issue #62992 that discusses some of the perceived differences here between spec and implementation noting that the runtime effectively implements floating-point You'll first note that Then when you consider Now when compared to the official C# definition there is a quirk here since The question then remains to determine whether or not |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int-
There is some kind of discussion in stackoverflow
It might be more useful than %?
Beta Was this translation helpful? Give feedback.
All reactions