Is it a whole number?

26

1

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!

MD XF

Posted 2017-11-18T03:09:18.727

Reputation: 11 605

2@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 – 2017-11-18T03:28:24.383

May we take input as two numbers, representing a fraction? – LyricLy – 2017-11-18T03:32:44.773

@LyricLy No, that would either be much easier for some languages or unnecessary for others. – MD XF – 2017-11-18T03:35:04.010

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 – 2017-11-18T03:56:09.497

"Output may be deterministic"? I suppose you meant "Output must be deterministic" – user202729 – 2017-11-18T04:10:16.337

"Non-negative integers" implies no decimal part, by the nature of what an integer is. Do you mean to specify "Non-negative numbers with no decimal part", or no? Yes I'm splitting hairs over grammar. – Thomas Ward – 2017-11-18T04:14:37.797

@ThomasWard I'm just making sure it's perfectly clear. – MD XF – 2017-11-18T04:15:06.837

@MDXF web browser caching, sorry. – Thomas Ward – 2017-11-18T04:26:27.850

Should -0 returns truthy? At least by now the Retina answer get it wrong. What about 1.0? – user202729 – 2017-11-18T05:08:24.327

@user202729 Unless you're doing assembly, -0 evaluates to its simplest form 0, and that's what should be inputted. 1.0 evaluates to its simplest form 1, and that's what should be inputted. – MD XF – 2017-11-18T05:20:53.360

Must we return a truthy value or can we have true/false inverted in our output? – Thomas Ward – 2017-11-18T05:38:21.233

Now the Ly answer cannot handle 1 but can handle 1.0. Is that valid? – user202729 – 2017-11-18T06:58:26.613

How do you represent non-integer numbers with church numerals? – Paŭlo Ebermann – 2017-11-18T09:26:58.983

Can we define what we consider truthy? (e.g. can 0 be true, and 1 be false). – Tom Carpenter – 2017-11-18T12:35:16.687

There are missing test cases like -1.0 ; 1.0 ; 500.000 ; -500.000 – sergiol – 2017-11-18T14:13:05.860

@TomCarpenter No, that's not allowed by concensus – MD XF – 2017-11-18T20:49:13.690

1@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 – 2017-11-20T09:02:35.837

@ilkkachu Whole Number != Natural Number if I recall my math right. they are separate concepts. – Thomas Ward – 2017-11-20T12:46:36.703

In a language like Befunge, where there aren't really different data types, can we take in numbers like so: 123.0000617? – MildlyMilquetoast – 2017-11-22T21:22:42.123

Sinclair ZX80 (4K ROM) has integer-only BASIC, so on that basis am I allowed an entry for this technology? i.e., any floating point number will produce an error (false), so I only need to check for numbers of zero or more. – Shaun Bebbers – 2017-11-28T16:54:08.837

Answers

13

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.

Erik the Outgolfer

Posted 2017-11-18T03:09:18.727

Reputation: 38 134

That's elegant! I don't know why I added unnecessary ceiling to my answer.... – Galen Ivanov – 2017-11-18T10:15:13.527

4@GalenIvanov This is just floor(n) == abs(n). – Erik the Outgolfer – 2017-11-18T13:45:49.687

16

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!

flawr

Posted 2017-11-18T03:09:18.727

Reputation: 40 560

11

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@#⌋&

Conor O'Brien

Posted 2017-11-18T03:09:18.727

Reputation: 36 228

8

Pyth, 4 bytes

qs.a

Test suite

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

isaacg

Posted 2017-11-18T03:09:18.727

Reputation: 39 268

6

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?

Zgarb

Posted 2017-11-18T03:09:18.727

Reputation: 39 083

What would λx → floor(x) == abs(x) look like in Husk? – Lynn – 2017-11-18T10:40:15.670

@Lynn That would be §=⌊a, so one byte longer. – Zgarb – 2017-11-18T11:14:12.660

6

