# Is it a whole number?

A number is whole if it is a non-negative integer with no decimal part. So 0 and 8 and 233494.0 are whole, while 1.1 and 0.001 and 233494.999 are not.

## Input

A floating-point number in the default base/encoding of your language.

For example, the default integer representation for Binary Lambda Calculus would be Church numerals. But the default integer representation for Python is base 10 decimal, not Unary.

## Output

A truthy value if the input is whole, a falsy value if it is not.

Note that if your language only supports decimal precision to, say, 8 places, 1.000000002 can be considered whole.

Input and output may be done via any standard I/O methods.

# Test cases

Input        -> Output
332          -> true
33.2         -> false
128239847    -> true
0.128239847  -> false
0            -> true
0.000000000  -> true
1.111111111  -> false
-3.1415926   -> false
-3           -> false


# Scoring

As with , the shortest submission wins. Good luck!

• @StephenLeppik It's no easier than the vanilla parity challenge, or the Hello World, or the Truth Machine. (In fact, it's harder than most of those.) – MD XF Nov 18 '17 at 3:28
• May we take input as two numbers, representing a fraction? – LyricLy Nov 18 '17 at 3:32
• @LyricLy No, that would either be much easier for some languages or unnecessary for others. – MD XF Nov 18 '17 at 3:35
• Note that whole numbers are non-negative integers. Please update your first sentence to reflect this. And you can add in negative numbers test cases if you wish to show how whole numbers are not negative and falsy outputs for negative numbers are valid. – Thomas Ward Nov 18 '17 at 3:56
• @ThomasWard, that wikipedia article seems to not fully agree with you: "Texts that exclude zero from the natural numbers sometimes refer to the natural numbers together with zero as the whole numbers, but in other writings, that term is used instead for the integers (including negative integers)". I read the title as asking about integers. – ilkkachu Nov 20 '17 at 9:02

# Retina, 12 10 bytes

Match the format of a non-negative whole number

# Implicit (version predating challenge), 10 bytes

÷±1>?{;;ö}


Try it online! (Will only work after Dennis pulls TIO for the eighteenth time, bugs are stupid)

