检查数字是否为正整数
check if a number is a positive integer
是否有用于检查数字是否为正整数的预定义函数?
例如
2.17 -> NO
3 -> YES
目前我正在这样做:if number - int(number) = 0 then 'YES' else 'NO'
我喜欢我的功能:
if (round(number,0) == number then 'Yes' else 'No'
可以使用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 ;
我认为没有直接的 function
,您必须编写逻辑表达式。
您可以使用以下两个中的任何一个,两者花费的时间相同,一个使用 mod
函数,另一个使用 int
function
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;
或
data _null_ ;
x=236893953323.1235433;
if x=int(x) then
put "Integer";
else put "Not Integer";
run ;
我建议使用以下改进的 Bendy 代码。它检查整数以及符号是正数还是负数。我不知道检查两者的功能。
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
这里有一个 FCMP 选项,可以编写您自己的实际功能。您也可以在函数中使用任何其他解决方案。我喜欢添加 'fuzz' 因子,因为这些是浮点数;所以如果你想让它成为一个模糊整数(即 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;
是否有用于检查数字是否为正整数的预定义函数?
例如
2.17 -> NO
3 -> YES
目前我正在这样做:if number - int(number) = 0 then 'YES' else 'NO'
我喜欢我的功能:
if (round(number,0) == number then 'Yes' else 'No'
可以使用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 ;
我认为没有直接的 function
,您必须编写逻辑表达式。
您可以使用以下两个中的任何一个,两者花费的时间相同,一个使用 mod
函数,另一个使用 int
function
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;
或
data _null_ ;
x=236893953323.1235433;
if x=int(x) then
put "Integer";
else put "Not Integer";
run ;
我建议使用以下改进的 Bendy 代码。它检查整数以及符号是正数还是负数。我不知道检查两者的功能。
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
这里有一个 FCMP 选项,可以编写您自己的实际功能。您也可以在函数中使用任何其他解决方案。我喜欢添加 'fuzz' 因子,因为这些是浮点数;所以如果你想让它成为一个模糊整数(即 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;