不能对双打使用模数吗?

发布于 2025-01-02 13:44:01 字数 256 浏览 2 评论 0原文

我有一个 C++ 程序(使用 g++ 编译)。我试图将两个双精度数作为操作数应用于模函数,但出现以下错误:

错误:“double”和“double”类型的操作数对二进制“operator%”无效

这是代码:

int main() {
    double x = 6.3;
    double y = 2;
    double z = x % y;
}

I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error:

error: invalid operands of types 'double' and 'double' to binary 'operator%'

Here's the code:

int main() {
    double x = 6.3;
    double y = 2;
    double z = x % y;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

做个少女永远怀春 2025-01-09 13:44:01

% 运算符用于整数。您正在寻找 fmod() 函数。

#include <cmath>

int main()
{
    double x = 6.3;
    double y = 2.0;
    double z = std::fmod(x,y);

}

The % operator is for integers. You're looking for the fmod() function.

#include <cmath>

int main()
{
    double x = 6.3;
    double y = 2.0;
    double z = std::fmod(x,y);

}
浮生面具三千个 2025-01-09 13:44:01

fmod(x, y) 是您使用的函数。

fmod(x, y) is the function you use.

別甾虛僞 2025-01-09 13:44:01

您可以实现自己的模函数来为您做到这一点:

double dmod(double x, double y) {
    return x - (int)(x/y) * y;
}

然后您可以简单地使用dmod(6.3, 2)来获得余数,0.3

You can implement your own modulus function to do that for you:

double dmod(double x, double y) {
    return x - (int)(x/y) * y;
}

Then you can simply use dmod(6.3, 2) to get the remainder, 0.3.

滴情不沾 2025-01-09 13:44:01

使用 中的 fmod()。如果您不想包含 C 头文件:

template<typename T, typename U>
constexpr double dmod (T x, U mod)
{
    return !mod ? x : x - mod * static_cast<long long>(x / mod);
}

//Usage:
double z = dmod<double, unsigned int>(14.3, 4);
double z = dmod<long, float>(14, 4.6);
//This also works:
double z = dmod(14.7, 0.3);
double z = dmod(14.7, 0);
double z = dmod(0, 0.3f);
double z = dmod(myFirstVariable, someOtherVariable);

Use fmod() from <cmath>. If you do not want to include the C header file:

template<typename T, typename U>
constexpr double dmod (T x, U mod)
{
    return !mod ? x : x - mod * static_cast<long long>(x / mod);
}

//Usage:
double z = dmod<double, unsigned int>(14.3, 4);
double z = dmod<long, float>(14, 4.6);
//This also works:
double z = dmod(14.7, 0.3);
double z = dmod(14.7, 0);
double z = dmod(0, 0.3f);
double z = dmod(myFirstVariable, someOtherVariable);
无法言说的痛 2025-01-09 13:44:01

这是一个同时适用于正数和负数的函数:

#include <cmath>

inline double nmod(double x, double y) {
    return std::fmod(std::fmod(x, y) + y, y);
}

如果 x 为负数或正数,它将始终返回正数。

请注意,如果y为负数,则返回值将为负数。如果您需要一个在任何情况下都返回正值的函数,那么需要使用 y 上的std::fabs(),即:

inline double nmod2(double x, double y) {
    return std::fmod(std::fmod(x, y) + std::fabs(y), y);
}

Here is a function that also works with both positive and negative numbers:

#include <cmath>

inline double nmod(double x, double y) {
    return std::fmod(std::fmod(x, y) + y, y);
}

It will always return a positive number if x is negative or positive.

Note that if y is negative, the returned value will be negative. If you need a function that will return a positive in any case, then one need to use std::fabs() on y, i.e.:

inline double nmod2(double x, double y) {
    return std::fmod(std::fmod(x, y) + std::fabs(y), y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文