# 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? – dmckee --- ex-moderator kitten Jan 31 '11 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. – dmckee --- ex-moderator kitten Jan 31 '11 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). – Daniel Standage Jan 31 '11 at 19:39
• In golfscript, this would be zero bytes since x is already an integer :) – gnibbler Jan 31 '11 at 20:53
• @TheGuywithTheHat Type casting is fine, but this does not give the correct answer. – Daniel Standage Apr 7 '14 at 17:15

# 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

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 Jul 14 '20 at 20:41
• @Dat I'll just remove it, because int(x//1) has already been done. – mbomb007 Jul 14 '20 at 20:46

# Jelly, 1 byte

Ḟ


Try it online!

%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!

# 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


Try it online!

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 a double. 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


Try it online!

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 😂

# Lua, 2725 18 bytes

print(arg[1]//1|0)


Try it online!

### Explanation

arg[1] is the first argument passed to the script.

//1 floor divides by 1.

|0 bitwise OR, we use this to convert to an int.

# Pyth, 3 bytes

/Q1


Try it online!

# TI-Basic, 4 bytes

X-fPart(X


fPart( is a one-byte token, stands for 'fractional part' and returns anything right of the decimal point. Parentheses don't need to be closed in TI-Basic.

• Doesn't work for negative numbers. – lirtosiast Oct 10 '15 at 22:21