Test integerness

From Rosetta Code
Revision as of 08:31, 21 June 2014 by rosettacode>Dkf (→‎Tcl: Added implementation)
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