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.


Posted 2017-05-22T19:04:31.273


2This 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 – 2017-05-22T19:25:46.643

@Dennis, sorry for underspecifies. I fix some problems of question – None – 2017-05-22T19:40:10.543

4Casting 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 – 2017-05-22T19:43:22.203

@Dennis, fixed all – None – 2017-05-22T19:54:11.387

Can the result have a leading dot and zeroes (eg 5.67 -> 5.00) – dzaima – 2017-05-24T19:52:28.557



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.

Leaky Nun

Posted 2017-05-22T19:04:31.273

Reputation: 45 011

I think you could save a byte by using 0x80000000 instead of -2147483648 – James – 2017-05-22T19:26:27.817

Nice work. It working and seems interesting. – None – 2017-05-22T19:32:14.100

You could also do i++<=n; instead – James – 2017-05-22T20:03:34.433

negative values not important, so is' default can be 0 – dzaima – 2017-05-22T20:21:50.317

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 – 2017-05-22T20:24:50.117

1<<32-1 == -2147483648 – Digital Trauma – 2017-05-22T20:28:07.660

Or even shorter: 5<<31 – Dada – 2017-05-24T11:44:33.403


CJam, 6 bytes


Try it online!

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


Posted 2017-05-22T19:04:31.273

Reputation: 539

"Manipulating like converting float to string, and cutting all after dot are allowed." – Leaky Nun – 2017-05-22T19:14:25.540

1@LeakyNun "are allowed" means that I can do that, doesn't it? – FrodCube – 2017-05-22T19:16:17.777

Oops, I misread it... – Leaky Nun – 2017-05-22T19:16:40.463

"Any usage of built in language functions (like toInteger()) or similar is not allowed" – None – 2017-05-22T19:30:54.203

1@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 – 2017-05-22T19:49:15.590


Javascript, 6 chars



f = x=>~~x

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


Posted 2017-05-22T19:04:31.273

Reputation: 2 697


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!

Felipe Nardi Batista

Posted 2017-05-22T19:04:31.273

Reputation: 2 345


Japt, 2 bytes


Try it online



Try it online


Posted 2017-05-22T19:04:31.273

Reputation: 24 623


Batch, 53 bytes

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

Trims everything after .


Posted 2017-05-22T19:04:31.273

Reputation: 539