测试给定数字是否为整数
我正在尝试实现一个函数来测试一个数字是否是一个整数:
#include <iostream>
#include <typeinfo>
bool integer(float k) {
if (k == 20000) return false;
if (k == -20000) return false;
if (k == 0) return true;
if (k < 0) return integer(k + 1);
if (k > 0) return integer(k - 1);
return false;
}
int main() {
float s = 23.34;
float s1 = 45;
cout << boolalpha;
cout << integer(s) << endl;
cout << integer(s1) << endl;
}
所以这个想法是,如果一个数字是一个整数,那么它是负数还是正数并不重要。如果我们减少或增加一,我们一定会得到零,但问题是,我们如何创建增加和减少的上限和下限?
I am trying to implement a function which tests if a number is an integer:
#include <iostream>
#include <typeinfo>
bool integer(float k) {
if (k == 20000) return false;
if (k == -20000) return false;
if (k == 0) return true;
if (k < 0) return integer(k + 1);
if (k > 0) return integer(k - 1);
return false;
}
int main() {
float s = 23.34;
float s1 = 45;
cout << boolalpha;
cout << integer(s) << endl;
cout << integer(s1) << endl;
}
So the idea is that if a number is an integer, it does not matter if it is negative or positive. If we decrease or increase it by one, we must get zero, but the problem is, that how can we create upper and lower bounds for increasing and decreasing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
该解决方案应该适用于所有可能的 k 值。我非常确定在这种情况下您可以使用
==
安全地比较浮点数。尝试深思熟虑地命名函数。
integer
没有给出任何线索它实际上做什么,所以我将函数名称更改为更有意义的名称。对于未来,测试一个数字是否为整数应该感觉像是一个非常简单的操作,因此您应该有一种强烈的感觉,最好的解决方案将非常简单。我希望您意识到您最初的解决方案由于多种原因是荒谬的(最大的原因:在绝大多数情况下它会导致堆栈溢出)。
This solution should work for all possible values of
k
. I am pretty sure this is a case where you can safely compare floats using==
.Try to thoughtfully name functions.
integer
does not give any clue what it actually does, so I changed the function name to something more meaningful.For the future, testing if a number is integer should feel like a very simple operation, so you should have a strong feeling that the best solution will be very simple. I hope you realize your original solution is absurd for many reasons (biggest reason: it will cause a stack overflow for the vast majority of cases).
为什么不直接做这样的事情:
?
(当然,请随意使用正确的 C++ 类型转换。)
Why not just do something like this:
?
(Feel free to use proper C++ type casts of course.)
这是行不通的,因为对于足够大的浮点数,
x-1 == x
。您应该测试浮点数的位模式以检查小数部分是否为 0。
This is not going to work, as for sufficiently large floats,
x-1 == x
.You should test the bit pattern of the float to check whether the fractional part is 0.
将其在 limit.h 宏中设置为 INT_MAX (对于最大值)或 INT_MIN (对于最小值)以获得整数
正确答案
its in limit.h macro set to INT_MAX (for maximum) or INT_MIN (for minimum ) for the integers
correct answer
我们可以使用 math.h 中的 trunc 方法
We could use the trunc method from math.h
我想到了一个更简单的方法。
考虑一个浮点数,比如 1.5。这个数字的下限(即1)和这个数字的上限(即2)是不同的。对于任何包含小数部分的值都是如此。
另一方面,整数的下限和上限值相同。因此,可以很容易地检查数字的上限值和下限值,从而确定它是否是整数。
I thought of a simpler way.
Consider a float number, say 1.5. The floor of this number (i.e. 1) and the ceiling of this number (i.e. 2) are different. This is true for any value having a decimal part in it.
On the other hand, an integer has both the floor and ceil values as the same. So, it'll be easy to check the ceil and floor values of the number, and hence, see if it is an integer.
您可以只使用 boost 词法转换标头
You can just use the boost lexical cast header
下面是您的问题的有效代码。
现在我将尝试借助 2 个极端案例示例来解释我的代码。
情况 1:给定的要检查的数字 = 1.10。因此 num = 1.10 且 n = 1。但是现在,num - n = 0.10 并且不等于 0。因此代码结果为 false!
情况 2:给定要检查的数字 = 1。因此 num = 1 且 n = 1。但是现在,num - n = 0,这等于 0。因此代码结果为 true!
Below is a working code for your question.
Now I will try to explain my code with the help of 2 corner case examples.
Case 1: Given number to check = 1.10. Thus num = 1.10 and n = 1. But now, num - n = 0.10 and this is not equal to 0. Hence the code results in false!
Case 2: Given number to check = 1. Thus num = 1 and n = 1. But now, num - n = 0 and this is equal to 0. Hence the code results in true!
好吧,为什么不只是这个?
Well, why not just this??