Test integerness: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: Add Python.)
Line 15: Line 15:
<pre>False
<pre>False
True</pre>
True</pre>

=={{header|Python}}==
<lang python>>>> def isint(f):
return int(f) == f

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


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 12:09, 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>

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