| Bytes | Lang | Time | Link |
|---|---|---|---|
| 016 | Python 3 | 250808T030205Z | Lucenapo |
| 032 | Tcl | 250807T145817Z | sergiol |
| 018 | AWK | 250806T203531Z | xrs |
| 007 | ARBLE | 171122T222528Z | ATaco |
| 002 | Thunno 2 | 230608T183514Z | The Thon |
| 002 | Vyxal | 210608T063830Z | emanresu |
| 017 | Commodore BASIC C64/128 C64Mini VIC20... 17 tokenized BASIC bytes | 190617T094403Z | Shaun Be |
| 002 | Stax | 190307T164822Z | recursiv |
| 006 | Runic Enchantments | 190307T152042Z | Draco18s |
| 004 | cQuents | 190307T155027Z | Stephen |
| 003 | TIBASIC TI84 | 190307T143430Z | absolute |
| 018 | Ruby | 190307T114647Z | GBrandt |
| 010 | ><> | 190307T102334Z | Emigna |
| 007 | Brachylog | 190307T055105Z | Unrelate |
| 017 | Gol><> | 190212T160330Z | KrystosT |
| 057 | Python 2 | 181113T230623Z | Triggern |
| 003 | MathGolf | 181120T150543Z | maxb |
| 231 | PHP | 181119T212518Z | Titus |
| 024 | JavaScript SpiderMonkey | 181113T171507Z | yetanoth |
| 067 | Scratch in scratchblocks2 | 181119T090405Z | W. K. |
| 003 | 05AB1E | 181113T084002Z | Kevin Cr |
| 011 | JavaScript Node.js | 181113T074109Z | l4m2 |
| 005 | TIBASIC | 181112T202951Z | kamoroso |
| 037 | C | 171118T175134Z | HatsuPoi |
| 016 | Javascript 16 Bytes | 180305T181140Z | Luis fel |
| 073 | SNOBOL4 CSNOBOL4 | 180305T175838Z | Giuseppe |
| 004 | Pyt | 180126T202736Z | qqq |
| 025 | Scala | 171120T172717Z | sprague4 |
| 020 | Unexpanded Sinclair ZX81 | 171122T100748Z | Shaun Be |
| 003 | Jelly | 180126T210324Z | chromati |
| 003 | Jelly | 180126T205359Z | ellie |
| 553 | Taxi | 180126T205247Z | Engineer |
| 024 | Prolog SWI | 180102T103347Z | qwertxzy |
| 010 | Google Sheets | 171119T175454Z | Taylor R |
| 043 | C# | 171118T175342Z | HatsuPoi |
| 003 | Japt | 171118T094853Z | Shaggy |
| 006 | Hexagony | 171127T154359Z | Martin E |
| 010 | Pyth 10 Bytes | 171127T201810Z | Tornado5 |
| 030 | D | 171119T011611Z | Adalynn |
| 015 | JavaScript | 171119T225625Z | ericw314 |
| 033 | C++ gcc | 171125T162650Z | HenrikS |
| 025 | C gcc | 171118T041305Z | MD XF |
| 028 | PHP | 171124T181553Z | halfmang |
| 024 | PowerShell | 171121T045516Z | briantis |
| 013 | Julia | 171120T195134Z | EricSher |
| 004 | Pyth | 171120T194736Z | Stan Str |
| 010 | Implicit version predating challenge | 171119T035836Z | MD XF |
| 031 | C on Linux | 171118T175434Z | CSM |
| 023 | C + ecpp | 171120T192540Z | MD XF |
| 017 | Kotlin | 171120T190548Z | ovs |
| 030 | R | 171120T173913Z | Andrew H |
| 017 | Ruby | 171120T161841Z | Reinstat |
| 029 | C# | 171118T192851Z | yoonicod |
| 027 | REXX | 171120T094115Z | idrougge |
| 043 | D | 171118T175611Z | HatsuPoi |
| 048 | Python 3 | 171118T124333Z | Ben |
| 012 | Clojurescript | 171119T164456Z | MattPutn |
| 038 | Lua | 171119T094600Z | MCAdvent |
| 004 | Enlist | 171119T091552Z | user2027 |
| 003 | Jelly | 171118T043435Z | user2027 |
| 014 | Java OpenJDK 8 | 171119T015259Z | Olivier |
| 008 | MY | 171119T013424Z | Adalynn |
| 015 | Jq 1.5 | 171119T001140Z | jq170727 |
| 010 | Retina | 171118T045735Z | mbomb007 |
| 016 | Haskell | 171118T114129Z | flawr |
| nan | 171118T210519Z | Brad Gil | |
| 021 | Symbolic Python | 171118T205909Z | FlipTack |
| 015 | Octave | 171118T100733Z | Tom Carp |
| 018 | Python 2 and Python 3 | 171118T032137Z | Thomas W |
| 020 | Ruby | 171118T124059Z | marmelad |
| 014 | Wolfram Language Mathematica | 171118T033303Z | Conor O& |
| 012 | JavaScript | 171118T031227Z | Nissa |
| 111 | Perl 5 | 171118T152546Z | Nahuel F |
| 017 | QBIC | 171118T130940Z | steenber |
| 032 | Common Lisp | 171118T124412Z | Renzo |
| 018 | Python 2 | 171118T123409Z | ovs |
| 004 | Pyth | 171118T112727Z | izzyg |
| 014 | JavaScript | 171118T111016Z | edc65 |
| 004 | Recursiva | 171118T104609Z | 0xffcour |
| 004 | J | 171118T083048Z | Galen Iv |
| 003 | APL Dyalog | 171118T100443Z | Erik the |
| 003 | Husk | 171118T095537Z | Zgarb |
| 094 | /// | 171118T074258Z | boboquac |
| 047 | Ly | 171118T035307Z | LyricLy |
| 006 | Pyth | 171118T040313Z | Mr. Xcod |
| 006 | Aceto | 171118T040406Z | qqq |
Thunno 2, 2 bytes
N=
Explanation
N= # Implicit input
N # Cast to integer
= # Equals the input?
# Implicit output
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))
Runic Enchantments, 6 bytes
i:kn=@
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.
cQuents, 4 bytes
?$-1
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.
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.
Ruby, 18 characters
->x{x>=0&&0==x%1}
Brachylog, 7 bytes
⌋₁ℕ.&⌉₁
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.
Python 2, 59 60 57 bytes
lambda s:re.match(r"^\+?[0-9]+(\.0*)?$",s)>None
import re
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.
MathGolf, 3 bytes
i±=
Explanation
Same as a bunch of other answers.
i Read input as integer
± Convert to absolute value
= Is it equal to the implicit input?
PHP, 23+1 bytes
<?=$argn==abs(0|$argn);
prints 1 for truthy; empty output for falsy.
Run as pipe with -F.
JavaScript (SpiderMonkey), 24 bytes
s=>s<0?0:s==Math.ceil(s)
A great thanks to @Jo King for converting the code from 29bytes to 24.
Scratch in scratchblocks2, 92 67 bytes
thanks lirtosiast
when gf clicked
ask[]and wait
say<([abs v]of(answer))=(round(answer
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)
TI-BASIC, 5 bytes
Takes input and gives output through Ans.
Ans=abs(int(Ans
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;
}
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))
SNOBOL4 (CSNOBOL4), 73 bytes
DEFINE('W(N)D')
W N SPAN('1234567890') '.' REM . D
W =EQ(D) 1 :(RETURN)
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.
Pyt, 3 4 bytes
Đ⎶Å≡
implicit input
Đ duplicates top value
⎶ pushes closest int to top value
Å abs(TOS)
≡ checks if equal
implicit output
Scala, 25 chars
(f:Double)=>f.ceil.abs==f
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.
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.
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.
Google Sheets, 10 Bytes
Anonymous worksheet function that tales input from cell A1 and outputs to the calling cell.
=A1=Int(A1
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));
}
}
}
Japt, 6 4 3 bytes
¶Âa
Explanation
Checks for strict equality (¶) between the input and the absolute value (a) of the input with bitwise NOT applied twice (Â).
Hexagony, 6 bytes
?~<%@'
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.
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
JavaScript, 17 15 bytes
_=>_<0?0:_==~~_
_=>_<0?0:_==(_|0)
Thanks to edc65 for catching my mistakes.
C (gcc), 27 28 27 25 bytes
-2 thanks to PrincePolka
#define g(f)!fmod(f,f>=0)
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.
PHP, 28 bytes
First solution:
echo!(ceil($n=$argv[1])-$n);
Second solution:
echo floor($n=$argv[1])==$n;
PowerShell, 24 bytes
+"$args"-match'^[^-.]+$'
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 :(
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?
Version that doesn’t find 0 as a whole number:
&qs.a
Ands the original answer with the input, so 0 falsifies the answer.
Returns a falsy or truthy value that isn’t always a Boolean.
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
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.
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.
R, 30 22 bytes
a=scan();`if`(a%%1==0&a>0,T,F)
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;}
REXX, 27 bytes
arg n
say trunc(n)=n & n>=0
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");
}
}
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
Enlist, 4 bytes
:1Ae
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.
MY, 8 bytes
ω≥ωω⌊=∧←
The link uses arrays to test multiple numbers at once.
How?
ω≥=ω>=0(0is popped when stack is empty)ωω⌊==ω==floor(ω)∧←= take the logical and of the things above, then output.
Jq 1.5, 15 bytes
.>=0and.==floor
Not much to explain:
(. >= 0) and # non-negative and
(. == floor) # value == (value truncated to integer)
Retina, 12 10 bytes
Match the format of a non-negative whole number
^\d+\.?0*$
-2 thanks to FryAmTheEggman
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]
Symbolic Python, 21 bytes
_=_%(_==_)==(_!=_)<=_
Uses a chained comparison:
_%(_==_) == (_!=_)checks ifn%1 == 0, only true ifnhas no decimal part.(_!=_) <= _checks if0 <= n, only true if the integer is non-negative.
Octave, 15 bytes
@(x)any(x==0:x)
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)
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))
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)
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 >.
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.
Ruby, 20 bytes
->x{x.div(1)==x.abs}
Ruby, 22 bytes
->x{[0,x].max==x.to_i}
Wolfram Language (Mathematica), 17 15 14 bytes
Saved 1 byte thanks to Not a tree!
#>=0==#~Mod~1&
First Mathematica answer \o/
Mathematica, 15 bytes
Saved 2 bytes thanks to @user202729!
#==⌊Abs@#⌋&
JavaScript, 9 12 bytes
n=>!n%1&n>=0
Didn't realize that I had to take in negatives. Pointed out by @kamoroso94.
Perl 5, 11 +1(-p) bytes
$_=abs==int
The -l switch not counted because for tests display
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
JavaScript, 14
n=>!(n<0||n%1)
APL (Dyalog), 3 bytes
⌊≡|
Note that f← has been prepended in the TIO link due to technical limitations, but it's not normally needed.
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?
///, 94 bytes, input hard-coded
/=/\/x://:/\/\///-0/-:.0/.:.|/|:-|/|:0=1=2=3=4=5=6=7=8=9=|x/|:||/true:x:/-:/.:/|/+:++/false/||
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.
Ly, 35 47 bytes
ir"-"=[0u;]pr[["0"=![psp>l"."=[ppr!u;]p<2$]pp]]
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.
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
