使用模时如何解决 C++ 输出负数?

How I can solve C++ output negative numbers when using modulo?

在我编写的代码中,我使用了以下两个函数来计算显示负数的 mod。

fmod(-10,11)
(-10, 11)

虽然correct answer is 1。它总是在 c++ 中显示 answer -10。我该如何解决?

From cppreference.com:

double fmod (double numer, double denom);
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.

在您的情况下,它是 -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10,这对于 fmod 的实施是正确的。如果您需要其他答案,您应该实现自己的版本,因为 modulo 以不同的方式定义负数。