Test integerness: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|J}}: adding input and output)
(→‎Tcl: Added implementation)
Line 15: Line 15:
<pre>False
<pre>False
True</pre>
True</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
</pre>

Revision as of 08:31, 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

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