Test integerness: Difference between revisions

From Rosetta Code
Content added Content deleted
(add REXX)
(→‎{{header|Rexx}}: zero and negative wholw numbers are integers, right?)
Line 29: Line 29:
<lang rexx>/* REXX ---------------------------------------------------------------
<lang rexx>/* REXX ---------------------------------------------------------------
* 20.06.2014 Walter Pachl
* 20.06.2014 Walter Pachl
* zero and negative wholw numbers are integers, right?
*--------------------------------------------------------------------*/
*--------------------------------------------------------------------*/
Call test_integer 3.14
Call test_integer 3.14
Line 45: Line 46:
When datatype(x)<>'NUM' Then
When datatype(x)<>'NUM' Then
Say x 'is not an integer (not even a number)'
Say x 'is not an integer (not even a number)'
/***********************************************
When x=0 Then
When x=0 Then
Say x 'is zero and thus not an integer'
Say x 'is zero and thus not an integer'
When x<0 Then
When x<0 Then
Say x 'is negative and thus not an integer'
Say x 'is negative and thus not an integer'
***********************************************/
Otherwise Do
Otherwise Do
If datatype(x,'W') Then
If datatype(x,'W') Then
Line 65: Line 68:
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 zero and thus not an integer
0 is an integer
-3 is negative and thus not an integer /pre>
-3 is an integer/pre>





Revision as of 19:05, 21 June 2014

Test integerness is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a real numeric value, test whether or not it is an integer.

J

<lang J>(= <.) 3.14 7</lang>

Output:
0 1

Perl 6

<lang perl6>say pi.narrow ~~ Int; say 1e5.narrow ~~ Int;</lang>

Output:
False
True

Python

<lang python>>>> def isint(f):

   return int(f) == f

>>> isint(3.14) False >>> isint(3.0) True >>> </lang>

Rexx

<lang rexx>/* REXX ---------------------------------------------------------------

  • 20.06.2014 Walter Pachl
  • zero and negative wholw numbers are integers, right?
  • --------------------------------------------------------------------*/

Call test_integer 3.14 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 '-3' Exit test_integer: Parse Arg x Numeric Digits 1000 Select

 When datatype(x)<>'NUM' Then
   Say x 'is not an integer (not even a number)'
 /***********************************************
 When x=0 Then
   Say x 'is zero and thus not an integer'
 When x<0 Then
   Say x 'is negative and thus not an integer'
 ***********************************************/
 Otherwise Do
   If datatype(x,'W') Then
     Say x 'is an integer'
   Else
     Say x 'isnt an integer'
   End
 End

Return </lang> output

3.14 isn't 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
-3 is an integer/pre> 


=={{header|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>
{{out}}
<pre>
3.14: no
7: yes
1000000000000000000000: yes