17
\$\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\$ Commented Jan 31, 2011 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\$ Commented Jan 31, 2011 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\$ Commented Jan 31, 2011 at 19:39
  • 2
    \$\begingroup\$ In golfscript, this would be zero bytes since x is already an integer :) \$\endgroup\$
    – gnibbler
    Commented Jan 31, 2011 at 20:53
  • 1
    \$\begingroup\$ @TheGuywithTheHat Type casting is fine, but this does not give the correct answer. \$\endgroup\$ Commented Apr 7, 2014 at 17:15

44 Answers 44

1
2
0
\$\begingroup\$

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.

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

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.

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

RProgN, 1 byte

_

A builtin, thanks to the new challenge spec.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ same for K Try it online! \$\endgroup\$
    – mkst
    Commented Sep 11, 2017 at 9:57
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
    Commented Jul 14, 2020 at 20:41
  • 1
    \$\begingroup\$ @Dat I'll just remove it, because int(x//1) has already been done. \$\endgroup\$
    – mbomb007
    Commented Jul 14, 2020 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\$

Pyth, 3 bytes

/Q1

Try it online!

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

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

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

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.

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

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!

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

Thunno 2, 2 bytes

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.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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