remquo Results Not Making Sense

不问归期 提交于 2019-11-29 17:36:09
Pascal Cuoq

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.

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()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!