15
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Any limits on the size of the numbers to be handled correctly? Need to treat negatives? \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 31 '11 at 17:00
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 31 '11 at 17:21
  • \$\begingroup\$ @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). \$\endgroup\$ – Daniel Standage Jan 31 '11 at 19:39
  • 2
    \$\begingroup\$ In golfscript, this would be zero bytes since x is already an integer :) \$\endgroup\$ – gnibbler Jan 31 '11 at 20:53
  • 1
    \$\begingroup\$ @TheGuywithTheHat Type casting is fine, but this does not give the correct answer. \$\endgroup\$ – Daniel Standage Apr 7 '14 at 17:15

40 Answers 40

1
2
0
\$\begingroup\$

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) );
  }
}
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

Keg, 6 5 4 bytes

:1%-

Try it online!

Look ma, no unicode!

A port of the RProgN2 answer

\$\endgroup\$
0
\$\begingroup\$

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

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)
\$\endgroup\$
0
\$\begingroup\$

Python 3, 15 bytes

f=lambda n:n//1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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 😂

\$\endgroup\$
0
\$\begingroup\$

Lua, 27 25 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.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 3 bytes

/Q1

Try it online!

\$\endgroup\$
-1
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Doesn't work for negative numbers. \$\endgroup\$ – lirtosiast Oct 10 '15 at 22:21
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.