Python 2, 18 bytes

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

Try it online!

ovs

Posted 2017-11-18T03:09:18.727

Reputation: 21 408

5

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

boboquack

Posted 2017-11-18T03:09:18.727

Reputation: 2 017

5

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

Tom Carpenter

Posted 2017-11-18T03:09:18.727

Reputation: 3 990

5

C++ (gcc), 33 bytes

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

Try it online!

HenrikS

Posted 2017-11-18T03:09:18.727

Reputation: 151

Welcome to PPCG! – Steadybox – 2017-11-25T16:48:59.553

4

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.

Thomas Ward

Posted 2017-11-18T03:09:18.727

Reputation: 193

4

Pyth, 6 bytes

&gQZsI

Try it online!

Mr. Xcoder

Posted 2017-11-18T03:09:18.727

Reputation: 39 774

4

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!

FantaC

Posted 2017-11-18T03:09:18.727

Reputation: 1 425

4

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.

MD XF

Posted 2017-11-18T03:09:18.727

Reputation: 11 605

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

– HatsuPointerKun – 2017-11-18T11:27:10.410

@HatsuPointerKun It works without it too

– Mr. Xcoder – 2017-11-18T14:05:17.623

2I don't think you need -lm on windows-based C distributions (works on my machine) – Conor O'Brien – 2017-11-18T17:35:00.890

25 , #define g(f)!fmod(f,f>=0) – PrincePolka – 2017-11-24T18:07:57.667

4

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

steenbergh

Posted 2017-11-18T03:09:18.727

Reputation: 7 772

4

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

HatsuPointerKun

Posted 2017-11-18T03:09:18.727

Reputation: 1 891

