如何检查除法的结果是否为 C 中的整数?
How can I check if the result of a division is an integer in C?
我需要检查数学除法的结果是否为整数。
例如,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
,所以我无法真正将这些部分组合在一起。
我不明白你函数的逻辑。但也许这对你有帮助。将除法结果的 %
模运算结果与除法结果的底版本进行比较,以 0
。如果等于 0
, return true;
, 如果不等于 return false;
.
在 long long int res_floored = res;
处发生了从 double
到 long long int
的隐式转换 - 值下降,f.e。 4.7
到 4
。不需要显式转换。
在此之前,我们必须检查除法结果的 floored double
值是否能够保存在 long long int
中。因此,我将res
与宏LONG_MAX
和LONG_INT
、headerlimits.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
,因为两个floating-point值相除可以得到一个整数值。
#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
你似乎在尝试做这样的事情:
result = (double) num / i;
check = (int) result;
if(check == result) {
...
从逻辑上讲,这是正确的。但它在实践中行不通,因为浮点数没有无限精度。
检查整除性的正确方法是使用模运算符:
if(num % i == 0) {
// Code to run if num / i is an integer
您想测试整数除法是否没有余数:使用计算余数的模运算符 %
。 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 > 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;
}
我需要检查数学除法的结果是否为整数。
例如,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
,所以我无法真正将这些部分组合在一起。
我不明白你函数的逻辑。但也许这对你有帮助。将除法结果的 %
模运算结果与除法结果的底版本进行比较,以 0
。如果等于 0
, return true;
, 如果不等于 return false;
.
在 long long int res_floored = res;
处发生了从 double
到 long long int
的隐式转换 - 值下降,f.e。 4.7
到 4
。不需要显式转换。
在此之前,我们必须检查除法结果的 floored double
值是否能够保存在 long long int
中。因此,我将res
与宏LONG_MAX
和LONG_INT
、headerlimits.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
,因为两个floating-point值相除可以得到一个整数值。
#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
你似乎在尝试做这样的事情:
result = (double) num / i;
check = (int) result;
if(check == result) {
...
从逻辑上讲,这是正确的。但它在实践中行不通,因为浮点数没有无限精度。
检查整除性的正确方法是使用模运算符:
if(num % i == 0) {
// Code to run if num / i is an integer
您想测试整数除法是否没有余数:使用计算余数的模运算符 %
。 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 > 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;
}