How can I check if the result of a division is an integer in C?

Viewed 1419

I need to check if the result of a mathematical division is an integer or not.

For example, 8 / 2 = 4 is ok. but 5 / 2 = 2.5 shouldn't be ok.

I've tried the following:

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;
}

I'm having nightmares about isdigit and how to insert the parameter in the right way. I know it requires an int, but I have a double, so I can't really put those pieces together.

3 Answers

It seems like you're trying to do something like this:

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

Logically, this is correct. But it will not work in practice because floats does not have infinite precision.

The proper way to check divisibility is using the modulo operator:

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

I don´t understand the logic of your function. But maybe this help you. Compare the result of the % modulo operation by the result of the division with the floored version of the division´s result, to 0. If it equals 0, return true;, if not return false;.

At long long int res_floored = res; happens an implicit conversion from double to long long int - The value gets floored, f.e. 4.7 to 4. No explicit cast is needed.

Before that, we have to check if the floored double value of the result of the division is capable to be hold in an long long int. Therefore, I compare res to the macros LONG_MAX and LONG_INT, header limits.h, which represent the maximum and minimum integral values a long int can hold. If it doens´t fit we return -1; as error.

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;
    }
}

I use double for both parameters, because the division of two floating-point values can result in an integral value.


#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));
}

Output:

1
0
1
0
1

You want to test if an integer division has no remainder: use the modulo operator % that computes the remainder. The isdigit() function has a different purpose: it tests whether a byte read by getc() is a digit ('0' to '9') or not.

Here is a modified version:

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;
}

Note that it would be much quicker for prime numbers to stop the search when i * i > 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;
}
Related