如何在c++中检查一个数字是否能被2*M_PI整除?

时间:2021-11-17 19:30:56

This is what I've done for checking the divisibility of a number by 2*M_PI. "w" is a constant that's 2/3 and t is the variable that varies by t += dt, where dt is 0.1. I'm trying to use the mod operator, %, to see if something is divisible. But its not working.

这就是我检查2*M_PI值的可分割性的方法。w是一个常数,等于2/3 t是变量t += dt, dt是0。1。我试着使用mod运算符%,看看是否有什么可分割的。但它不工作。

bool divisible; real w = 2/3; real t;

bool整除;真正的w = 2/3;真正的t;

if((w*t) % 2*M_PI == 0)
    {
        divisible = true;
    }

else
    {
        divisible = false;
    }

This is the error that I get, "invalid operands of types ‘real’ and ‘int’ to binary ‘operator%’"

这是我得到的错误,“从' real '和' int '类型到' operator% '的无效操作数”

What does this mean? How do I get this to work? So do I need to make w and t an int? They can't be because w is 2/3, and t increments from 0 by 0.1. Can someone please help me?

这是什么意思?我怎么让它工作呢?我需要把w和t设为int吗?不可能因为w等于2/3,t从0增加0。1。谁能帮帮我吗?

3 个解决方案

#1


3  

Use std::fmod instead, it operates on doubles rather than the integral % operator.

使用std::fmod代替,它对双精度操作而不是积分%操作符。

#2


2  

'%' is the integer modulo operator not working for float/double arguments/operands

'%'是不用于浮点/双参数/操作数的整型模运算符

There exists a float/double modf function in math.h which may help

数学中存在一个浮点/双模函数。h这可能有助于

#3


2  

Why would you want to know if a floating-point number is exactly divisible by another one?

为什么你想知道一个浮点数是否能被另一个整除?

Floating-point arithmetics should not be used for "precise" calculations. The outcome of every operation is defined strictly, but it differs from the mathematical meaning of the same operation. In particular:

浮点算术不应该用于“精确”计算。每个操作的结果都有严格的定义,但不同于同一操作的数学意义。特别是:

double a = 1e20;
double b = 1e-20;

double c = (a + b) - a;

You might expect that c will be equal to b, but in fact it won't!

你可能认为c会等于b,但实际上它不会!

You should only compare floating-point numbers with some window. Means - does the specific floating-point value lie within some finite-length range.

您应该只将浮点数与某个窗口进行比较。方法——特定的浮点值是否位于某个有限长度范围内。

#1


3  

Use std::fmod instead, it operates on doubles rather than the integral % operator.

使用std::fmod代替,它对双精度操作而不是积分%操作符。

#2


2  

'%' is the integer modulo operator not working for float/double arguments/operands

'%'是不用于浮点/双参数/操作数的整型模运算符

There exists a float/double modf function in math.h which may help

数学中存在一个浮点/双模函数。h这可能有助于

#3


2  

Why would you want to know if a floating-point number is exactly divisible by another one?

为什么你想知道一个浮点数是否能被另一个整除?

Floating-point arithmetics should not be used for "precise" calculations. The outcome of every operation is defined strictly, but it differs from the mathematical meaning of the same operation. In particular:

浮点算术不应该用于“精确”计算。每个操作的结果都有严格的定义,但不同于同一操作的数学意义。特别是:

double a = 1e20;
double b = 1e-20;

double c = (a + b) - a;

You might expect that c will be equal to b, but in fact it won't!

你可能认为c会等于b,但实际上它不会!

You should only compare floating-point numbers with some window. Means - does the specific floating-point value lie within some finite-length range.

您应该只将浮点数与某个窗口进行比较。方法——特定的浮点值是否位于某个有限长度范围内。