Test integerness: Difference between revisions

From Rosetta Code
Content added Content deleted
(add ooRexx)
(add Perl solution)
Line 93: Line 93:
3+0i is an integer</pre>
3+0i is an integer</pre>



=={{header|Perl}}==

<lang perl6>use Math::Complex;

sub is_int {
my $number = shift;
if (ref $number eq 'Math::Complex') {
return 0 if $number->Im != 0;
$number = $number->Re;
}
return int($number) == $number;
}

for (5, 4.1, sqrt(2), sqrt(4), 1.1e10, 3.0-0.0*i, 4-3*i, 5.6+0*i) {
printf "%20s is%s an integer\n", $_, (is_int($_) ? "" : " NOT");
}</lang>

{{out}}
<pre>
5 is an integer
4.1 is NOT an integer
1.4142135623731 is NOT an integer
2 is an integer
11000000000 is an integer
3 is an integer
4-3i is NOT an integer
5.6 is NOT an integer
</pre>


=={{header|Perl 6}}==
=={{header|Perl 6}}==

Revision as of 22:34, 24 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 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>

ooRexx

<lang oorexx>/* REXX ---------------------------------------------------------------

  • 22.06.2014 Walter Pachl using a complex data class
  • ooRexx Distribution contains an elaborate complex class
  • parts of which are used here
  • --------------------------------------------------------------------*/

Numeric Digits 1000 Call test_integer .complex~new(1e+12,0e-3) Call test_integer .complex~new(3.14) Call test_integer .complex~new(1.00000) Call test_integer .complex~new(33) Call test_integer .complex~new(999999999) Call test_integer .complex~new(99999999999) Call test_integer .complex~new(1e272) Call test_integer .complex~new(0) Call test_integer .complex~new(1.000,-3) Call test_integer .complex~new(1.000,-3.3) Call test_integer .complex~new(,4) Call test_integer .complex~new(2.00000000,+0) Call test_integer .complex~new(,0) Call test_integer .complex~new(333) Call test_integer .complex~new(-1,-1) Call test_integer .complex~new(1,1) Call test_integer .complex~new(,.00) Call test_integer .complex~new(,1) Call test_integer .complex~new(0003,00.0) Exit

test_integer: Use Arg cpx cpxa=left(changestr('+-',cpx,'-'),13) -- beautify representation Select

 When cpx~imaginary<>0 Then
   Say cpxa 'is not an integer'
 When datatype(cpx~real,'W') Then
   Say cpxa 'is an integer'
 Otherwise
   Say cpxa 'is not an integer'
 End

Return

class complex
method init /* initialize a complex number */

expose real imaginary /* expose the state data */ use Strict arg first=0, second=0 /* access the two numbers */ real = first + 0 /* force rounding */ imaginary = second + 0 /* force rounding on the second */

method real /* return real part of a complex */

expose real /* access the state information */ return real /* return that value */

method imaginary /* return imaginary part */

expose imaginary /* access the state information */ return imaginary /* return the value */

method string /* format as a string value */

expose real imaginary /* get the state info */ return real'+'imaginary'i' /* format as real+imaginaryi */</lang> output

1E+12+0i      is an integer
3.14+0i       is not an integer
1.00000+0i    is an integer
33+0i         is an integer
999999999+0i  is an integer
1.00000000E+1 is an integer
1E+272+0i     is an integer
0+0i          is an integer
1.000-3i      is not an integer
1.000-3.3i    is not an integer
0+4i          is not an integer
2.00000000+0i is an integer
0+0i          is an integer
333+0i        is an integer
-1-1i         is not an integer
1+1i          is not an integer
0+0i          is an integer
0+1i          is not an integer
3+0i          is an integer


Perl

<lang perl6>use Math::Complex;

sub is_int {

   my $number = shift;
   
   if (ref $number eq 'Math::Complex') {
       return 0 if $number->Im != 0;
       $number = $number->Re;
   }
   
   return int($number) == $number;

}

for (5, 4.1, sqrt(2), sqrt(4), 1.1e10, 3.0-0.0*i, 4-3*i, 5.6+0*i) {

   printf "%20s is%s an integer\n", $_, (is_int($_) ? "" : " NOT");

}</lang>

Output:
                   5 is an integer
                 4.1 is NOT an integer
     1.4142135623731 is NOT an integer
                   2 is an integer
         11000000000 is an integer
                   3 is an integer
                4-3i is NOT an integer
                 5.6 is NOT an integer

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

This example is incomplete. Please ensure that it meets all task requirements and remove this message.

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