[英]C sqrt == long int
I don't know why if I have the following code:如果我有以下代码,我不知道为什么:
int main() {
long int height_cat, number_worker_cats, number_helper_cats, height_tree;
bool flag;
scanf("%ld%ld", &height_cat, &number_worker_cats);
for (number_helper_cats = 1; ; ++number_helper_cats) {
for (height_tree = 1; (long int)pow(number_helper_cats + 1, height_tree) <= height_cat; ++height_tree) {
if ((long int)(pow(number_helper_cats + 1, height_tree) - height_cat) == 0 && (long int)(pow(number_helper_cats, height_tree) - number_worker_cats) == 0) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
printf("%ld, %ld\n", number_helper_cats, height_tree);
}
I'm searching for number_helper_cats
and height_tree
which (number_helper_cats +1)^height_tree = height_cat
and number_helper_cats^height_tree = number_worker_cats
where height_cat
and number_worker_cats
are integers
.我正在寻找number_helper_cats
和height_tree
其中(number_helper_cats +1)^height_tree = height_cat
和number_helper_cats^height_tree = number_worker_cats
其中height_cat
和number_worker_cats
是integers
。
For example if height_cat = 216
and number_worker_cats = 125
, the code will stop on number_helper_cats = 5
and height_tree = 3
since (5+1)^3 = 216
and 5^3 = 125
.例如,如果height_cat = 216
和number_worker_cats = 125
,代码将在number_helper_cats = 5
和height_tree = 3
停止,因为(5+1)^3 = 216
和5^3 = 125
。
But if I have the following code it doesnt work, loops forever, why?但是如果我有以下代码它不起作用,永远循环,为什么?
int main() {
long int height_cat, number_worker_cats, number_helper_cats, height_tree;
bool flag;
scanf("%ld%ld", &height_cat, &number_worker_cats);
for (number_helper_cats = 1; ; ++number_helper_cats) {
for (height_tree = 1; pow(number_helper_cats + 1, height_tree) <= height_cat; ++height_tree) {
if ((long int)(pow(number_helper_cats + 1, height_tree)) == height_cat &&
(long int)(pow(number_helper_cats, height_tree)) == number_worker_cats) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
printf("%ld, %ld\n", number_helper_cats, height_tree);
}
Everything is long int
and every height_cat and number_worker_cats for testcase are true for the operations, another example height_cat = 5764801, number_worker_cats = 1679616, number_helper_cats = 6 and height_tree = 8 because (6 + 1)^8 = 5764801, 6^8 = 1679616. But again the first code runs well, and the second one loops forever.一切都是long int
并且测试用例的每个 height_cat 和 number_worker_cats 对于操作都是正确的,另一个例子 height_cat = 5764801, number_worker_cats = 1679616, number_helper_cats = 6 和 height_tree = 8 因为 (6 + 1)^8 = 5764801, =167^6 . 但是第一个代码再次运行良好,第二个代码永远循环。 pow are precise I mean 6^3 = 216 and 5^3 = 125 right? pow 是精确的我的意思是 6^3 = 216 和 5^3 = 125 对吗? :p :p
The result of pow
is double
, and double
numbers are not precise in a lot of cases. pow
的结果是double
,并且double
数字在很多情况下并不精确。 To test equality with a double
, a common method would be要使用double
测试相等性,常用方法是
if (abs(pow(number_helper_cats + 1, height_tree) - height_cat)) < 0.001); // 0.001 is an arbitrary small number
{
...
}
Aand for test for <=
, you should use pow(number_helper_cats + 1, height_tree) <= height_cat + 0.001
.对于<=
测试,您应该使用pow(number_helper_cats + 1, height_tree) <= height_cat + 0.001
。
However, I must mention that you code cannot produce the infinite loop you mentioned in your quesiton with my gcc 4.7.2 .但是,我必须提到您的代码无法使用我的 gcc 4.7.2 生成您在问题中提到的无限循环。 All your loops just end normally.你所有的循环都正常结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.