÷±1>?{;;ö}   no implicit input :(
±1          push -1
>         push (input > -1)
?{...}   if truthy
;      pop (input > -1)
;     pop -1
ö    push iswhole(input)
implicit integer output.
if (input > -1) was false, it prints (input > -1).
otherwise, it prints iswhole(input).


Posting this as an alternate solution since I had to update the builtin after realizing that whole numbers are >= 0, which feels cheaty.

# Implicit, 1 byte

ö


Do no other languages have this builtin?!

   implicit float input
ö  push truthy if whole, falsy otherwise
implicit int output


# Brachylog, 7 bytes

⌋₁ℕ.&⌉₁


Try it online!

At first, I expected to submit a one-byte answer ℕ, which is a predicate meant to be equivalent to an assertion that the input (which is the output variable as well) is a whole number. Turns out, it doesn't consider integer-valued floats to be integers (throwing a false negative on the 0.00000000 test case, as well as the 1.0 non-test-case), so I had to do this instead:

           The input
⌋₁         rounded down
ℕ        which is non-negative
.       is the output variable
&      and the input
⌉₁    rounded up
is also the output variable.

• Any number with a decimal separator is a float. – Fatalize Mar 7 '19 at 7:48

# ><>, 10 bytes

:1%$0(+0=n  Try it online! Explanation Calculates (n%1 + n<0) == 0 # TI-BASIC (TI-84), 3 bytes not(fPart(Ans  Example: 3.5 3.5 not(fPart(Ans 0 21 21 not(fPart(Ans 1  Built-in functions are amazing, no? Prints 0 (false) if Ans has a decimal part or 1 (true) if it doesn't. # Recursiva, 4 bytes =aIa  Try it online! # MY, 8 bytes ω≥ωω⌊=∧←  Try it online! The link uses arrays to test multiple numbers at once. ## How? • ω≥ = ω>=0 (0 is popped when stack is empty) • ωω⌊= = ω==floor(ω) • ∧← = take the logical and of the things above, then output. # Clojurescript, 12 bytes #(=(int %)%)  No TIO, this doesn't work in Clojure, only Clojurescript. You can try it here though: cljs.user=> (def f #(=(int %)%)) #'cljs.user/f cljs.user=> (f 2) true cljs.user=> (f 2.0) true cljs.user=> (f 2.5) false  # Python 3, 5957 48 bytes print("1")if float(input())%1==0else print("0")  Try it • Welcome to the site! You don't have to explicitly output true and false, instead you can output any two distinct values, such as 1 and 0, that are the same as true and false. Also, this answer can be golfed by removing unnecessary whitespace, and other such tricks found here – caird coinheringaahing Nov 18 '17 at 12:46 • Fails for negative integers (the requirement is to treat these as not-whole numbers). – Jonathan Allan Nov 18 '17 at 14:40 • Note that eval is shorter than float and will suffice here. Also you can put the if inside your print: print("1"if eval(input())%1==0else"0"). Furthermore you could use string indexing: print("01"[eval(input())%1==0]). But we don't need "1" and "0" at all: print(eval(input())%1==0). A lambda would then save more: lambda n:n%1==0 (not that any of this fixes the issue I noted above). – Jonathan Allan Nov 18 '17 at 14:46 • Why not just print the boolean directly? print(float(input())%1==0) Although the negative integer problem is pretty major. – jpmc26 Nov 19 '17 at 7:58 ## D : 43 bytes Just discovered that D has strange cast rules. Code : int w(float n){return n==cast(int)n&&n>=0;}  ### For testing D Code : void main() { float[] f = [ 332,33.2f,128239847,0.128239847f,0,0.0000f,1.1111111111f,-3.1415926f,-3 ]; for (int t = 0; t < f.length; ++t) { writeln(f[t]," = ", w(f[t])?"true":"false"); } }  • Yeah, D has some weird casting rules for sure. – Zacharý Nov 19 '17 at 1:05 # PowerShell, 24 bytes +"$args"-match'^[^-.]+$'  Try it online! Convert the argument to a string, then back to a number (actually unnecessary, could have also just used $args directly, but I thought of the latter first and it's the same byte count), then do a regex match, looking to see that the string has no - or . characters in its entirety.

Simple and boring :(

# PHP, 28 bytes

First solution:

echo!(ceil($n=$argv)-$n);  Second solution: echo floor($n=$argv)==$n;

• gives false positive for every negative int – Titus Nov 19 '18 at 21:37

# Pyth - 10 Bytes

&qQ.EQgQ0


Explanation :

              Implicitly Print
&              And
q           Are Equal
Q        Input
.E       Ceiling of
Q     Input
g           Greater Than or Equal to
Q        Input
0         0


## Hexagony, 6 bytes

?~<%@'


Try it online!

Output via exit code. 1 for whole numbers and 0 otherwise.

### Explanation

Unfolded:

 ? ~
< % @
' .


If we try to read two integers, then the second one will be zero for whole numbers.

?    Read whole number part, N.
~    Multiply by -1.
<    This branches depending on whether the value is positive or not, i.e.
whether N is negative or not.

If it's negative, the IP moves SE.

'    Move the memory pointer backwards, to the right.
%    Computes (0 % N). Irrelevant.
~    Multiply by -1. Irrelevant.
@    Terminate the program.

If N isn't negative, the IP moves NE from the <.

?    Read the fractional part, F.
'    Move the memory pointer backwards, to the right.
%    Computes (0 % F). This terminates the program with exit code 1 if
F is zero. Otherwise, it does nothing.
~    Multiply by -1. Irrelevant.
<    Reflect the IP to SW.
~    Multiply by -1. Irrelevant.
%    Computes (0 % F). Now irrelevant.
'    Move the IP back again.
@    Terminate  the program.


# Japt, 64 3 bytes

¶Âa


Try it

## Explanation

Checks for strict equality (¶) between the input and the absolute value (a) of the input with bitwise NOT applied twice (Â).

• Does it work for numbers > 2^31 ? – edc65 Nov 19 '17 at 0:51

# Scala, 25 chars

(f:Double)=>f.ceil.abs==f

• Just to clarify, the type definition cannot be excluded – Taylor Scott Nov 22 '17 at 16:58

# 05AB1E, 3 bytes

ïÄQ


Explanation:

ï      # Trim all decimals of the (implicit) input
Ä     # Take its absolute value
Q    # Check if it's equal to the (implicit) input (and output implicitly)


## Scratch in scratchblocks2, 92 67 bytes

thanks lirtosiast

when gf clicked


Try it online

• Surely when gf clicked ask[]and wait say<([abs v]of(answer))=(round(answer is shorter? – lirtosiast Nov 19 '18 at 9:24
• Ah yes, somehow I forgot about that. – W. K. Nov 19 '18 at 9:27

# PHP, 23+1 bytes

<?=$argn==abs(0|$argn);


prints 1 for truthy; empty output for falsy. Run as pipe with -F.

# JavaScript, 9 12 bytes

n=>!n%1&n>=0


Didn't realize that I had to take in negatives. Pointed out by @kamoroso94.

# Ruby, 20 bytes

->x{x.div(1)==x.abs}


# Ruby, 22 bytes

->x{[0,x].max==x.to_i}

• Why is there a leading space in the second code snippet? – user72349 Nov 18 '17 at 15:41

# Jq 1.5, 15 bytes

.>=0and.==floor


Not much to explain:

(. >= 0) and     # non-negative and
(. == floor)     # value == (value truncated to integer)


Try it online!

# Enlist, 4 bytes

:1Ae


Try it online!

Direct port of my Jelly answer above. Because of some reasons HyperNeutrino didn't implement Floor or Ceiling in Enlist, I use :1 (integer division by 1) instead, which takes 2 bytes.

# Lua, 38 bytes

function f(n)print(n>=0 and n%1==0)end


Try it online!

## REXX, 27 bytes

arg n
say trunc(n)=n & n>=0


# Kotlin, 17 bytes

{it%1==.0&&it>=0}


Try it online!

# C + ecpp, 23 bytes

#def $x !fmod(x,1)&x>=0  Defines an operator $ that takes one right operand x and evaluates to 1 if x is whole and 0 if it is not.

Try it online!