Test integerness: Difference between revisions
(→{{header|J}}: remove specious {{{incomplete}}} tag; add alternative approach; format entry) |
Walterpachl (talk | contribs) (→{{header|REXX}}: add complex numbers) |
||
Line 31: | Line 31: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{incomplete}} |
|||
<lang rexx>/* REXX --------------------------------------------------------------- |
<lang rexx>/* REXX --------------------------------------------------------------- |
||
* 20.06.2014 Walter Pachl |
* 20.06.2014 Walter Pachl |
||
* |
* 22.06.2014 WP add complex numbers such as 13-12j etc. |
||
* (using 13e-12 or so is not (yet) supported) |
|||
*--------------------------------------------------------------------*/ |
*--------------------------------------------------------------------*/ |
||
Call test_integer 3.14 |
Call test_integer 3.14 |
||
Line 44: | Line 44: | ||
Call test_integer 'AA' |
Call test_integer 'AA' |
||
Call test_integer '0' |
Call test_integer '0' |
||
Call test_integer '- |
Call test_integer '1.000-3i' |
||
Call test_integer '1.000-3.3i' |
|||
Call test_integer '4j' |
|||
Call test_integer '2.00000000+0j' |
|||
Call test_integer '0j' |
|||
Call test_integer '333' |
|||
Call test_integer '-1-i' |
|||
Call test_integer '1+i' |
|||
Call test_integer '.00i' |
|||
Call test_integer 'j' |
|||
Call test_integer '0003-00.0j' |
|||
Exit |
Exit |
||
test_integer: |
test_integer: |
||
Parse Arg |
Parse Arg xx |
||
Numeric Digits 1000 |
Numeric Digits 1000 |
||
Parse Value parse_number(xx) With x imag |
|||
If imag<>0 Then Do |
|||
Say left(xx,13) 'is not an integer (imaginary part is not zero)' |
|||
Return |
|||
End |
|||
Select |
Select |
||
When datatype(x)<>'NUM' Then |
When datatype(x)<>'NUM' Then |
||
Say |
Say left(xx,13) 'is not an integer (not even a number)' |
||
/*********************************************** |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
***********************************************/ |
|||
Otherwise Do |
Otherwise Do |
||
If datatype(x,'W') Then |
If datatype(x,'W') Then |
||
Say |
Say left(xx,13) 'is an integer' |
||
Else |
Else |
||
Say |
Say left(xx,13) 'isn''t an integer' |
||
End |
End |
||
End |
End |
||
Return |
Return |
||
parse_number: Procedure |
|||
</lang> |
|||
Parse Upper Arg x |
|||
x=translate(x,'I','J') |
|||
If pos('I',x)>0 Then Do |
|||
pi=verify(x,'+-','M') |
|||
Select |
|||
⚫ | |||
real=left(x,pi-1) |
|||
imag=substr(x,pi) |
|||
End |
|||
⚫ | |||
real=0 |
|||
imag=x |
|||
End |
|||
Otherwise /*pi=1*/Do |
|||
p2=verify(substr(x,2),'+-','M') |
|||
If p2>0 Then Do |
|||
real=left(x,p2) |
|||
imag=substr(x,p2+1) |
|||
End |
|||
Else Do |
|||
real=0 |
|||
imag=x |
|||
End |
|||
End |
|||
End |
|||
End |
|||
Else Do |
|||
real=x |
|||
imag='0I' |
|||
End |
|||
pi=verify(imag,'+-','M') |
|||
If pi=0 Then Do |
|||
Parse Var imag imag_v 'I' |
|||
imag_sign='+' |
|||
End |
|||
Else |
|||
Parse Var imag imag_sign 2 imag_v 'I' |
|||
If imag_v='' Then |
|||
imag_v=1 |
|||
imag=imag_sign||imag_v |
|||
Return real imag</lang> |
|||
'''output''' |
'''output''' |
||
<pre>3.14 isn't an integer |
<pre>3.14 isn't an integer |
||
1.00000 is an integer |
|||
33 is an integer |
|||
999999999 is an integer |
999999999 is an integer |
||
99999999999 is an integer |
99999999999 is an integer |
||
1E272 is an integer |
1E272 is an integer |
||
AA is not an integer (not even a number) |
AA is not an integer (not even a number) |
||
0 is an integer |
0 is an integer |
||
- |
1.000-3i is not an integer (imaginary part is not zero) |
||
1.000-3.3i is not an integer (imaginary part is not zero) |
|||
4j is not an integer (imaginary part is not zero) |
|||
2.00000000+0j is an integer |
|||
⚫ | |||
⚫ | |||
-1-i is not an integer (imaginary part is not zero) |
|||
1+i is not an integer (imaginary part is not zero) |
|||
.00i is an integer |
|||
j is not an integer (imaginary part is not zero) |
|||
0003-00.0j is an integer</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 19:57, 22 June 2014
Given a numeric, possibly complex value, test whether or not it is an integer.
To be clear, we're not talking about whether the number is stored with the specific data type for integers, but instead we want to test whether there exists an integer with the exact same value. In other words, we want to test for integerness in the mathematical sense, not as a data type.
J
Solution:<lang j> isInt =: = <.</lang> Alternative solution (remainder after diving by 1?): <lang j> isInt=: 0 = 1&|</lang> Example:<lang j> isInt 3.14 7 0 1</lang>
Perl 6
<lang perl6>for pi, 1e5, 1+0i {
say .narrow ~~ Int;
}</lang>
- Output:
False True True
Python
<lang python>>>> def isint(f):
return complex(f).imag == 0 and complex(f).real.is_integer()
>>> [isint(f) for f in (1.0, 2, (3.0+0.0j), 4.1, (3+4j), (5.6+0j))] [True, True, True, False, False, False] >>> </lang>
REXX
<lang rexx>/* REXX ---------------------------------------------------------------
- 20.06.2014 Walter Pachl
- 22.06.2014 WP add complex numbers such as 13-12j etc.
- (using 13e-12 or so is not (yet) supported)
- --------------------------------------------------------------------*/
Call test_integer 3.14 Call test_integer 1.00000 Call test_integer 33 Call test_integer 999999999 Call test_integer 99999999999 Call test_integer 1e272 Call test_integer 'AA' Call test_integer '0' Call test_integer '1.000-3i' Call test_integer '1.000-3.3i' Call test_integer '4j' Call test_integer '2.00000000+0j' Call test_integer '0j' Call test_integer '333' Call test_integer '-1-i' Call test_integer '1+i' Call test_integer '.00i' Call test_integer 'j' Call test_integer '0003-00.0j' Exit
test_integer: Parse Arg xx Numeric Digits 1000 Parse Value parse_number(xx) With x imag If imag<>0 Then Do
Say left(xx,13) 'is not an integer (imaginary part is not zero)' Return End
Select
When datatype(x)<>'NUM' Then Say left(xx,13) 'is not an integer (not even a number)' Otherwise Do If datatype(x,'W') Then Say left(xx,13) 'is an integer' Else Say left(xx,13) 'isnt an integer' End End
Return parse_number: Procedure
Parse Upper Arg x x=translate(x,'I','J') If pos('I',x)>0 Then Do pi=verify(x,'+-','M') Select When pi>1 Then Do real=left(x,pi-1) imag=substr(x,pi) End When pi=0 Then Do real=0 imag=x End Otherwise /*pi=1*/Do p2=verify(substr(x,2),'+-','M') If p2>0 Then Do real=left(x,p2) imag=substr(x,p2+1) End Else Do real=0 imag=x End End End End Else Do real=x imag='0I' End pi=verify(imag,'+-','M') If pi=0 Then Do Parse Var imag imag_v 'I' imag_sign='+' End Else Parse Var imag imag_sign 2 imag_v 'I' If imag_v= Then imag_v=1 imag=imag_sign||imag_v
Return real imag</lang>
output
3.14 isn't an integer 1.00000 is an integer 33 is an integer 999999999 is an integer 99999999999 is an integer 1E272 is an integer AA is not an integer (not even a number) 0 is an integer 1.000-3i is not an integer (imaginary part is not zero) 1.000-3.3i is not an integer (imaginary part is not zero) 4j is not an integer (imaginary part is not zero) 2.00000000+0j is an integer 0j is an integer 333 is an integer -1-i is not an integer (imaginary part is not zero) 1+i is not an integer (imaginary part is not zero) .00i is an integer j is not an integer (imaginary part is not zero) 0003-00.0j is an integer
Tcl
The simplest method of doing this is testing whether the value is equal to the value after casting it to a integral value. <lang tcl>proc isNumberIntegral {x} {
expr {$x == entier($x)}
} foreach x {3.14 7 1000000000000000000000} {
puts [format "%s: %s" $x [expr {[isNumberIntegral $x] ? "yes" : "no"}]]
}</lang>
- Output:
3.14: no 7: yes 1000000000000000000000: yes