Test integerness: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added version 2 and 3.) |
(→{{header|REXX}}: the isInt subroutine (function) was enhanced to handle big (gihugeic) integers.) |
||
Line 257: | Line 257: | ||
This REXX version handles an exponent indicator of '''E''', '''D''', or '''Q''' (either lower or uppercase), and it also supports a trailing '''I''' or '''J''' imaginary indicator. |
This REXX version handles an exponent indicator of '''E''', '''D''', or '''Q''' (either lower or uppercase), and it also supports a trailing '''I''' or '''J''' imaginary indicator. |
||
This version also handles numbers larger than can be stored (within REXX) as simple integers within the limits of '''numeric digits'''. |
|||
<br>Thus, with '''numeric digits 100''', a number such as '''1e101''' wouldn't be considered an integer (within the rules of REXX). |
|||
<lang rexx>/*REXX pgm tests if a # (possibly complex) is equivalent to an integer.*/ |
<lang rexx>/*REXX pgm tests if a # (possibly complex) is equivalent to an integer.*/ |
||
numeric digits |
numeric digits 3000 /*be able to handle big integers.*/ |
||
parse arg #s /*get optional #s list from C.L. */ |
parse arg #s /*get optional #s list from C.L. */ |
||
if #s='' then #s= '3.14 1.00000 33 999999999 99999999999 1e272 AA 0' , |
if #s='' then #s= '3.14 1.00000 33 999999999 99999999999 1e272 AA 0' , |
||
Line 275: | Line 274: | ||
end /*j*/ /* [↑] process each # in list. */ |
end /*j*/ /* [↑] process each # in list. */ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*──────────────────────────────────ISINT subroutine────────────────────*/ |
|||
/*──────────────────────────────────one-liner subroutines───────────────*/ |
|||
isInt: |
isInt: procedure; parse arg n /*obtain the number in question. */ |
||
if datatype(n, 'Whole') then return 1 /*it's a simple integer (small). */ |
|||
if \datatype(n, 'Numn') then return 0 /*Not an integer if not a number.*/ |
|||
parse var n 'E' pow /*separate base from the 10's pow*/ |
|||
return pow>0 /*Exponent positive? It's an int*/ |
|||
/*──────────────────────────────────ISSIGN subroutine───────────────────*/ |
|||
isSign: arg ? 2; return ?=='+' |?=='-' /*concise method to test a sign. */ |
isSign: arg ? 2; return ?=='+' |?=='-' /*concise method to test a sign. */ |
||
/*──────────────────────────────────TIMAG subroutine────────────────────*/ |
/*──────────────────────────────────TIMAG subroutine────────────────────*/ |
||
Line 327: | Line 331: | ||
0003-00.0j is an integer. |
0003-00.0j is an integer. |
||
1.2d1 is an integer. |
1.2d1 is an integer. |
||
2e55666 is an integer. |
|||
+0003-00.0j is an integer. |
+0003-00.0j is an integer. |
||
+0j is an integer. |
+0j is an integer. |
||
Line 343: | Line 347: | ||
would be considered an integer (extra blanks were added to show the number with more clarity). |
would be considered an integer (extra blanks were added to show the number with more clarity). |
||
<lang rexx>/*REXX pgm tests if a # (possibly complex) is equivalent to an integer.*/ |
<lang rexx>/*REXX pgm tests if a # (possibly complex) is equivalent to an integer.*/ |
||
numeric digits |
numeric digits 3000 /*be able to handle big integers.*/ |
||
unaB='++ -- -+ +-' /*list of unary operators. */ |
unaB='++ -- -+ +-' /*list of unary operators. */ |
||
unaA='+ + - -' /*list of unary operators trans. */ |
unaA='+ + - -' /*list of unary operators trans. */ |
||
Line 366: | Line 370: | ||
end /*j*/ |
end /*j*/ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*──────────────────────────────────ISINT subroutine────────────────────*/ |
|||
/*──────────────────────────────────one-liner subroutines───────────────*/ |
|||
isInt: |
isInt: procedure; parse arg n /*obtain the number in question. */ |
||
if datatype(n, 'Whole') then return 1 /*it's a simple integer (small). */ |
|||
if \datatype(n, 'Numn') then return 0 /*Not an integer if not a number.*/ |
|||
parse var n 'E' pow /*separate base from the 10's pow*/ |
|||
return pow>0 /*Exponent positive? It's an int*/ |
|||
/*──────────────────────────────────ISSIGN subroutine───────────────────*/ |
|||
isSign: arg ? 2; return ?=='+' |?=='-' /*concise method to test a sign. */ |
isSign: arg ? 2; return ?=='+' |?=='-' /*concise method to test a sign. */ |
||
/*──────────────────────────────────TIMAG subroutine────────────────────*/ |
/*──────────────────────────────────TIMAG subroutine────────────────────*/ |