# Shortest Floor Function

Your task is to implement a floor function in as few bytes as possible.

A floor function is a function that takes a real number and returns the largest integer less than or equal to the input.

Your program should support both positive and negative inputs. Since it is provably impossible to support all real numbers you need only support a reasonable subset of them. This subset should include positive numbers, negative numbers and of course numbers that are not integers. Such number systems include fixed-point numbers, floating point numbers and strings.

Your code may be a complete program or function.

This is so answers will be scored in bytes with less bytes being a better score.

• Any limits on the size of the numbers to be handled correctly? Need to treat negatives? Commented Jan 31, 2011 at 17:00
• I ask about range because single precision IEEE 745 floating point runs up to 2^127, which is to say that 64 bit integers would not be sufficient. Commented Jan 31, 2011 at 17:21
• @dmckee The problem definition states any real number, not just positive ones. As far as number size is concerned, let's assume that is not a concern (i.e., an answer that handles that case is not necessarily better than an answer that doesn't handle that case, unless of course it is of equal or lesser length). Commented Jan 31, 2011 at 19:39
• In golfscript, this would be zero bytes since x is already an integer :) Commented Jan 31, 2011 at 20:53
• @TheGuywithTheHat Type casting is fine, but this does not give the correct answer. Commented Apr 7, 2014 at 17:15

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

# 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. # RProgN, 1 byte _  A builtin, thanks to the new challenge spec. Try it online! • same for K Try it online! – mkst Commented Sep 11, 2017 at 9:57 # 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) ); } }  # Wren, 16 bytes Exactly ported from the PARI/GP answer. Fn.new{|x|x-x%1}  Try it online! # Wren, 26 bytes Ported from the Keg answer. Fn.new{|x|x.split(".")[0]}  Try it online! # Keg, 65 4 bytes :1%-  Try it online! Look ma, no unicode! A port of the RProgN2 answer # 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)  • int(x) wouldn't work since python floor will round up negative numbers while floor will always go to the lower ones. For example. floor(-5.9) should be -6, but int(-5.9) will return -5. you can take a couple more bytes and add a -1 if the original number is smaller than 1. – Dat Commented Jul 14, 2020 at 20:41 • @Dat I'll just remove it, because int(x//1) has already been done. Commented Jul 14, 2020 at 20:46 # Jelly, 1 byte Ḟ  Try it online! or a non-builtin answer %1_@  Try it online! ## How the second one works %1_@ - Main link. Takes n on the left %1 - Fractional part of n _@ - n - frac(n)  # Python 3, 15 bytes f=lambda n:n//1  Try it online! # Pyth, 3 bytes /Q1  Try it online! # Lua, 272518 15 bytes print(...//1|0)  Try it online! ### 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. # 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.

# ARBLE, 4 bytes

n//1


Try it online!

Outputs with a leading .0. If this is forbidden, then...

# ARBLE, 5 bytes

floor


Try it online!

# Thunno 2, 2 bytes

1÷


Try it online!

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.