g | x | w | all
Bytes Lang Time Link
nanPerl 5 p250123T160937ZXcali
004ARBLE230809T053249ZATaco
001Kona250122T120921ZAbstract
027C gcc250121T192526ZRosario
043ibe250121T174207Zmadeforl
nan250117T020000ZRARE Kpo
002Thunno 2230809T151634ZThe Thon
005floor230808T095519Zbsoelch
082MMIX210429T183147ZNoLonger
031Hexagony210429T180035ZUndersla
015Lua210121T192933ZBenrob03
003Vyxal210416T043515ZWasif
041C210121T140938Zelechris
00205AB1E200717T162741ZKevin Cr
003Pyth210122T011103ZScott
007x8664 machine code210121T174059ZEasyasPi
015C gcc210121T155316ZEasyasPi
015Python 3210121T135245ZnTerior
001Jelly210121T121655Zcaird co
007Excel200720T230343ZGeneral
006Javascript200714T174933ZMaciej S
039Turing Machine Code200714T211211Zouflak
013Python150429T010742Zmbomb007
001APL160229T165949ZAdá
017Julia191116T231941Zcaseyk
004Keg191116T101643Zlyxal
016Wren191116T091822Zuser8505
008AArch64 machine language Linux170911T184131Zceilingc
001RProgN170911T063415ZATaco
011x86_64 machine language Linux170911T055208Zceilingc
019Java 8170903T192135ZJakob
004RProgN 2170904T035254ZATaco
022Perl 5151227T070321ZCodefun6
013Mouse2002151226T200814Zcat
005MATL151226T112214ZLuis Men
080C110314T154305ZHannesh
037PHP110131T215211ZKevin Br
056C#110131T173707ZNellius
023Perl110204T032029ZKent Fre
032JavaScript110201T174235ZKevin Br
007J150428T145806ZEelvex
020Python110315T023223Zdan04
nan110314T160156ZMichael
010PARI/GP110131T170632ZEelvex
081Python110204T012818Zl0nwlf
nan110201T201354ZAlexandr
015DC110201T134631ZHiato
026LISP110131T180010ZEelvex
051C110131T171831ZEelvex

Perl 5 -p, 14 + 1 = 15 bytes

$_=-($_<0)+int

Try it online!

ARBLE, 4 bytes

n//1

Try it online!

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

ARBLE, 5 bytes

floor

Try it online!

Kona, 1 Byte

Built-in function _

C (gcc), 27 bytes

f(float a){return a-(a<0);}

Try it online!

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

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.

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)

implementation

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)

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.

Vyxal, 3 bytes

1%-

Push input, modulo with 1 and negate!

Try it!

Vyxal, 1 byte

Though a builtin.....

Try it!

C, 42 41 bytes

int F(float x){int i=x;return i<=x?i:i-1;}

Try it online!

int F(float x){int i=x;return x>i?i:i-1;}

Try it online!

05AB1E, 2 bytes

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

Pyth, 3 bytes

/Q1

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.

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 😂

C (gcc), 16 15 bytes

g;f(d){g=d>>9;}

Try it online!

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.

Python 3, 15 bytes

f=lambda n:n//1

Try it online!

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)

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

Try it online!

or alternatively using the bitwise "OR" operator:

x=>x|0

Try it online!

Turing Machine Code, 39 bytes

0 * * r 0
0 _ _ l 1
1 * _ l 1
1 . _ l 2

Try it online!

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|

Try it online!

 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

Keg, 6 5 4 bytes

:1%-

Try it online!

Look ma, no unicode!

A port of the RProgN2 answer

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!

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) );
  }
}

RProgN, 1 byte

_

A builtin, thanks to the new challenge spec.

Try it online!

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)

Try It Online

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.

Try it online!

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;}