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