C++ 测试给定数字是否为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7646512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing if given number is integer
提问by dato datuashvili
I am trying to implement user defined function which tests if a number is an integer:
我正在尝试实现用户定义的函数,该函数测试一个数字是否为整数:
#include <iostream>
#include <typeinfo>
using namespace std;
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);
else 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;
return 0;
}
So the idea is that,if a number is an integer, does not matter if it is a 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?
所以这个想法是,如果一个数字是一个整数,它是负数还是正数都没有关系,如果我们将它减少或增加一,我们必须得到零,但问题是,我们如何创建上和增加和减少的下限?
回答by tenfour
#include <cmath>
bool is_integer(float k)
{
return std::floor(k) == k;
}
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 ==
.
此解决方案应该适用于 的所有可能值k
。我很确定在这种情况下,您可以安全地使用==
.
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.
尝试仔细地命名函数。integer
没有给出它实际做什么的任何线索,所以我将函数名称更改为更有意义的名称。
For the future, testing if a number is integer should feellike 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).
对于未来,测试如果一个数是整数,应该感觉像一个非常简单的操作,所以你应该有一个强烈的感觉,最好的解决办法将是非常简单的。我希望你意识到你原来的解决方案是荒谬的,原因有很多(最大的原因:在绝大多数情况下它会导致堆栈溢出)。
回答by Paul R
Why not just do something like this:
为什么不做这样的事情:
bool integer(float k)
{
return k == (float)(int)k;
}
?
?
(Feel free to use proper C++ type casts of course.)
(当然可以随意使用适当的 C++ 类型转换。)
回答by quant_dev
This is not going to work, as for sufficiently large floats, x-1 == x
.
这不会起作用,至于足够大的浮点数,x-1 == x
.
You should test the bit pattern of the float to check whether the fractional part is 0.
您应该测试浮点数的位模式以检查小数部分是否为 0。
回答by Aman Agarwal
its in limit.h macro set to INT_MAX (for maximum) or INT_MIN (for minimum ) for the integers
它的 limit.h 宏设置为整数的 INT_MAX(最大值)或 INT_MIN(最小值)
correct answer
正确答案
bool integer(float k)
{
if( k == (int) k) return true;
return false;
}
回答by Gautham
回答by Anirudh Prabhakaran
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.
我想到了一个更简单的方法。
考虑一个浮点数,比如 1.5。这个数字的下限(即1)和这个数字的上限(即2)是不同的。对于任何包含小数部分的值都是如此。
另一方面,整数具有相同的下限和上限值。因此,检查数字的 ceil 和 floor 值会很容易,因此,看看它是否是一个整数。
#include <cmath>
bool is_integer(float n){
int c = ceil(n);
int f = floor(n);
if(f==c){
return true;
} else {
return false;
}
}
回答by Benedikt Wutzi
You can just use the boost lexical cast header
你可以只使用 boost lexical cast header
bool isinteger(float k){
try{
int tmp = boost::lexical_cast<int>(k);
(void*) tmp;
return true;
}catch(boost::bad_lexical_cast &c){
return false;
}
回答by user5864689
Well, why not just this??
好吧,为什么不只是这个??
#include <iostream>
using namespace std;
bool is_integer(float check){
if (int(check) == check)
return true;
else return false;
}
int main()
{
float input;
cin >> input;
if (is_integer(input))
cout << endl << "It's an integer";
else cout << endl <<" Not an integer";
return 0;
}