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

82 Answers 82

1 2
3
0
\$\begingroup\$

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.

\$\endgroup\$
6
  • 3
    \$\begingroup\$ or #define f(v)!rintf(v) \$\endgroup\$ Nov 19 '17 at 8:18
  • \$\begingroup\$ You should be able to use the implicit int typing if you drop the int. \$\endgroup\$
    – Potato44
    Nov 20 '17 at 4:55
  • \$\begingroup\$ @technosaurus - Why not accept double and save a byte? #define f(v)!rint(v) \$\endgroup\$ Nov 20 '17 at 14:50
  • \$\begingroup\$ Actually, I don't think this answer is correct at all - should it be v==rint(v)? Or perhaps !fmod(v,1) \$\endgroup\$ Nov 20 '17 at 14:52
  • \$\begingroup\$ @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. \$\endgroup\$
    – CSM
    Nov 21 '17 at 21:53
0
\$\begingroup\$

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.

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

Julia, 13 bytes

f(x)=x in 0:x

Try it online!

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

ARBLE, 9 bytes

eq(a%1,0)

Try it online!

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

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!

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

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.

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

Jelly, 3 bytes

Ḟ⁼A

Try it online!

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

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

Jelly, 3 bytes

AḞ=

Try it online!

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

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!

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

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.

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

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Fails on 3 and -3.1415926 \$\endgroup\$
    – Herman L
    Mar 5 '18 at 18:14
0
\$\begingroup\$

TI-BASIC, 5 bytes

Takes input and gives output through Ans.

Ans=abs(int(Ans
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ @JoKing thanks for pointing out. \$\endgroup\$
    – Sourav
    Nov 19 '18 at 11:44
  • \$\begingroup\$ false can be 0 and I'm pretty sure === can be == \$\endgroup\$
    – Jo King
    Nov 19 '18 at 12:00
  • \$\begingroup\$ @JoKing yes. that way I will reduce 1 byte. \$\endgroup\$
    – Sourav
    Nov 19 '18 at 12:11
  • \$\begingroup\$ and four bytes for false to 0.. or if you're really insistent on booleans rather than falsey values, at least do 0>1 \$\endgroup\$
    – Jo King
    Nov 19 '18 at 12:21
  • \$\begingroup\$ @JoKing the output requires false. If think that way then 0 is not significant option. \$\endgroup\$
    – Sourav
    Nov 19 '18 at 12:24
0
\$\begingroup\$

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Deadcode
    Feb 11 '19 at 0:52
  • \$\begingroup\$ @Deadcode Well, crap. You're right. Thanks for pointing this out! \$\endgroup\$ Feb 12 '19 at 15:37
0
\$\begingroup\$

Gol><>, 17 bytes

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

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Feb 12 '19 at 21:47
  • \$\begingroup\$ @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) \$\endgroup\$ Feb 12 '19 at 22:05
  • \$\begingroup\$ @KrystosTheOverlord in a little while? :P \$\endgroup\$
    – ASCII-only
    Mar 7 '19 at 6:47
0
\$\begingroup\$

Ruby, 18 characters

->x{x>=0&&0==x%1}
\$\endgroup\$
0
\$\begingroup\$

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.

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

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.

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

Stax, 2 bytes

1%

Run and debug it

Modulus by 1.

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

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

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

Vyxal, 2 bytes

⌊=

Try it Online!

⌊  # Floor
 = # Is equal to original?
\$\endgroup\$
1 2
3

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.