简体   繁体   English

如何检查商是否是整数? C ++

[英]How to check if quotient is an integer? C++

I'm a beginner and I am looking for a way to check if a quotient in C++ is an integer. 我是一个初学者,我正在寻找一种方法来检查C ++中的商是否为整数。

For example: 例如:

int a;
double b;
cin >> a;
b = a / 4;

How do I know that b is not an integer (something like 5/4=1.25)? 我怎么知道b不是整数(类似于5/4 = 1.25)?

You don't need to check whether b is an integer, because it always will be. 您无需检查b是否为整数,因为它始终是整数。 You declared it int . 您将其声明为int

Furthermore, because both a and 4 are also integers, a/4 is integer division, so even the expression can never be fractional. 此外,由于a4都是整数,所以a/4是整数除法,因此即使表达式也永远不能是分数。

If you fix all of that, one way to see whether a floating-point value represents a whole number is to compare its original value with one deliberately truncated to an integer: 如果解决了所有这些问题,查看浮点值是否代表整数的一种方法是将其原始值与一个故意截断为整数的值进行比较:

foobar == static_cast<int>(foobar)

However, as with any floating-point operations this is potentially susceptible to rounding errors so you would need to be quite a bit more specific with your general requirements. 但是,与任何浮点运算一样,这很可能会产生舍入错误,因此您将需要更加具体地满足一般要求。

In the particular case shown, it would be much simpler to check modulo instead: 5 % 4 is not zero, so 5/4 is not whole. 在所示的特定情况下,取模取余会简单得多: 5 % 4不是零,所以5/4不是整数。

Before the edit : 编辑之前

Your a and b variables are of type int as you declared them as such. 您声明的ab变量均为int类型。 They will remain of type int no matter the values you try to supply to them via the standard input. 无论您尝试通过标准输入提供给它们的值,它们都将保持int类型。 The type and a result of the a / 4 expression is also an int because all operands are integers involved in integer division . a / 4表达式的类型和结果也是一个int因为所有操作数都是整数除法涉及的整数

After the edit: 编辑后:

If b is declared to be of type double it will remain of type double but will receive the cut-out result of an integer division as a / 4 is of type int . 如果将b声明为double类型,则它将保留为double类型,但由于a / 4的类型为int 因此将收到整数除法的切除结果。 So if all operands are integers the result of an arithmetic expression will also be an integer. 因此,如果所有操作数都是整数,则算术表达式的结果也将是整数。 To make it of type double cast one of the operands to double : 要使其类型为double ,请将操作数之一转换为double

double b = static_cast<double>(a) / 4;

or 要么

double b = a / 4.;

In languages like C when declaring a variable of an integer type, the value therein always will be an integer. 在像C这样的语言中,当声明一个整数类型的变量时,其中的值始终是整数。 However I guess what you're asking is, how to check if the result of a computation is a integer value. 但是我猜您在问的是,如何检查计算结果是否为整数值。 If you're doing arithmetic with integer types the most naive to understand way to do this, is to perform the inverse operation and test if the result matches the original input. 如果您要对整数类型进行算术运算,最天真地了解这样做的方法是执行逆运算并测试结果是否与原始输入匹配。

EDIT If both operands are of integer type (as before the question edit), then you can do this: 编辑如果两个操作数都是整数类型(与问题编辑之前一样),则可以执行以下操作:

b = a / 4;
if( b*4 == a ){
    cout << "a/4 is integer";
}

However there are other ways to do this. 但是,还有其他方法可以做到这一点。 For example using the remainder operator % (often confused with modulo – or rather a misnormer). 例如,使用余数运算符% (通常与取模运算符混淆-或更确切地说是误称)。 On many CPU architectures the operation performing a division also gives the remainder. 在许多CPU架构上,执行除法运算也会得到余数。 This 这个

b = a / 4;
if( b%4 == 0 ){
    cout << "a/4 is integer";
}

will be close to optimal, because a single CPU instruction will perform both computations (over the first two lines) and the comparision will usually test for a special flag set by the division operation if no remainder was left. 它将接近最佳,因为一条CPU指令将执行两个计算(在前两行中),并且如果没有剩余余数,则比较通常会测试除法运算设置的特殊标志。

*However if floating point arithmetic enters the pictures (as how after the question edit), things become a little bit more troublesome. *但是,如果浮点算术输入图像(问题编辑后如何进行),事情就会变得有些麻烦。 You no longer can do the inversion test, because floating point arithmetic is imprecise¹. 您不再可以进行反转测试,因为浮点运算不精确¹。

If all you're interested in is the remainder/modulo, your best bet is using the fmod function and then testing if the result is within the margin of precission error epsilon. 如果您只对余数/模感兴趣,那么最好的选择是使用fmod函数,然后测试结果是否在精度误差epsilon的范围内。

if( DBL_EPSILON > fmod(a, 4) ){
        cout << "a/4 is integer (within double floating point precision)";
}

If you're in need of both quotient and remainder then my preferred method is to truncate and subtract (which is usually faster): 如果您同时需要商和余数,那么我的首选方法是截断并减去(通常更快):

double b = a/4.;
double b_r = b - trunc(b);
if( DBL_EPSILON > fabs(b_r) ){
        cout << "a/4 is integer (within double floating point precision)";
}

1: for the majority of numbers involved; 1:涉及的大多数数字; there's a set of numbers, which when used in floating point arithmetic operations will yield exact results. 有一组数字,在浮点算术运算中使用时,它们会产生精确的结果。 But this is only a small subset of all the numbers representable by floating point. 但这只是浮点可表示的所有数字的一小部分。

As other answers suggest, if you assign the division to a float you'll be getting an integer value always, since in C++ (and most languages) integer division results in an integer. 正如其他答案所建议的那样,如果将除法分配给浮点数,则将始终获得整数值,因为在C ++(和大多数语言)中,整数除法会生成整数。

In fact, you don't even need b to be a float . 实际上,您甚至不需要b就是float You can do one of the following: 您可以执行以下操作之一:

Use the modulo operator <- recommended 使用模运算符<- 推荐

int a;
std::cin >> a;
if (a % 4 == 0) { /* do something */ }

Use a divisor-specific divisibility test 使用除数特定的除数测试

int a;
std::cin >> a;
if (a & 0x11 == 0) { /* do something */ }

An integer is divisible by 4 if its least-significant 2 bits are 0. But, in fact, the compiler will probably optimize the previous test to this one. 如果整数的最低有效2位为0,则该整数可以被4整除。但是,实际上,编译器可能会将先前的测试优化为该整数。

Multiply the integral quotient 乘以整数商

If you intended to use a/4 anyway, try: 如果您仍然打算使用a / 4,请尝试:

int a;
std::cin >> a;
auto b = a / 4; /* b is an int... */
if (b * 4 == a) { /* do something */ }

This isn't very useful for the case of a fixed divisor; 对于固定除数的情况,这不是很有用; but if it hadn't been fixed, you might do: 但是,如果尚未修复,则可以执行以下操作:

int a;
std::cin >> a;
std::cin >> c;
auto b = a / c;
if (b * c == a) { /* do something */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM