# Floor implementation

Target: Write a floor() function from scratch.

Manipulating like converting float to string, and cutting all after dot are allowed.

All languages are allowed.

Any usage of built in language functions (like toInteger()) or similar is not allowed, casting operators are not allowed.

So, your task is implement floor() function without using casting and built in language functions.

Your function must support at least max 32-bit integer values (you will never receive negative values).

Forced casting (like somevar as float) and normal casting - such as(int) somevar - is not allowed, any built in functions, not allowed, all other stuff allowed.

Winner: Shortest implemention.

• This is severely underspecified. 1. Which input range do we have to support. In particular, can the input be negative? 2. What exactly does built in language functions(like tointeger()) or something like this mean? I assume casting to integer is forbidden as well, but what about operations that simply return an integer (e.g., integer division by 1)? 3. C returns an integral floating point number, not an integer. Any hypothetical tointeger() would probably do something else. – Dennis May 22 '17 at 19:25
• @Dennis, sorry for underspecifies. I fix some problems of question – user64239 May 22 '17 at 19:40
• Casting is still unclear. Unless we are forced to return an integral float, we'll have to cast at some point. Also, does max int32 values refer to the maximal signed or the maximal unsigned integer? It might be clearer if you just state the limits in decimal. – Dennis May 22 '17 at 19:43
• @Dennis, fixed all – user64239 May 22 '17 at 19:54
• Can the result have a leading dot and zeroes (eg 5.67 -> 5.00) – dzaima May 24 '17 at 19:52

# C (gcc), 57 bytes

i;int f(float n){for(i=-2147483648;i<=n;i++);return i-1;}


Try it online! (it takes too long, so I changed it to start from 0.

• I think you could save a byte by using 0x80000000 instead of -2147483648 – DJMcMayhem May 22 '17 at 19:26
• Nice work. It working and seems interesting. – user64239 May 22 '17 at 19:32
• You could also do i++<=n; instead – DJMcMayhem May 22 '17 at 20:03
• negative values not important, so is' default can be 0 – dzaima May 22 '17 at 20:21
• You can remove the int return type from the function definition - the compiler will default to int if no other type is given. – Digital Trauma May 22 '17 at 20:24

# CJam, 6 bytes

q'.%0=


Try it online!

I found several 6 bytes solution but I can't do less than this.

• "Manipulating like converting float to string, and cutting all after dot are allowed." – Leaky Nun May 22 '17 at 19:14
• @LeakyNun "are allowed" means that I can do that, doesn't it? – FrodCube May 22 '17 at 19:16
• Oops, I misread it... – Leaky Nun May 22 '17 at 19:16
• "Any usage of built in language functions (like toInteger()) or similar is not allowed" – user64239 May 22 '17 at 19:30
• @monobogdan I haven't used any of those. I'm reading the input q, splitting where the point is '.% and taking the part before the point 0= – FrodCube May 22 '17 at 19:49

# Javascript, 6 chars

x=>~~x


Test:

f = x=>~~x

document.addEventListener('input', event => {
var x = event.target.valueAsNumber
document.querySelector('output').textContent = x + " => " + f(x)
})
<input type=number> <output></output>

# Python 2, 21 13 bytes

lambda x:x//1


Outputs a float if input is float, outputs an integer if input is integer.

Try it online!

# Japt, 2 bytes

|0


Try it online

## Alternative

ÂU


Try it online

# Batch, 53 bytes

@for /f "tokens=1 delims=." %%a in ("%1")do @echo %%a


Trims everything after .