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 code-golf, the shortest submission wins. Good luck!
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 about1.0
? – user202729 – 2017-11-18T05:08:24.327@user202729 Unless you're doing assembly,
-0
evaluates to its simplest form0
, and that's what should be inputted.1.0
evaluates to its simplest form1
, and that's what should be inputted. – MD XF – 2017-11-18T05:20:53.360Must 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 handle1.0
. Is that valid? – user202729 – 2017-11-18T06:58:26.613How 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.123Sinclair 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