26
\$\begingroup\$

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ @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.) \$\endgroup\$ – MD XF Nov 18 '17 at 3:28
  • \$\begingroup\$ May we take input as two numbers, representing a fraction? \$\endgroup\$ – LyricLy Nov 18 '17 at 3:32
  • \$\begingroup\$ @LyricLy No, that would either be much easier for some languages or unnecessary for others. \$\endgroup\$ – MD XF Nov 18 '17 at 3:35
  • \$\begingroup\$ 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. \$\endgroup\$ – Thomas Ward Nov 18 '17 at 3:56
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – ilkkachu Nov 20 '17 at 9:02

81 Answers 81

2
\$\begingroup\$

Retina, 12 10 bytes

Match the format of a non-negative whole number

^\d+\.?0*$

Try it online

-2 thanks to FryAmTheEggman

\$\endgroup\$
2
\$\begingroup\$

Jelly, 3 bytes

ḞA=

Try it online!

The algorithm is the same as the one used in the Mathematica answer above.

\$\endgroup\$
  • \$\begingroup\$ No, it's not a bug that you can't explicitly state a 0 integer part, it's a feature, and specifically a syntax rule that dictates that a leading 0 is a 0 on its own, so for example 053 is the same as 0 53 and 0.5 is the same as 0 .5. \$\endgroup\$ – Erik the Outgolfer Nov 18 '17 at 9:42
  • \$\begingroup\$ @EriktheOutgolfer Nice. But when will you need 2 consecutive nilad? \$\endgroup\$ – user202729 Nov 18 '17 at 9:52
  • \$\begingroup\$ A couple of cases would be a dyad-nilad pair followed by a nilad-dyad pair and a quick which takes multiple nilads in a row (specifically ƭ = tie), and a third case would be something like a trailing (dyad)(nilad)(nilad) or (monad)(nilad) or (dyad)(nilad)(nilad)(monad) or even (monad)(nilad)(monad) etc. \$\endgroup\$ – Erik the Outgolfer Nov 18 '17 at 10:00
  • \$\begingroup\$ @EriktheOutgolfer Apparently ƭ is one of the quicks that does not have syntax specification... / What does those trailing do? \$\endgroup\$ – user202729 Nov 18 '17 at 10:09
  • \$\begingroup\$ All the detail about how Jelly interprets the "input" (the code in your footer) is only necessary because of your test-suite implementation (using Jelly code to give the numbers) if you use an argument it is evaluated as Python code - see this for example. \$\endgroup\$ – Jonathan Allan Nov 18 '17 at 13:54
2
\$\begingroup\$

Ruby, 17 bytes

->n{n>=0&&n%1==0}

Try it online!

It's not great, but I don't think it can get smaller.

\$\endgroup\$
2
\$\begingroup\$

R, 30 22 bytes

a=scan();`if`(a%%1==0&a>0,T,F)
\$\endgroup\$
  • \$\begingroup\$ It's a pity that we can't take strings as input, because grepl("^\\d+.?0*$",scan(,"")) is 29 bytes. Ah well, +1. \$\endgroup\$ – Giuseppe Nov 20 '17 at 19:25
2
\$\begingroup\$

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 :(
÷            read float
 ±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
\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ Any number with a decimal separator is a float. \$\endgroup\$ – Fatalize Mar 7 at 7:48
2
\$\begingroup\$

><>, 10 bytes

:1%$0(+0=n

Try it online!

Explanation

Calculates (n%1 + n<0) == 0

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Recursiva, 4 bytes

=aIa

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Python 3, 59 57 48 bytes

print("1")if float(input())%1==0else print("0")

Try it

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – caird coinheringaahing Nov 18 '17 at 12:46
  • 1
    \$\begingroup\$ Fails for negative integers (the requirement is to treat these as not-whole numbers). \$\endgroup\$ – Jonathan Allan Nov 18 '17 at 14:40
  • 3
    \$\begingroup\$ 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). \$\endgroup\$ – Jonathan Allan Nov 18 '17 at 14:46
  • 1
    \$\begingroup\$ Why not just print the boolean directly? print(float(input())%1==0) Although the negative integer problem is pretty major. \$\endgroup\$ – jpmc26 Nov 19 '17 at 7:58
1
\$\begingroup\$

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 : Try it online

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");
    }
}
\$\endgroup\$
  • \$\begingroup\$ Yeah, D has some weird casting rules for sure. \$\endgroup\$ – Zacharý Nov 19 '17 at 1:05
1
\$\begingroup\$

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[0] 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 :(

\$\endgroup\$
1
\$\begingroup\$

PHP, 28 bytes

First solution:

echo!(ceil($n=$argv[1])-$n);

Second solution:

echo floor($n=$argv[1])==$n;
\$\endgroup\$
  • \$\begingroup\$ gives false positive for every negative int \$\endgroup\$ – Titus Nov 19 '18 at 21:37
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.
?    Read another value. Irrelevant.
%    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.
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 4 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 (Â).

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

Scala, 25 chars

(f:Double)=>f.ceil.abs==f
\$\endgroup\$
  • \$\begingroup\$ Just to clarify, the type definition cannot be excluded \$\endgroup\$ – Taylor Scott Nov 22 '17 at 16:58
1
\$\begingroup\$

05AB1E, 3 bytes

ïÄQ

Try it online or verify all test cases.

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)
\$\endgroup\$
1
\$\begingroup\$

Scratch in scratchblocks2, 92 67 bytes

thanks lirtosiast

when gf clicked
ask[]and wait
say<([abs v]of(answer))=(round(answer

Try it online

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

PHP, 23+1 bytes

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

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

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 9 12 bytes

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

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

\$\endgroup\$
0
\$\begingroup\$

Ruby, 20 bytes

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

Ruby, 22 bytes

->x{[0,x].max==x.to_i}
\$\endgroup\$
  • 1
    \$\begingroup\$ Why is there a leading space in the second code snippet? \$\endgroup\$ – user72349 Nov 18 '17 at 15:41
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Lua, 38 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

REXX, 27 bytes

arg n
say trunc(n)=n & n>=0
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 17 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.