remquo Results Not Making Sense

后端 未结 2 1458
不知归路
不知归路 2020-12-22 05:23

I read here that remquo should return:

If successful, returns the floating-point remainder of the division x/y as defined in

相关标签:
2条回答
  • 2020-12-22 05:45

    Since your question is about the value returned by remquo, it is entirely about std::remainder, since that part of remquo's behavior is defined directly as identical to that of std::remainder.

    std::remainder provides the IEEE 754 remainder operation, which is different from fmod in that the fmod of two positive values can be expected to be always positive, whereas the IEEE 754 remainder is defined with respect to the integral quotient q as the nearest integer to the mathematical quotient x/y, so that remainder = x - y*q produces a negative result each time the mathematical quotient rounds up to the next integer.

    The IEEE 754 remainder operation is the one being discussed in the question “Math.IEEERemainder returns negative results.” so you might want to read it and the accepted answer although they are about a different programming language.

    Note: the part of the specification about the quotient being “congruent modulo 2n to the magnitude of the integral quotient” simply means that you do not get the entire integral quotient, which indeed might not fit an int type (think of remquo(FLT_MAX, 1.0f, ...). Instead, you get an implementation-defined number of the least significant bits of the integral quotient. The implementation-defined number of bits that you get must be at least three, but can be more.

    0 讨论(0)
  • 2020-12-22 05:50

    The behavior is correct; as stated in std::remainder, the quotient is rounded to nearest integer, then the remainder could be negative.

    If you are working with integer, I suggest you the C function div()

    0 讨论(0)
提交回复
热议问题