I think you can remove a space between return and (. – Zacharý – 2017-11-19T01:00:24.520

return a bool and there's no need for the ternary. – TheLethalCoder – 2017-11-20T17:27:53.247

@TheLethalCoder bool does not exist in Java – HatsuPointerKun – 2017-11-20T18:21:57.433

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? – Zacharý – 2017-11-30T16:31:08.870

@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. – HatsuPointerKun – 2017-12-07T11:14:50.033

1

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

– Kevin Cruijssen – 2018-03-20T12:35:16.240

4

Google Sheets, 10 Bytes

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

=A1=Int(A1

Taylor Scott

Posted 2017-11-18T03:09:18.727

Reputation: 6 709

4

Prolog (SWI), 24 bytes

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

Try it online!

qwertxzy

Posted 2017-11-18T03:09:18.727

Reputation: 101

3

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.

LyricLy

Posted 2017-11-18T03:09:18.727

Reputation: 3 313

3

JavaScript, 14

n=>!(n<0||n%1)

edc65

Posted 2017-11-18T03:09:18.727

Reputation: 31 086

13 bytes: n=>!(n-(n|0)) – Ismael Miguel – 2017-11-18T13:38:03.790

@IsmaelMiguel. It doesn't work for negative numbers. – None – 2017-11-18T13:40:48.547

@ThePirateBay Which exact number? I tried with 5, 5.1, -5, -5.1, 0, 0.1 and -0.1. – Ismael Miguel – 2017-11-18T13:41:48.877

@IsmaelMiguel. (n=>!(n-(n|0)))(-3) returns true, but should return false. See the last test case. – None – 2017-11-18T13:42:39.857

Oh, you're right on that :/ I mis-read the question :/ – Ismael Miguel – 2017-11-18T13:44:25.700

3

Perl 5, 11 +1(-p) bytes

$_=abs==int

The -l switch not counted because for tests display

try it online

Nahuel Fouilleul

Posted 2017-11-18T03:09:18.727

Reputation: 5 582

3

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

HatsuPointerKun

Posted 2017-11-18T03:09:18.727

Reputation: 1 891

1This will only work up to INT_MAX. (I don't really care but some people do.) – MD XF – 2017-11-18T20:46:05.823

Would changing it to return(int)n==n... work? – Zacharý – 2017-11-19T01:01:21.813

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. – Toby Speight – 2017-11-22T08:54:46.623

You can save 8 more bytes: w(float n){n=n==(int)n&&n>=0;} – Johan du Toit – 2017-12-07T14:29:21.953

3

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

yummypasta

Posted 2017-11-18T03:09:18.727

Reputation: 371

230 bytes: bool z(float x)=>x%1==0&&x>=0; and you can use a single & for 29 bytes – Dennis_E – 2017-11-20T09:22:06.160

@Dennis_E Thanks! – yummypasta – 2017-11-20T15:54:22.347

x=>x%1==0&x>=0 is shorter and compile to Func<float, bool> – TheLethalCoder – 2017-11-20T17:26:09.473

3

Perl 6,  15  13 bytes

{.narrow~~UInt}

Test it

{.Int==$_>=0}

Test it

Brad Gilbert b2gills

Posted 2017-11-18T03:09:18.727

Reputation: 12 713

3

Java (OpenJDK 8), 14 bytes

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

Try it online!

Olivier Grégoire

Posted 2017-11-18T03:09:18.727

Reputation: 10 647

3

JavaScript, 17 15 bytes

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

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

Thanks to edc65 for catching my mistakes.

ericw31415

Posted 2017-11-18T03:09:18.727

Reputation: 2 229

3

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.

Shaun Bebbers

Posted 2017-11-18T03:09:18.727

Reputation: 1 814

1Is this a tokenized language? If it is you should indicate that in your answer. – Taylor Scott – 2017-11-22T15:35:29.187

1Sorry, 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 – Shaun Bebbers – 2017-11-22T15:37:13.830

1Specifically, 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. – Shaun Bebbers – 2017-11-22T15:49:32.943

1and ZX Spectrum too – edc65 – 2017-11-23T15:06:52.020

I'm not sure how the tokenizer works on the Sinclair ZX Spectrum; I do know that I can save bytes by more typing on both machines, but then that confuses the CodeGolf issue somewhat. So if I used NOT PI instead of 0 then I save some bytes because NOT PI is 2 bytes whereas 0 is 5 bytes or something. – Shaun Bebbers – 2017-11-23T15:29:33.817

1As 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) – edc65 – 2017-11-24T08:29:08.883

@edc65 very good there - are you making some ZX softwares? – Shaun Bebbers – 2017-11-24T16:53:32.270

3

JavaScript (Node.js), 11 bytes

n=>n<0==n%1

Try it online!

l4m2

Posted 2017-11-18T03:09:18.727

Reputation: 5 985

2

Jelly, 3 bytes

ḞA=

Try it online!

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

user202729

Posted 2017-11-18T03:09:18.727

Reputation: 14 620

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. – Erik the Outgolfer – 2017-11-18T09:42:35.063

@EriktheOutgolfer Nice. But when will you need 2 consecutive nilad? – user202729 – 2017-11-18T09:52:05.767

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. – Erik the Outgolfer – 2017-11-18T10:00:10.937

@EriktheOutgolfer Apparently ƭ is one of the quicks that does not have syntax specification... / What does those trailing do? – user202729 – 2017-11-18T10:09:30.693

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.

– Jonathan Allan – 2017-11-18T13:54:20.110

2

Retina, 12 10 bytes

Match the format of a non-negative whole number

^\d+\.?0*$

Try it online

-2 thanks to FryAmTheEggman

mbomb007

Posted 2017-11-18T03:09:18.727

Reputation: 21 944

2

J, 7 4 bytes

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

<.=|

Try it online!

Galen Ivanov

Posted 2017-11-18T03:09:18.727

Reputation: 13 815

2

Common Lisp, 36 32 bytes

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

Try it online!

Transposition of marmeladze’s answer.

Renzo

Posted 2017-11-18T03:09:18.727

Reputation: 2 260

2

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.

FlipTack

Posted 2017-11-18T03:09:18.727

Reputation: 13 242

2

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

MD XF

Posted 2017-11-18T03:09:18.727

Reputation: 11 605

2

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.

Reinstate Monica -- notmaynard

Posted 2017-11-18T03:09:18.727

Reputation: 1 053

2

R, 30 22 bytes

a=scan();`if`(a%%1==0&a>0,T,F)

Andrew Haynes

Posted 2017-11-18T03:09:18.727

Reputation: 311

It's a pity that we can't take strings as input, because grepl("^\\d+.?0*$",scan(,"")) is 29 bytes. Ah well, +1. – Giuseppe – 2017-11-20T19:25:57.983

2

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.

Unrelated String

Posted 2017-11-18T03:09:18.727

Reputation: 5 300

1Any number with a decimal separator is a float. – Fatalize – 2019-03-07T07:48:37.183

2

><>, 10 bytes

:1%$0(+0=n

Try it online!

Explanation

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

Emigna

Posted 2017-11-18T03:09:18.727

Reputation: 50 798

2

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.

Tau

Posted 2017-11-18T03:09:18.727

Reputation: 1 935

1

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 (Â).

Shaggy

Posted 2017-11-18T03:09:18.727

Reputation: 24 623

Does it work for numbers > 2^31 ? – edc65 – 2017-11-19T00:51:40.650

1

Recursiva, 4 bytes

=aIa

Try it online!

officialaimm

Posted 2017-11-18T03:09:18.727

Reputation: 2 739

1

Python 3, 59 57 48 bytes

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

Try it

Ben

Posted 2017-11-18T03:09:18.727

Reputation: 47

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 – 2017-11-18T12:46:14.017

1

Fails for negative integers (the requirement is to treat these as not-whole numbers).

– Jonathan Allan – 2017-11-18T14:40:41.990

3Note 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 – 2017-11-18T14:46:36.903

1Why not just print the boolean directly? print(float(input())%1==0) Although the negative integer problem is pretty major. – jpmc26 – 2017-11-19T07:58:57.337

1

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

HatsuPointerKun

Posted 2017-11-18T03:09:18.727

Reputation: 1 891

Yeah, D has some weird casting rules for sure. – Zacharý – 2017-11-19T01:05:47.483

1

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.

Zacharý

Posted 2017-11-18T03:09:18.727

Reputation: 5 710

1

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

MattPutnam

Posted 2017-11-18T03:09:18.727

Reputation: 521

1

Scala, 25 chars

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

sprague44

Posted 2017-11-18T03:09:18.727

Reputation: 81

Just to clarify, the type definition cannot be excluded – Taylor Scott – 2017-11-22T16:58:13.577

1

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 :(

briantist

Posted 2017-11-18T03:09:18.727

Reputation: 3 110

1

PHP, 28 bytes

First solution:

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

Second solution:

echo floor($n=$argv[1])==$n;

jstnthms

Posted 2017-11-18T03:09:18.727

Reputation: 181

gives false positive for every negative int – Titus – 2018-11-19T21:37:25.363

1

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.

Martin Ender

Posted 2017-11-18T03:09:18.727

Reputation: 184 808

1

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

Tornado547

Posted 2017-11-18T03:09:18.727

Reputation: 389

1

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)

Kevin Cruijssen

Posted 2017-11-18T03:09:18.727

Reputation: 67 575

1

Scratch in scratchblocks2, 92 67 bytes

thanks lirtosiast

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

Try it online

W. K.

Posted 2017-11-18T03:09:18.727

Reputation: 51

1Surely when gf clicked ask[]and wait say<([abs v]of(answer))=(round(answer is shorter? – lirtosiast – 2018-11-19T09:24:28.420

Ah yes, somehow I forgot about that. – W. K. – 2018-11-19T09:27:38.777

1

PHP, 23+1 bytes

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

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

Titus

Posted 2017-11-18T03:09:18.727

Reputation: 13 814

0

JavaScript, 9 12 bytes

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

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

Nissa

Posted 2017-11-18T03:09:18.727

Reputation: 3 334

0

Ruby, 20 bytes

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

Ruby, 22 bytes

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

marmeladze

Posted 2017-11-18T03:09:18.727

Reputation: 227

1Why is there a leading space in the second code snippet? – None – 2017-11-18T15:41:10.980

0

C on Linux, 31 bytes

int f(float v){return!rintf(v);}

The correct prototype for rintf is float rintf(float). However, in IEEE floating-point, 0.0f has the binary representation of all zeros. We (mis)use this feature to get away without using a prototype; the zero/non-zero value is then inverted into 1 or 0.

CSM

Posted 2017-11-18T03:09:18.727

Reputation: 219

3or #define f(v)!rintf(v) – technosaurus – 2017-11-19T08:18:47.663

You should be able to use the implicit int typing if you drop the int. – Potato44 – 2017-11-20T04:55:34.523

@technosaurus - Why not accept double and save a byte? #define f(v)!rint(v) – Toby Speight – 2017-11-20T14:50:12.537

Actually, I don't think this answer is correct at all - should it be v==rint(v)? Or perhaps !fmod(v,1) – Toby Speight – 2017-11-20T14:52:40.013

@TobySpeight, it would have to be !fmodf(v,1.0) for the return to be a float (int-sized), and 1.0 to force the parameter to a float, both needed because there is no prototype. – CSM – 2017-11-21T21:53:05.360

I missed the bit where you said it only works on platforms where int is the same size as float. I'll read more carefully in future! It's still the case that this returns true for 0.5 and false for 2.0, contrary to requirements. – Toby Speight – 2017-11-22T08:46:33.807

0

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!

jq170727

Posted 2017-11-18T03:09:18.727

Reputation: 411

0

D, 36 33 30 bytes

T f(T)(T n){return(n<0)==n%1;}

Try it online!

I will not be beaten in D!

Zacharý

Posted 2017-11-18T03:09:18.727

Reputation: 5 710

0

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.

user202729

Posted 2017-11-18T03:09:18.727

Reputation: 14 620

0

Lua, 38 bytes

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

Try it online!

MCAdventure10

Posted 2017-11-18T03:09:18.727

Reputation: 103

0

REXX, 27 bytes

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

idrougge

Posted 2017-11-18T03:09:18.727

Reputation: 641

0

Kotlin, 17 bytes

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

Try it online!

ovs

Posted 2017-11-18T03:09:18.727

Reputation: 21 408

0

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!

MD XF

Posted 2017-11-18T03:09:18.727

Reputation: 11 605

0

Pyth, 4 bytes

Not a difficult question, but I thought I’d give it a try. Also did not think that zero was whole, but it saves a byte.

qs.a

Explanation:

 s.a      Integer of the absolute value of the implicit input
q         Is it the same as the original implicit input?

Test Suite

Version that doesn’t find 0 as a whole number:

&qs.a

Ands the original answer with the input, so 0 falsifies the answer.

Test Suite

Returns a falsy or truthy value that isn’t always a Boolean.

Stan Strum

Posted 2017-11-18T03:09:18.727

Reputation: 436

0

Julia, 13 bytes

f(x)=x in 0:x

Try it online!

EricShermanCS

Posted 2017-11-18T03:09:18.727

Reputation: 121

0

ARBLE, 9 bytes

eq(a%1,0)

Try it online!

ATaco

Posted 2017-11-18T03:09:18.727

Reputation: 7 898

0

Pyt, 3 4 bytes

Đ⎶Å≡
    implicit input
Đ   duplicates top value
⎶   pushes closest int to top value
Å   abs(TOS)
≡   checks if equal
    implicit output

Try it online!

FantaC

Posted 2017-11-18T03:09:18.727

Reputation: 1 425

0

Taxi, 553 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 5 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Equal's Corner.Go to Trunkers:s 1 l.Pickup a passenger going to Equal's Corner.Go to Equal's Corner:w 1 l.Switch to plan "b" if no one is waiting.'1' is waiting at Writer's Depot.[b]'0' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.

Try it online!

Ungolfed:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 5th left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Equal's Corner.
Go to Trunkers: south 1st left.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner: west 1st left.
Switch to plan "b" if no one is waiting.
'1' is waiting at Writer's Depot.
[b]
'0' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.

I'm surprised it took this long to post such a basic challenge. I like them. Taxi is... still not good for golfing.

Engineer Toast

Posted 2017-11-18T03:09:18.727

Reputation: 5 769

0

Jelly, 3 bytes

Ḟ⁼A

Try it online!

Uses Erik's strategy of floor(n) == abs(n).

ellie

Posted 2017-11-18T03:09:18.727

Reputation: 131

0

Jelly, 3 bytes

AḞ=

Try it online!

chromaticiT

Posted 2017-11-18T03:09:18.727

Reputation: 211

0

SNOBOL4 (CSNOBOL4), 73 bytes

	DEFINE('W(N)D')
W	N SPAN('1234567890') '.' REM . D
	W =EQ(D) 1	:(RETURN)

Try it online!

An anonymous function; returns 1 for truthy and an empty string for falsey.

Takes N the input and matches it against the regex \\d.(\\d*)$, setting (\\d*) as D. Then if D==0 (''==0 in SNOBOL), sets W to 1. If not, it doesn't set W, which defaults to the empty string, and returns W.

Giuseppe

Posted 2017-11-18T03:09:18.727

Reputation: 21 077

0

Javascript - 16 Bytes

a=>a>=0&&a%1<=0

f=a=>a>=0&&a%1<=0

// ----------- Test Cases -------------

console.log(332, f(332))
console.log(33.2, f(33.2))
console.log(128239847, f(128239847))
console.log(0.128239847, f(0.128239847))
console.log(0, f(0))
console.log(0.000000000, f(0.000000000))
console.log(1.111111111, f(1.111111111))
console.log(-3.1415926, f(-3.1415926))
console.log(-3, f(-3))

Luis felipe De jesus Munoz

Posted 2017-11-18T03:09:18.727

Reputation: 9 639

1Fails on 3 and -3.1415926 – Herman L – 2018-03-05T18:14:19.927

0

TI-BASIC, 5 bytes

Takes input and gives output through Ans.

Ans=abs(int(Ans

kamoroso94

Posted 2017-11-18T03:09:18.727

Reputation: 739

0

JavaScript (SpiderMonkey), 24 bytes

s=>s<0?0:s==Math.ceil(s)

Try it online!

A great thanks to @Jo King for converting the code from 29bytes to 24.

Sourav

Posted 2017-11-18T03:09:18.727

Reputation: 31

@JoKing thanks for pointing out. – Sourav – 2018-11-19T11:44:42.687

false can be 0 and I'm pretty sure === can be == – Jo King – 2018-11-19T12:00:25.583

@JoKing yes. that way I will reduce 1 byte. – Sourav – 2018-11-19T12:11:30.117

and four bytes for false to 0.. or if you're really insistent on booleans rather than falsey values, at least do 0>1 – Jo King – 2018-11-19T12:21:37.533

@JoKing the output requires false. If think that way then 0 is not significant option. – Sourav – 2018-11-19T12:24:48.750

No, if you look closer, it requires a falsey value, of which zero is an example

– Jo King – 2018-11-19T12:27:10.187

but the output will be differ from what he excepted.Is that ok in rules of codegolf. – Sourav – 2018-11-19T12:33:48.130

The test cases only show an example of input and output, not the exact expectations. The question asks for a truthy/falsey value, which could be anything like 1 0, true false, '' 'non-empty string' or any mixture of those – Jo King – 2018-11-19T12:45:51.840

0

Python 2, 59 60 57 bytes

lambda s:re.match(r"^\+?[0-9]+(\.0*)?$",s)>None
import re

Try it online!

Takes input as a string.

Regex is as follows:

  • starts with + or a number
  • has AT LEAST 1 digit
  • either:
    • has a period, but only zeroes follow the period
    • has a period, but nothing after the period
    • terminates without a period

Python 2's re.match objects are greater than None objects, so it will return True if the regex matched and False otherwise.

Edit: no longer has false positives on negative numbers (not sure what i was thinking...) and correctly handles trailing periods. Thanks, Deadcode!

I feel so bad about the greater than None check.

Triggernometry

Posted 2017-11-18T03:09:18.727

Reputation: 765

This is incorrect. The problem defines a whole number as a non-negative integer, so you need \+? (or nothing) not [+-]?. Also, shouldn't it be 0* not 0+? Ending with a decimal point is generally valid. – Deadcode – 2019-02-11T00:52:24.713

@Deadcode Well, crap. You're right. Thanks for pointing this out! – Triggernometry – 2019-02-12T15:37:01.757

0

MathGolf, 3 bytes

i±=

Try it online!

Explanation

Same as a bunch of other answers.

i     Read input as integer
 ±    Convert to absolute value
  =   Is it equal to the implicit input?

maxb

Posted 2017-11-18T03:09:18.727

Reputation: 5 754

0

Gol><>, 17 bytes

I:0(q0h:S(-zq1!0h

A simple solution, but I will be golfing this alot more

Try it online!

KrystosTheOverlord

Posted 2017-11-18T03:09:18.727

Reputation: 681

7 bytes, with error/0 as falsey, 1 as truthy. If you don't like that than you can at least get rid of no-ops (like q1!0 lol) and switch to modulo-1 for 10 bytes – Jo King – 2019-02-12T21:47:37.600

@JoKing Those look really good, I honestly didn't realize that the '0' wasn't doing anything! I feel like such an idiot, in a little while I'll add your programs on (attributed to you of course) – KrystosTheOverlord – 2019-02-12T22:05:30.717

@KrystosTheOverlord in a little while? :P – ASCII-only – 2019-03-07T06:47:47.743

0

Ruby, 18 characters

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

GBrandt

Posted 2017-11-18T03:09:18.727

Reputation: 121

0

Runic Enchantments, 6 bytes

i:kn=@

Try it online!

Works on inputs up to 65535. Compares the input with the input cast to character and back to number. Inputs larger than 65535 cast to char exceed the byte size and roll over.

Draco18s no longer trusts SE

Posted 2017-11-18T03:09:18.727

Reputation: 3 053

0

cQuents, 4 bytes

?$-1

Try it online!

Explanation

?        Given input n, output true if n is in the sequence and false if it is not
         Each term in the sequence equals
 $        current index
  -1                    - 1

The -1 in to include 0 - if the challenge just wanted positive integers, ?$ would be enough.

Stephen

Posted 2017-11-18T03:09:18.727

Reputation: 12 293

0

Stax, 2 bytes

1%

Run and debug it

Modulus by 1.

recursive

Posted 2017-11-18T03:09:18.727

Reputation: 8 616

0

Commodore BASIC (C64/128 C64Mini VIC-20...) 17 tokenized BASIC bytes

 0INPUTA:?A=INT(A)

In Commodore (Microsoft) 8-BIT BASIC, -1 is TRUE and 0 is FALSE.

If you require strictly 1 to represent TRUE and 0 to represent FALSE, this is how to do it:

Version 2, 20 tokenized BASIC bytes

 0INPUTA:?ABS(A=INT(A))

or

 0INPUTA:?-(A=INT(A))

enter image description here

Shaun Bebbers

Posted 2017-11-18T03:09:18.727

Reputation: 1 814