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

83 Answers 83

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\$
0
14
\$\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\$
2
  • \$\begingroup\$ That's elegant! I don't know why I added unnecessary ceiling to my answer.... \$\endgroup\$ Nov 18, 2017 at 10:15
  • 4
    \$\begingroup\$ @GalenIvanov This is just floor(n) == abs(n). \$\endgroup\$ Nov 18, 2017 at 13:45
10
\$\begingroup\$

Pyth, 4 bytes

qs.a

Test suite

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

\$\endgroup\$
10
\$\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\$
0
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\$
2
  • \$\begingroup\$ What would λx → floor(x) == abs(x) look like in Husk? \$\endgroup\$
    – Lynn
    Nov 18, 2017 at 10:40
  • \$\begingroup\$ @Lynn That would be §=⌊a, so one byte longer. \$\endgroup\$
    – Zgarb
    Nov 18, 2017 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\$
0
5
\$\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\$
0
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\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Steadybox
    Nov 25, 2017 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\$
0
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\$

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\$
4
  • \$\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\$ Nov 18, 2017 at 11:27
  • \$\begingroup\$ @HatsuPointerKun It works without it too \$\endgroup\$
    – Mr. Xcoder
    Nov 18, 2017 at 14:05
  • 2
    \$\begingroup\$ I don't think you need -lm on windows-based C distributions (works on my machine) \$\endgroup\$ Nov 18, 2017 at 17:35
  • \$\begingroup\$ 25 , #define g(f)!fmod(f,f>=0) \$\endgroup\$ Nov 24, 2017 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\$
6
  • \$\begingroup\$ I think you can remove a space between return and (. \$\endgroup\$
    – Adalynn
    Nov 19, 2017 at 1:00
  • \$\begingroup\$ return a bool and there's no need for the ternary. \$\endgroup\$ Nov 20, 2017 at 17:27
  • \$\begingroup\$ @TheLethalCoder bool does not exist in Java \$\endgroup\$ Nov 20, 2017 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\$
    – Adalynn
    Nov 30, 2017 at 16:31
  • 1
    \$\begingroup\$ You can lower the Java 7 answer by 3 bytes by reversing the check and changing the boolean operator to a bitwise one: int w(float n){return(int)n!=n|n<0?0:1;} (40 bytes). As Java 8 lambda it's even shorter: n->(int)n==n&n>=0 (17 bytes), and same applies to the C# answer as lambda: n=>(int)n==n&n>=0 (also 17 bytes). \$\endgroup\$ Mar 20, 2018 at 12:35
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\$
0
3
\$\begingroup\$

JavaScript, 14

n=>!(n<0||n%1)
\$\endgroup\$
5
  • \$\begingroup\$ 13 bytes: n=>!(n-(n|0)) \$\endgroup\$ Nov 18, 2017 at 13:38
  • \$\begingroup\$ @IsmaelMiguel. It doesn't work for negative numbers. \$\endgroup\$
    – user72349
    Nov 18, 2017 at 13:40
  • \$\begingroup\$ @ThePirateBay Which exact number? I tried with 5, 5.1, -5, -5.1, 0, 0.1 and -0.1. \$\endgroup\$ Nov 18, 2017 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, 2017 at 13:42
  • \$\begingroup\$ Oh, you're right on that :/ I mis-read the question :/ \$\endgroup\$ Nov 18, 2017 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\$
0
3
\$\begingroup\$

Java (OpenJDK 8), 14 bytes

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

Try it online!

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

C#, 40 37 29 bytes

bool z(float x)=>x%1==0&x>=0;

Saved 8 bytes thanks to @Dennis_E!

Old Answer (37 bytes)

bool z(float x){return x%1==0&&x>=0;}

Old old answer (40 bytes):

bool z(float x){return (int)x==x&&x>=0;}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 30 bytes: bool z(float x)=>x%1==0&&x>=0; and you can use a single & for 29 bytes \$\endgroup\$
    – Dennis_E
    Nov 20, 2017 at 9:22
  • \$\begingroup\$ x=>x%1==0&x>=0 is shorter and compile to Func<float, bool> \$\endgroup\$ Nov 20, 2017 at 17:26
3
\$\begingroup\$

JavaScript, 17 15 bytes

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

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

Thanks to edc65 for catching my mistakes.

\$\endgroup\$
0
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\$
7
  • 1
    \$\begingroup\$ Is this a tokenized language? If it is you should indicate that in your answer. \$\endgroup\$ Nov 22, 2017 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\$ Nov 22, 2017 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\$ Nov 22, 2017 at 15:49
  • 1
    \$\begingroup\$ and ZX Spectrum too \$\endgroup\$
    – edc65
    Nov 23, 2017 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, 2017 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\$
4
  • 1
    \$\begingroup\$ This will only work up to INT_MAX. (I don't really care but some people do.) \$\endgroup\$
    – MD XF
    Nov 18, 2017 at 20:46
  • \$\begingroup\$ Would changing it to return(int)n==n... work? \$\endgroup\$
    – Adalynn
    Nov 19, 2017 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\$ Nov 22, 2017 at 8:54
  • \$\begingroup\$ You can save 8 more bytes: w(float n){n=n==(int)n&&n>=0;} \$\endgroup\$
    – jdt
    Dec 7, 2017 at 14:29
3
\$\begingroup\$

JavaScript (Node.js), 11 bytes

n=>n<0==n%1

Try it online!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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