【问题标题】:How can I check if the result of a division is an integer in C?如何检查除法的结果是否是C中的整数?
【发布时间】:2020-09-09 06:08:10
【问题描述】:

我需要检查数学除法的结果是否为整数。

例如,8 / 2 = 4 没关系。 但是 5 / 2 = 2.5 应该没问题。

我尝试了以下方法:

bool isPrime(int num) 
{
    /* Checks all the numbers before the given input. If the result of 
    dividing 
    the input by one of those numbers is an int, then the input is not a 
    prime number. */
    int i, check;
    double result;
    for (i=2; i<num; i++) {
        result = (double) num / i;
        check = (int) result;
        if (isdigit(check))
            return false;
    }
    return true;
}

我正在做关于isdigit 以及如何以正确方式插入参数的噩梦。我知道它需要一个int,但我有一个double,所以我不能真正将这些部分放在一起。

【问题讨论】:

  • isdigit 是关于字符,而不是数字,所以它似乎不是你要找的。​​span>
  • 使用% (modulus) 运算符检查除法后的余数:if (num % i == 0) /* num is a multiple of i --or-- num/i is an integer */;
  • 请显示整个函数。
  • 对于素数搜索,您不需要搜索最多 N-1 个;直到并包括ceil(sqrt(N)) 就足够了。如果 N 是合数,则一个因子不大于 √N,另一个不小于 √N)。在你检查了“被 2 整除”之后,你可以只检查奇数。如果您在循环外检查“可被 3 整除”,那么对于从 K 等于 1 开始的整数 K,所有较大的素数都具有 6K±1 的形式。即使 N 仅在数千个范围内,限制搜索范围也会产生根本的加速,更不用说更大了。使用“仅赔率”可以完成 1/2 的工作;使用 6K±1 做 1/3。

标签: c math integer division


【解决方案1】:

我不明白你的函数的逻辑。但也许这对你有帮助。将除法结果的% 模运算的结果与除法结果的下限版本进行比较,以0。如果它等于0return true;,如果不是return false;

long long int res_floored = res; 发生了从doublelong long int 的隐式转换 - 值会下降,例如4.74。不需要显式转换。

在此之前,我们必须检查除法结果的下限double 值是否能够保存在long long int 中。因此,我将res 与宏LONG_MAXLONG_INT 进行比较,标头limits.h 表示long int 可以容纳的最大和最小整数值。如果它不适合我们return -1; 作为错误。

int div_result_in_int (double dividend, double divisor);
{
    double res = dividend / divisor;

    if (res > LONG_MAX || res < LONG_MIN)
    {
        return -1;
    }

    long long int res_floored = res;

    if (res % res_floored == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

我对两个参数都使用double,因为两个浮点值相除可以得到一个整数值。


#include <stdio.h>
#include <limits.h>

#define true 1
#define false 0

int div_result_in_int (double dividend, double divisor)
{
    double res = dividend / divisor;

    if (res > LONG_MAX || res < LONG_MIN)
    {
        return -1;
    }

    long long int res_floored = res;

    if (res == res_floored)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main(void)
{
    printf("%d\n", div_result_in_int(8,4));
    printf("%d\n", div_result_in_int(9,5));
    printf("%d\n", div_result_in_int(3,1));
    printf("%d\n", div_result_in_int(97,14));
    printf("%d\n", div_result_in_int(2,0.5));
}

输出:

1
0
1
0
1

【讨论】:

  • 如果res 超出int 的范围,这将不起作用。
  • @chqrlie 现在好点了吗?
  • 技术上不行:如果res 超出long long int 的范围,它仍然不起作用。
  • 问题不明确:OP 是否要测试结果是否为整数或是否适合 int 或其他整数类型?
  • @chqrlie 嗯 :-/ 我让我的答案活着以供参考。
【解决方案2】:

您想测试整数除法是否没有余数:使用计算余数的模运算符%isdigit() 函数有不同的用途:它测试getc() 读取的字节是否为数字('0''9')。

这是修改后的版本:

bool isPrime(int num) {
    /* Checks all the numbers before the given input. If the result of 
       dividing the input by one of those numbers is integer, then the
       input is not a prime number. */
    int i;
    for (i = 2; i < num; i++) {
        if (num % i == 0)
            return false;
    }
    return true;
}

请注意,当i * i &gt; num 时,质数停止搜索会更快:

bool isPrime(int num) {
    /* Checks all the numbers before the given input. If the result of 
       dividing the input by one of those numbers is integer, then the
       input is not a prime number. */
    int i;
    for (i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return false;
    }
    return true;
}

【讨论】:

    【解决方案3】:

    您似乎正在尝试执行以下操作:

    result = (double) num / i;
    check = (int) result;
    if(check == result) {
        ...
    

    从逻辑上讲,这是正确的。但它在实践中不起作用,因为浮点数没有无限精度。

    检查整除性的正确方法是使用模运算符:

    if(num % i == 0) {
        // Code to run if num / i is an integer
    

    【讨论】:

    • @NateEldredge:在原始帖子中没有 num 的类型,并且 OP 提到了 double 类型。但是用例非常明显。我的坏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2016-04-14
    • 2017-12-14
    • 2023-03-27
    • 2014-11-02
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多