2015-05-22 84 views
0

是否有预定义的函数来检查一个数是否是一个正整数?检查一个数是否是一个正整数

2.17 -> NO

3 -> YES

目前我在做这个:if number - int(number) = 0 then 'YES' else 'NO'

+0

你只是检查数字是整数,而不是如果它们是正数或负数 – NEOmen

+0

大约-3? – NEOmen

回答

4

我不认为这是为一个直接function,你会写逻辑表达式。

您可以使用下面的任何两个人,都采取相同的时间内,一个使用mod功能其他用途int功能

data _NULL_; 
x=236893953323.1235433; 
if mod(x,1) = 0 then /**If you want to check the number is "positive" integer or not then use if mod(x,1) = 0 and x > 0 **/ 
put "Integer"; 
else put "Not Integer"; 
run; 

OR

data _null_ ; 
x=236893953323.1235433; 
    if x=int(x) then 
put "Integer"; 
else put "Not Integer"; 
run ; 
+0

NEOmen;你的'mod'评论不能按预期工作。从'x'中删除小数点并运行它将返回“非整数”。我认为你必须使用'if mod(x,1)= 0 && x> 0 then'。 –

+0

更正:)! – NEOmen

0

我喜欢我的功能:

如果(圆(数,0) ==数字然后'是'否''

+0

因为您不会告诉我们您的语言,所以我使用'人类语言' –

+0

我认为线索是“sas”标签:) –

+2

您的人类语言不需要匹配的圆括号? :) – Joe

2

可以使用int()功能比较:

data _null_ ; 
    do x=-5,1.4, 1.0, 2 ; 
    if x=abs(int(x)) then put "match" x= ; 
    else put "not match" x= ; 
    end ; 
run ; 
+0

Bendy,我想用户也想知道这个标志是正面还是负面。通过使用“sign”功能,我已经改善了您的答案。 –

+0

感谢没有注意到积极的...我已经嵌套在'abs()' – Bendy

+0

这是非常整洁,如果用户如果正在寻找正整数,但你不能用它轻松地检查让说负的整数。 :) –

4

我建议以下改进柔韧的码。它检查整数以及符号是正数还是负数。我不知道有一个函数检查两者。

data _null_ ; 
    do x=-1.4, 1.0, 1.5, -2 ; 
    if x=int(x) and sign(x)=1 then put "Positive Integer: " x= ; 
    else put "NOT Positive Integer: " x= ; 
    end ; 
run ; 

结果:

NOT Positive Integer: x=-1.4 
Positive Integer: x=1 
NOT Positive Integer: x=1.5 
NOT Positive Integer: x=-2 
1

这里写你自己的实际功能的钙镁磷肥选项。您也可以在函数中使用其他任何解决方案。我喜欢添加'模糊'因素,因为这些是浮点数;所以如果你想让它成为一个模糊的整数(即0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1不会加起来为真实的1)否则使其为零。如果你不关心这一点,那就摆脱这个选择。

proc fcmp outlib=work.funcs.func; 
    function isInteger(numIn,fuzz); 
     isInt = (abs(numIn-int(numIn)) le fuzz) and numIn>0; 
     return(isInt); 
    endsub; 

quit; 

options cmplib=work.funcs; 
data have; 
    input x; 
    datalines; 
-1 
-2.17 
2.17 
3 
3.000000000000000000000000001 
;;;; 
run; 

data want; 
    set have; 
    isInt = isInteger(x,0); 
    isFuzzy = isInteger(x,1e-12); 
run; 
相关问题