| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Perl 5 p | 250123T160937Z | Xcali |
| 004 | ARBLE | 230809T053249Z | ATaco |
| 001 | Kona | 250122T120921Z | Abstract |
| 027 | C gcc | 250121T192526Z | Rosario |
| 043 | ibe | 250121T174207Z | madeforl |
| nan | 250117T020000Z | RARE Kpo | |
| 002 | Thunno 2 | 230809T151634Z | The Thon |
| 005 | floor | 230808T095519Z | bsoelch |
| 082 | MMIX | 210429T183147Z | NoLonger |
| 031 | Hexagony | 210429T180035Z | Undersla |
| 015 | Lua | 210121T192933Z | Benrob03 |
| 003 | Vyxal | 210416T043515Z | Wasif |
| 041 | C | 210121T140938Z | elechris |
| 002 | 05AB1E | 200717T162741Z | Kevin Cr |
| 003 | Pyth | 210122T011103Z | Scott |
| 007 | x8664 machine code | 210121T174059Z | EasyasPi |
| 015 | C gcc | 210121T155316Z | EasyasPi |
| 015 | Python 3 | 210121T135245Z | nTerior |
| 001 | Jelly | 210121T121655Z | caird co |
| 007 | Excel | 200720T230343Z | General |
| 006 | Javascript | 200714T174933Z | Maciej S |
| 039 | Turing Machine Code | 200714T211211Z | ouflak |
| 013 | Python | 150429T010742Z | mbomb007 |
| 001 | APL | 160229T165949Z | Adá |
| 017 | Julia | 191116T231941Z | caseyk |
| 004 | Keg | 191116T101643Z | lyxal |
| 016 | Wren | 191116T091822Z | user8505 |
| 008 | AArch64 machine language Linux | 170911T184131Z | ceilingc |
| 001 | RProgN | 170911T063415Z | ATaco |
| 011 | x86_64 machine language Linux | 170911T055208Z | ceilingc |
| 019 | Java 8 | 170903T192135Z | Jakob |
| 004 | RProgN 2 | 170904T035254Z | ATaco |
| 022 | Perl 5 | 151227T070321Z | Codefun6 |
| 013 | Mouse2002 | 151226T200814Z | cat |
| 005 | MATL | 151226T112214Z | Luis Men |
| 080 | C | 110314T154305Z | Hannesh |
| 037 | PHP | 110131T215211Z | Kevin Br |
| 056 | C# | 110131T173707Z | Nellius |
| 023 | Perl | 110204T032029Z | Kent Fre |
| 032 | JavaScript | 110201T174235Z | Kevin Br |
| 007 | J | 150428T145806Z | Eelvex |
| 020 | Python | 110315T023223Z | dan04 |
| nan | 110314T160156Z | Michael | |
| 010 | PARI/GP | 110131T170632Z | Eelvex |
| 081 | Python | 110204T012818Z | l0nwlf |
| nan | 110201T201354Z | Alexandr | |
| 015 | DC | 110201T134631Z | Hiato |
| 026 | LISP | 110131T180010Z | Eelvex |
| 051 | C | 110131T171831Z | Eelvex |
Kona, 1 Byte
Built-in function
_
C (gcc), 27 bytes
f(float a){return a-(a<0);}
It means that the function cast the float to one int, is floor for number >0 else it is floor + 1
ibe, 43 bytes
dsQagQmqadafWmQakqagWmwsaWmwmqsjWmwagQmWasQ
since ibe doesn't have a built-in floor function, i had to make one myself which was pretty fun lol.
heres how it works (this is not valid code because ibe does not support commenting):
dsQ get input, set it to Q
agQmq convert Q to a number (add 0 to it)
ad set W to 0
(W will be the fractional part of Q)
afWmQ copy Q to W
akq do...
agWmw subtract 1 from W
saWmwmq ...while W is greater than 1
sjWmw modulo W by 1 (to prevent W from being 1 in special occasions)
(W is now the fractional part of Q)
agQmW subtract W from Q to get the integer part of Q
asQ print it out (the final floored number)
awk
It works by recognizing int() truncation has same effect as ceil() for negative operands, so it subtracts out the boolean outcome from its integral value.
jot - -3.25 3.25 0.25 |
awk 'function __(_) { return -(+_<(_=int(_)))+_ }'
-3.25 -4 -1.00 -1 1.25 1
-3.00 -3 -0.75 -1 1.50 1
-2.75 -3 -0.50 -1 1.75 1
-2.50 -3 -0.25 -1 2.00 2
-2.25 -3 0.00 0 2.25 2
-2.00 -2 0.25 0 2.50 2
-1.75 -2 0.50 0 2.75 2
-1.50 -2 0.75 0 3.00 3
-1.25 -2 1.00 1 3.25 3
The function is the most portable possible because it makes no assumption of data representation structure of the input, nor is it dependent on language-specific division-approaches (e.g. floored-division). In fact, bitwise ops and div(/)-mod(%) ops are totally overkill for this task.
ps : Not that skipping the int() function call saves that much :
awk 'function __(_) { return -(+_<(_=int(_)))+_ }'
awk 'function __(_) { return -(+_<(_-=_%1))+_ }'
Thunno 2, 2 bytes
1÷
Floor divide by one.
Funnily enough, Thunno 2 doesn't have a proper floor built-in. For positive numbers, you can use N (truncate), but that doesn't work for negative numbers.
floor, 5 bytes
floor
The floor function is a (the only) built-in.
Full program (17 bytes):
Floor only accepts integers as program Input, so the program uses two inputs to generate a rational number to be passed to the floor function
f:x y->floor(x/y)
MMIX, 8 bytes (2 instrs)
05000300 F8010000
Disassembly
FIX $0,ROUND_DOWN,$0
POP 1,0
MMIX has a builtin instruction for this. You can specify the rounding mode if you like, but you can use the default. (Options are towards 0, towards \$-\infty\$, towards \$\infty\$, or to nearest half to even.)
Honestly, I'd advise using a FIX directly, as opposed to calling.
Hexagony, 31 bytes
,{.4{?<.(@!?\"!3'-<_".$>@>?~..@
, { . 4
{ ? < . (
@ ! ? \ " !
3 ' - < _ " .
$ > @ > ? ~
. . @ . .
. . . .
Try it online! or Try it online differently!
Takes input of the form +/-a.b, ex: +4.5 or -0.1 or +3. Since hexagony doesn't have built in decimal types, the input must contain a + if positive in order to not cut off the first digit of the number. This answer can definitely be improved, but its a start.
Lua, 27 25 18 15 bytes
print(...//1|0)
Explanation
... variable-argument operator, returns the remaining arguments passed to the current function (top-level code block with all the program arguments in this case).
//1 floor divides by 1.
|0 bitwise OR, we use this to convert to an int.
Vyxal, 3 bytes
1%-
Push input, modulo with 1 and negate!
Vyxal, 1 byte
⌊
Though a builtin.....
C, 42 41 bytes
int F(float x){int i=x;return i<=x?i:i-1;}
int F(float x){int i=x;return x>i?i:i-1;}
05AB1E, 2 bytes
Oï
Try it online or verify all values in the range \$[5,-5]\$ in \$0.1\$ increments.
Explanation:
O # Sum the stack, which will use the (implicit) input-string if the stack is empty
# (the input is read as string by default, so this `O` basically casts it to a float)
ï # Floor this input-float
# (and output the resulting integer implicitly)
Although it may look pretty straight-forward, there are actually some things to note:
In the legacy version of 05AB1E, which was built in Python. The ï builtin would translate to the following code snippets:
# Pop number
a = str(number) # Cast number to a string
a = ast.literal_eval(a) # Evaluate this string as Python structure (so it goes back to a float)
a = int(a) # Cast this float to an integer (which will truncate)
So whether you had string input or decimal input, it would cast it to a string during the code execution anyway, before casting it to an integer to truncate all decimal values.
Try it online with -0.5 as decimal argument.
Try it online with "-0.5" as string argument.
The new version of 05AB1E is built in Elixir however. Now, the ï builtin translates to the following code snippets (huge parts removed to only keep the relevant stuff):
call_unary(fn x -> to_integer(x) end, a) # Call the function `to_integer`, which will:
# (I've only kept relevant parts of this function)
is_float(value) -> round(Float.floor(value)) # If it's a float: floor it down
true -> # Else (it's a string):
case Integer.parse(to_string(value)) do # Parse it from string to integer
:error -> value # If this resulted in an error: return as is
{int, string} ->
cond do # Else it was parsed without errors:
Regex.match?(~r/^\.\d+$/, string) -> int
# If it contains no decimal values:
# Return parsed int
true -> value # Else: return as is
Or as a TL;DR: float inputs are floored; string inputs are truncated.
Try it online with -0.5 as decimal argument.
Try it online with "-0.5" as string argument.
As you can see, a string input gives the incorrect floored result for negative decimals. Unfortunately, the default (implicit) input from STDIN is always a string, so we'll have to convert it to a float first (for which I've used O - which only works on an empty stack in the new 05AB1E version built in Elixir; for the legacy 05AB1E version built in Python, the sum would result in 0 for an empty stack).
x86-64 machine code, 7 bytes
Machine code:
00000000: 66 0f 3a 0b c0 01 c3 f.:....
Assembly:
.intel_syntax noprefix
.globl floor_sse4
floor_sse4:
roundsd xmm0, xmm0, 1
ret
Accepts a double in an xmm0.
Returns the floored double in xmm0.
Outgolfs the previous answer without cheesing with fixed point.
- Nowhere did it say the return type had to be an
int, just an integer. Therefore, I can just skip the conversion code, storing the integer as adouble. JavaScript has been doing it for decades. - This one only requires SSE4.1. 😏
Yes, I know the SSE conversion functions are a little redundant in the test code.
x86-64 machine code, fixed point joke, 1 byte
Machine code:
00000000: c3 .
Assembly:
.intel_syntax noprefix
.globl floor_fixed
floor_fixed:
ret
This function takes a 16-bit fixed8 in ax and returns a signed 8-bit integer in ah. It is not sign extended.
Nobody gave any specifics about precision 😂
C (gcc), 16 15 bytes
g;f(d){g=d>>9;}
Such number systems include fixed-point numbers, floating point numbers and strings.
I don't know why people insist on using floats... Fixed point is much easier 🙂
Uses 32-bit fixed9 precision. Abuses arithmetic shift right. Return hack, not much else to say.
Jelly, 1 byte
Ḟ
or a non-builtin answer
%1_@
How the second one works
%1_@ - Main link. Takes n on the left
%1 - Fractional part of n
_@ - n - frac(n)
Excel, 7
Tested in Excel Online. Closing parens not counted toward score.
The only reason I posted this is because, oddly enough, it seems to work the exact same as FLOOR.MATH() with 1 argument.
=INT(A1)
Semantically, at least to me, it would make sense if this didn't work for negative numbers. However, INT(-.5) is -1 for some reason. TRUNC() does what I think INT() should do.
Javascript - 6 bytes
Usage of the arrow function declaration (as an anonymous function here) and a bitwise "AND" operator
x=>x&x
or alternatively using the bitwise "OR" operator:
x=>x|0
Python, 13
If the OP wants all functions to be non-anonymous, add f= to the front of each for two additional characters.
lambda x:x//1
Since x%1 returns the amount following the decimal point, this is pretty short (14):
lambda x:x-x%1
The shortest using string casting I could come up with (40):
lambda x:int(`x`.split('.')[0])+cmp(x,0)
APL, 1 byte (SBCS)
According to the updated rules, built-ins are allowed:
⌊
APL (dzaima/APL), 5 4 bytes
Anonymous tacit prefix function abiding by the old prohibition on built-ins:
⊢-1|
⊢ the argument
- minus
1| the division remainder when divided by 1
Julia, 17 bytes
f(x)=x-x%1-(x<0)
port of the PARI/GP answer
f(-pi) = -4
f(pi) = 3
Wren, 16 bytes
Exactly ported from the PARI/GP answer.
Fn.new{|x|x-x%1}
Wren, 26 bytes
Ported from the Keg answer.
Fn.new{|x|x.split(".")[0]}
AArch64 machine language (Linux), 8 bytes
0: 1e700000 fcvtms w0, d0
4: d65f03c0 ret
To try it out, compile and run the following C program
#include<stdio.h>
#include<math.h>
int f(double x){return floor(x);}
const char g[]="\x00\x00\x70\x1e\xc0\x03\x5f\xd6";
int main(){
for( double d = -1.5; d < 1.5; d+=.2 ) {
printf( "%f %d %d\n", d, f(d), ((int(*)(double))g)(d) );
}
}
x86_64 machine language (Linux), 11 bytes
0: c4 e3 79 0b c0 01 vroundsd $0x1,%xmm0,%xmm0,%xmm0
6: f2 0f 2c c0 cvttsd2si %xmm0,%eax
a: c3 retq
This requires a processor with AVX instructions.
To Try it online!, compile and run the following C program.
#include<stdio.h>
#include<math.h>
int f(double x){return floor(x);}
const char g[]="\xc4\xe3\x79\x0b\xc0\x01\xf2\x0f\x2c\xc0\xc3";
int main(){
for( double d = -1.5; d < 1.5; d+=.2 ) {
printf( "%f %d %d\n", d, f(d), ((int(*)(double))g)(d) );
}
}
Java 8, 19 bytes
Lambda from double to int. There are plenty of choices for types to assign to: Function<Double, Integer>, DoubleFunction<Integer>, or DoubleToIntFunction.
n->(int)(n<0?n-1:n)
RProgN 2, 4 bytes
]1%-
Explained
]1%-
] # Duplicate the implicit input
1% # Modulo 1
-# Subtract Modulo 1 of the input from the input, inplicitly outputting the floor'd result.
Perl 5, 22 bytes
s/\..+//&&$_<0?$_--:$_
This is larger than my first answer, which merely truncated the number - so, for negative numbers, it wasn't correct. This was due to a misunderstanding on my part of what a floor function does.
Mouse-2002, 13 bytes
Second place, ahh! (And this is a full program, not a function definition)
?&DUP &FRAC -
Take the fractional bit of the input and subtract it from the input.
MATL, 5 bytes
The programming language used in this answer was created after the challenge was posted.
it1\-
Examples
>> matl it1\-
> 5.6
5
>> matl it1\-
> -5.2
-6
Explanation
Pretty straightforward. It uses a modulo operation with divisor 1.
i % input
t % duplicate
1 % number literal
\ % modulus after division
- % subtraction
C (80)
Well it's not the shortest, but it's a great opportunity to show off my bit twiddling skills :D.
main(){int I,X=0x7FFFFF;scanf("%f",&I);printf("%d",((I&X)|X+1)>>-(I>>23)+150);}
PHP, 51 45 43 37 characters
function f($n){return~~$n-(~~$n>$n);}
This should be able to be applied to most languages that do not support the n%1 trick.
C# (56 chars):
My quick and naive answer earlier had a stupid logical flaw in it. Two approaches here, which I believe are both the same length. Double approach relies on the fact that casting to int removes the decimal part of a double.
int F(double d){return(int)(d%1==0?d:(int)d-(d<0?1:0));}
Decimal approach relies on the fact that d%1 returns the decimal part of the number for decimal data type.
int F(decimal d){return(int)(d%1==0?d:d-d%1-(d<0?1:0));}
Could save a few characters in both cases by returning their own type instead of int, but I feel a floor function should return an int.
Perl (23)
$_=int($_)-(int($_)>$_)
Example:
perl -ple '$_=int($_)-(int($_)>$_)'
Every value entered on input will now be printed "floored".
Its bass5098's technique, but smaller =).
As a function(36):
sub f{int($_[0])-(int($_[0])>$_[0])}
JavaScript, 50 34 33 32 characters
function f(n){return~~n-(~~n>n)}
Works the same way as the PHP one I submitted.
J 7 chars
f=:-1&| NB. x - (x mod 1)
eg.
f 3.14
3
f _3.14
_4
Python (20)
f=lambda x:int(x//1)
or, if the result doesn't need to be of type int, 15 characters:
f=lambda x:x//1
Ruby 1.9 (12 14)
f=->x{x-x%1}
It's more a "for the record" type solution along the lines of the PARI/GP one.
>> f[3.4] #=> 3.0
>> f[-3.4] #=> -4.0
PARI/GP (10)
In gp (and some other languages) x%1 gives the decimal part of x:
f(x)=x-x%1
NOTE: For negative x, x%1 returns (1 - abs(decimal part of x)), so the above works both for positive and negative numbers.
Python (81)
def f(x):return str(x - float("." + str(float(x)).split('.')[-1])).split('.')[0]
C 126 (including NL)
Doesn't use any built-in conversion such as (int)x.
r(float x) {
int
p=*(int*)&x,
f=p&8388607|1<<23,
e=((p>>23)&255)-150;
if(e>0)f*=1<<e;
if(e<0)f/=1<<-e;
return p>>31?-f-1:f;
}
DC (15 bytes)
Makes use of a nifty little trick that occurs during division in DC. Add a 'p' to then end to get output (it performs the floor correctly anyway), I assume that stuff is not already on the stack, and that input is in stdin.
[1-]sazk?z/d0>a
EG: echo 0 2.6 - | dc -e '[1-]sazk?z/d0>ap'
LISP (26)
(Same trick as in PARI/GP answer)
(defun f(x)(- x(mod x 1)))
C (51)
int F(float x){int i=x-2;while(++i<=x-1);return i;}