1

In C++ does a modulus use any floating point math behind the scenes?

int x = 1234;
int y = 5678;
int z = y % x;  // any floating point used underneath to calculate the integer result?

As background, I was thinking about this question where he said he couldn't use any floating point without FP emulation. Then I realized that I wasn't sure if the modulus operator used any sort of floating point assembly operations. My guess is it does not, but I would like to be sure.

Community
  • 1
  • 1
kmort
  • 2,848
  • 2
  • 32
  • 54

3 Answers3

3

No1. Refer to some implementations of such an operator.


1 An implementation can do whatever it wants insofar as the observed behavior is within the specification. However, I don't know of any implementation which would choose to use floating point operations, nor can I think of a general justification for doing so.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
2

No it does not use the floating point arithmetic. The result can be obtained very simply

z = y - ( y/x ) * x;

Early computers sometimes have no floating point coprocessor. So such operations are performed by using machine commands that operate with integer numbers.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

It's up to the implementation how "C++" calculates the modulus "behind the scenes"

twin
  • 1,669
  • 13
  • 11