Test integerness

Revision as of 13:51, 21 June 2014 by Grondilu (talk | contribs) (Wikipedia link)

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

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.

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