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

13
\$\begingroup\$

APL (Dyalog), 3 bytes

⌊≡|

Try it online!

Note that f← has been prepended in the TIO link due to technical limitations, but it's not normally needed.

\$\endgroup\$
  • \$\begingroup\$ That's elegant! I don't know why I added unnecessary ceiling to my answer.... \$\endgroup\$ – Galen Ivanov Nov 18 '17 at 10:15
  • 4
    \$\begingroup\$ @GalenIvanov This is just floor(n) == abs(n). \$\endgroup\$ – Erik the Outgolfer Nov 18 '17 at 13:45
16
\$\begingroup\$

Haskell, 27 16 bytes

This function checks whether x is contained in the list of nonnegative integers that are not greater than x.

f x=elem x[0..x]

Try it online!

\$\endgroup\$
11
\$\begingroup\$

Wolfram Language (Mathematica), 17 15 14 bytes

Saved 1 byte thanks to Not a tree!

#>=0==#~Mod~1&

Try it online!

First Mathematica answer \o/

Mathematica, 15 bytes

Saved 2 bytes thanks to @user202729!

#==⌊Abs@#⌋&
\$\endgroup\$
8
\$\begingroup\$

Pyth, 4 bytes

qs.a

Test suite

Is the number equal (q) to the floor (s) of its absolute value (.a)?

\$\endgroup\$
6
\$\begingroup\$

Husk, 3 bytes

£ΘN

Try it online! The third test case times out in TIO, so I chopped off a couple of digits. I tried to run it locally, but killed it after a couple of minutes since it was using over 6GB of memory and my computer started to stutter. It should theoretically finish at some point...

Explanation

This corresponds to the challenge description pretty directly.

