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

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!

# 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.

• That's elegant! I don't know why I added unnecessary ceiling to my answer.... Nov 18, 2017 at 10:15
• @GalenIvanov This is just floor(n) == abs(n). Nov 18, 2017 at 13:45

# Wolfram Language (Mathematica), 1715 14 bytes

Saved 1 byte thanks to Not a tree!

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


Try it online!

## Mathematica, 15 bytes

Saved 2 bytes thanks to @user202729!

#==⌊Abs@#⌋&


# Pyth, 4 bytes

qs.a


Test suite

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

# 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?

• What would λx → floor(x) == abs(x) look like in Husk?
– Lynn
Nov 18, 2017 at 10:40
• @Lynn That would be §=⌊a, so one byte longer. Nov 18, 2017 at 11:14

# Python 2, 18 bytes

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


Try it online!

# ///, 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.

# 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 >.

# C++ (gcc), 33 bytes

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


Try it online!

• Welcome to PPCG! Nov 25, 2017 at 16:48

# 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!

# Pyth, 6 bytes

&gQZsI


Try it online!

# 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

# Python 2 and Python 3, 21 18 bytes

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


3 bytes saved thanks to Mr. XCoder.

# C (gcc), 272827 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.

• 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 Nov 18, 2017 at 11:27
• @HatsuPointerKun It works without it too Nov 18, 2017 at 14:05
• I don't think you need -lm on windows-based C distributions (works on my machine) Nov 18, 2017 at 17:35
• 25 , #define g(f)!fmod(f,f>=0) Nov 24, 2017 at 18:07

## 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()));
}
}
}


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));
}
}
}

• I think you can remove a space between return and (. Nov 19, 2017 at 1:00
• return a bool and there's no need for the ternary. Nov 20, 2017 at 17:27
• @TheLethalCoder bool does not exist in Java Nov 20, 2017 at 18:21
• 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? Nov 30, 2017 at 16:31
• 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). Mar 20, 2018 at 12:35

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

=A1=Int(A1


# Prolog (SWI), 24 bytes

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


Try it online!

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. # JavaScript, 14 n=>!(n<0||n%1)  • 13 bytes: n=>!(n-(n|0)) Nov 18, 2017 at 13:38 • @IsmaelMiguel. It doesn't work for negative numbers. – user72349 Nov 18, 2017 at 13:40 • @ThePirateBay Which exact number? I tried with 5, 5.1, -5, -5.1, 0, 0.1 and -0.1. Nov 18, 2017 at 13:41 • @IsmaelMiguel. (n=>!(n-(n|0)))(-3) returns true, but should return false. See the last test case. – user72349 Nov 18, 2017 at 13:42 • Oh, you're right on that :/ I mis-read the question :/ Nov 18, 2017 at 13:44 # Perl 5, 11 +1(-p) bytes $_=abs==int


The -l switch not counted because for tests display

try it online

# Perl 6,  15  13 bytes

{.narrow~~UInt}


Test it

{.Int==\$_>=0}


Test it

# Java (OpenJDK 8), 14 bytes

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


Try it online!

# C#, 4037 29 bytes

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


Saved 8 bytes thanks to @Dennis_E!

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


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

• 30 bytes: bool z(float x)=>x%1==0&&x>=0; and you can use a single & for 29 bytes Nov 20, 2017 at 9:22
• @Dennis_E Thanks! Nov 20, 2017 at 15:54
• x=>x%1==0&x>=0 is shorter and compile to Func<float, bool> Nov 20, 2017 at 17:26

# JavaScript, 17 15 bytes

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


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


Thanks to edc65 for catching my mistakes.

# 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.

• Is this a tokenized language? If it is you should indicate that in your answer. Nov 22, 2017 at 15:35
• 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 Nov 22, 2017 at 15:37
• 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. Nov 22, 2017 at 15:49
• and ZX Spectrum too Nov 23, 2017 at 15:06
• 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) Nov 24, 2017 at 8:29

## 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 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;
}

• This will only work up to INT_MAX. (I don't really care but some people do.) Nov 18, 2017 at 20:46
• Would changing it to return(int)n==n... work? Nov 19, 2017 at 1:01
• 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. Nov 22, 2017 at 8:54
• You can save 8 more bytes: w(float n){n=n==(int)n&&n>=0;}
– jdt
Dec 7, 2017 at 14:29

# JavaScript (Node.js), 11 bytes

n=>n<0==n%1


Try it online!

# J, 7 4 bytes

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

<.=|


Try it online!

# Common Lisp, 36 32 bytes

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


Try it online!

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

• _%(_==_) == (_!=_) 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.