£ΘN  Implicit input: a number.
  N  The infinite list [1,2,3,4...
 Θ   Prepend 0: [0,1,2,3,4...
£    Is the input an element of this list?
\$\endgroup\$
  • \$\begingroup\$ What would λx → floor(x) == abs(x) look like in Husk? \$\endgroup\$ – Lynn Nov 18 '17 at 10:40
  • \$\begingroup\$ @Lynn That would be §=⌊a, so one byte longer. \$\endgroup\$ – Zgarb Nov 18 '17 at 11:14
6
\$\begingroup\$

Python 2, 18 bytes

lambda n:n%1==0<=n

Try it online!

\$\endgroup\$
5
\$\begingroup\$

///, 94 bytes, input hard-coded

/=/\/x://:/\/\///-0/-:.0/.:.|/|:-|/|:0=1=2=3=4=5=6=7=8=9=|x/|:||/true:x:/-:/.:/|/+:++/false/||

Try it online!

Input between the two terminating vertical lines (||)

Strips -00...0 and .00...0, converts all remaining digits to xs, then tests whether the remaining number still has xs after . or a - not followed by ..

Could save up to 7 bytes depending on what's counted as truthy and falsey since this language doesn't have native truthy/falsey values, currently is outputting true and false but could change to, for example, T and F for 87 bytes if that's allowed.

\$\endgroup\$
5
\$\begingroup\$

Octave, 15 bytes

@(x)any(x==0:x)

Try it online!

This one is based on the approach used in @flawr's Haskell answer.

While it brings the byte count down to 15, it is shamefully inefficient (no offence intended), creating a list of every integer from 0 to x and seeing if x is contained within.


Octave, 18 bytes

@(x)fix(x)==abs(x)

Try it online!

Takes input as a double precision number. Returns true if whole.

Checks if the input when rounded is equal to the magnitude of the input. This will only be the case when the number is positive and whole.


Octave, 18 bytes

@(x)~mod(x,+(x>0))

Try it online!

An alternate solution for 18 bytes. Sadly the mod function in Octave won't implicitly convert a bool to a double, so the +( ) is needed around the greater than comparison. Otherwise this solution would have been shorter.


Octave, 18 bytes

@(x)x>=0&~mod(x,1)

Try it online!

And another one... I can't seem to get lower than 18 bytes. All because of having to allow for 0 to be true with the >= instead of just >.

\$\endgroup\$
5
\$\begingroup\$

C++ (gcc), 33 bytes

int f(float x){return(uint)x==x;}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Steadybox Nov 25 '17 at 16:48
4
\$\begingroup\$

Aceto, 6 bytes

rfdi±=p
r grabs input
f converts it to a float
d and i duplicates it and converts it to an integer
± pushes the absolute value of it (b/c can't be negative)
= checks if they are equal
p prints out the result

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Pyth, 6 bytes

&gQZsI

Try it online!

\$\endgroup\$
4
\$\begingroup\$

QBIC, 17 bytes

?(:=int(a))*(a>0)

Explanation

?             PRINT
 (:=int(a))     if the input (: assigns the cmd line param to 'a') equals itself 
                cast to int, -1 else 0
 *              times
 (a>0)          if the input > 0, -1 else 0

If either check fails, this returns 0. If both are true, it returns -1 x -1 = 1

\$\endgroup\$
4
\$\begingroup\$

Python 2 and Python 3, 21 18 bytes

lambda n:n>=0==n%1

Try it online! (Py2)
Try it online! (Py3)

3 bytes saved thanks to Mr. XCoder.

\$\endgroup\$
4
\$\begingroup\$

C (gcc), 27 28 27 25 bytes

-2 thanks to PrincePolka

#define g(f)!fmod(f,f>=0)

Try it online!

Defines a macro "function" g that takes a parameter f (of any type). Then checks if casting f mod 1 is zero, and if f is non-negative.

\$\endgroup\$
  • \$\begingroup\$ I think you are required to include the size of the include directive in your byte count, since fmod is defined in math.h : see there \$\endgroup\$ – HatsuPointerKun Nov 18 '17 at 11:27
  • \$\begingroup\$ @HatsuPointerKun It works without it too \$\endgroup\$ – Mr. Xcoder Nov 18 '17 at 14:05
  • 2
    \$\begingroup\$ I don't think you need -lm on windows-based C distributions (works on my machine) \$\endgroup\$ – Conor O'Brien Nov 18 '17 at 17:35
  • \$\begingroup\$ 25 , #define g(f)!fmod(f,f>=0) \$\endgroup\$ – PrincePolka Nov 24 '17 at 18:07
4
\$\begingroup\$

C#, Java : 43 bytes

-1 byte thanks to Zacharý
-1 byte thanks to TheLetalCoder

int w(float n){return(n==(int)n&&n>=0)?1:0;}

C# has a special 33 bytes optimization that you can not do in java :

bool w(float n){return(int)n==n;}

For testing

C# code :

class Program {
    int w(float n){return(n==(int)n&&n>=0)?1:0;}

    static void Main(string[] args)
    {
        var p = new Program();
        float[] fTab = new float[]{
            332,33.2f,128239847,0.128239847f,0,0.0000f,1.1111111111f,-3.1415926f,-3
        };
        foreach (float f in fTab) {
            Console.WriteLine(string.Format("{0} = {1}", f, (p.w(f) != 0).ToString()));
        }
        Console.ReadKey();
    }
}

Java Code :

public class MainApp {
    int w(float n){return(n==(int)n&&n>=0)?1:0;}

    public static void main(String[]a) {
        MainApp m = new MainApp();
        float testArr[] = new float[]{
                332,33.2f,128239847,0.128239847f,0,0.0000f,1.1111111111f,-3.1415926f,-3
        };

        for (float v : testArr) {
            System.out.println(v + " = " + String.valueOf(m.w(v)!=0));
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I think you can remove a space between return and (. \$\endgroup\$ – Zacharý Nov 19 '17 at 1:00
  • \$\begingroup\$ return a bool and there's no need for the ternary. \$\endgroup\$ – TheLethalCoder Nov 20 '17 at 17:27
  • \$\begingroup\$ @TheLethalCoder bool does not exist in Java \$\endgroup\$ – HatsuPointerKun Nov 20 '17 at 18:21
  • \$\begingroup\$ I don't know Java/C# that well, but can you make it return(int)n==n? Or would that cast n==n to an int rather than just n? \$\endgroup\$ – Zacharý Nov 30 '17 at 16:31
  • \$\begingroup\$ @Zacharý It works in C#, because i can use the bool data type. In java, if i want to use a boolean data type, i have to type boolean. \$\endgroup\$ – HatsuPointerKun Dec 7 '17 at 11:14
4
\$\begingroup\$

Google Sheets, 10 Bytes

Anonymous worksheet function that tales input from cell A1 and outputs to the calling cell.

=A1=Int(A1
\$\endgroup\$
4
\$\begingroup\$

Prolog (SWI), 24 bytes

f(X):-X>=0,round(X)=:=X.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ly, 35 47 bytes

ir"-"=[0u;]pr[["0"=![psp>l"."=[ppr!u;]p<2$]pp]]

Try it online!

Ly has float support, but the only way to create a float currently is by performing division. There is no way to take a float as input, so I had to manually check the string instead.

Lost 13 bytes adding support for negative numbers.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 14

n=>!(n<0||n%1)
\$\endgroup\$
  • \$\begingroup\$ 13 bytes: n=>!(n-(n|0)) \$\endgroup\$ – Ismael Miguel Nov 18 '17 at 13:38
  • \$\begingroup\$ @IsmaelMiguel. It doesn't work for negative numbers. \$\endgroup\$ – user72349 Nov 18 '17 at 13:40
  • \$\begingroup\$ @ThePirateBay Which exact number? I tried with 5, 5.1, -5, -5.1, 0, 0.1 and -0.1. \$\endgroup\$ – Ismael Miguel Nov 18 '17 at 13:41
  • \$\begingroup\$ @IsmaelMiguel. (n=>!(n-(n|0)))(-3) returns true, but should return false. See the last test case. \$\endgroup\$ – user72349 Nov 18 '17 at 13:42
  • \$\begingroup\$ Oh, you're right on that :/ I mis-read the question :/ \$\endgroup\$ – Ismael Miguel Nov 18 '17 at 13:44
3
\$\begingroup\$

Perl 5, 11 +1(-p) bytes

$_=abs==int

The -l switch not counted because for tests display

try it online

\$\endgroup\$
3
\$\begingroup\$

Perl 6,  15  13 bytes

{.narrow~~UInt}

Test it

{.Int==$_>=0}

Test it

\$\endgroup\$
3
\$\begingroup\$

Java (OpenJDK 8), 14 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 17 15 bytes

_=>_<0?0:_==~~_

_=>_<0?0:_==(_|0)

Thanks to edc65 for catching my mistakes.

\$\endgroup\$
3
\$\begingroup\$

Unexpanded Sinclair ZX81, 20 bytes

 1 INPUT A
 2 PRINT ABS A=INT A

20 bytes because BASIC is tokenized.

Simply will output 1 (true) if number is positive and the value of the number entered equals its integer value. Outputs 0 either of those conditions are not met.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is this a tokenized language? If it is you should indicate that in your answer. \$\endgroup\$ – Taylor Scott Nov 22 '17 at 15:35
  • 1
    \$\begingroup\$ Sorry, yes it to tokenized, i.e., INPUT is 1 byte of RAMs, as is PRINT and even >= due to a ~quirk~ intended feature of Sinclair BASIC \$\endgroup\$ – Shaun Bebbers Nov 22 '17 at 15:37
  • 1
    \$\begingroup\$ Specifically, each line number is 5 bytes + 1 newline (\r\n equivalent in ZX81 speak), white spaces don't count as bytes as those are already included in the keywords usually due to the Sinclair 'one-key-press' entry system. On running, it will take more bytes as it will put the value of A onto the var stack; I think each numeric value is always 5 bytes of memory. \$\endgroup\$ – Shaun Bebbers Nov 22 '17 at 15:49
  • 1
    \$\begingroup\$ and ZX Spectrum too \$\endgroup\$ – edc65 Nov 23 '17 at 15:06
  • 1
    \$\begingroup\$ As far as I know the tokenization mode is exactly the same (Spectrum has more codes). And any literal number is 6 bytes: a 'numeric tag' byte followed by the value encoded in a 5 bytes proprietary floating point format) \$\endgroup\$ – edc65 Nov 24 '17 at 8:29
3
\$\begingroup\$

C, C++ : 38 37 bytes

-1 byte thanks to Zacharý

-1 byte thanks to ceilingcat

int w(float n){return(int)n==n&n>=0;}

For Testing

C : Try it online

C Code :

#include <stdio.h>
int main() {
    float f[] = { 332,33.2f,128239847,0.128239847f,0,0.0000f,1.1111111111f,-3.1415926f,-3 };
    int t;
    for ( t = 0; t < 9; ++t) {
        printf("%f = %s\n", f[t], w(f[t])?"true":"false");
    }
    return 0;
}

C++ Code :

#include <iostream>
int main() {
    std::initializer_list <std::pair<float,bool>> test{
        {332.f,true}, {33.2f,false}, {128239847.f,true}, {0.128239847f,false}, {0.f,true}, {0.000000f,true}, {1.111111f,false}, {-3.1415926f,false}, {-3.f,false}
    };

    for (const auto& a : test) {
        if (w(a.first) != a.second) {
            std::cout << "Error with " << a.first << '\n';
        }
    }
    return 0;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This will only work up to INT_MAX. (I don't really care but some people do.) \$\endgroup\$ – MD XF Nov 18 '17 at 20:46
  • \$\begingroup\$ Would changing it to return(int)n==n... work? \$\endgroup\$ – Zacharý Nov 19 '17 at 1:01
  • \$\begingroup\$ You can shave one byte by using unsigned int instead of a signed one (as you then don't need the >=0 test): return(unsigned)n==n; And in C, you can omit the return type for a further 4 bytes. \$\endgroup\$ – Toby Speight Nov 22 '17 at 8:54
  • \$\begingroup\$ You can save 8 more bytes: w(float n){n=n==(int)n&&n>=0;} \$\endgroup\$ – Johan du Toit Dec 7 '17 at 14:29
2
\$\begingroup\$

J, 7 4 bytes

Removed the unnecessary celing check after the solution of Erik The Outgolfer

<.=|

Try it online!

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

Common Lisp, 36 32 bytes

(lambda(n)(=(max n 0)(floor n)))

Try it online!

Transposition of marmeladze’s answer.

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

Symbolic Python, 21 bytes

_=_%(_==_)==(_!=_)<=_

Try it online!

Uses a chained comparison:

  • _%(_==_) == (_!=_) checks if n%1 == 0, only true if n has no decimal part.
  • (_!=_) <= _ checks if 0 <= n, only true if the integer is non-negative.
\$\endgroup\$
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

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.