-6
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis May 22 '17 at 19:25
  • \$\begingroup\$ @Dennis, sorry for underspecifies. I fix some problems of question \$\endgroup\$ – user64239 May 22 '17 at 19:40
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis May 22 '17 at 19:43
  • \$\begingroup\$ @Dennis, fixed all \$\endgroup\$ – user64239 May 22 '17 at 19:54
  • \$\begingroup\$ Can the result have a leading dot and zeroes (eg 5.67 -> 5.00) \$\endgroup\$ – dzaima May 24 '17 at 19:52
1
\$\begingroup\$

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.

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

CJam, 6 bytes

q'.%0=

Try it online!

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

\$\endgroup\$
  • \$\begingroup\$ "Manipulating like converting float to string, and cutting all after dot are allowed." \$\endgroup\$ – Leaky Nun May 22 '17 at 19:14
  • 1
    \$\begingroup\$ @LeakyNun "are allowed" means that I can do that, doesn't it? \$\endgroup\$ – FrodCube May 22 '17 at 19:16
  • \$\begingroup\$ Oops, I misread it... \$\endgroup\$ – Leaky Nun May 22 '17 at 19:16
  • \$\begingroup\$ "Any usage of built in language functions (like toInteger()) or similar is not allowed" \$\endgroup\$ – user64239 May 22 '17 at 19:30
  • 1
    \$\begingroup\$ @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= \$\endgroup\$ – FrodCube May 22 '17 at 19:49
1
\$\begingroup\$

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>

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

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!

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

Japt, 2 bytes

|0

Try it online


Alternative

ÂU

Try it online

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

Batch, 53 bytes

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

Trims everything after .

\$\endgroup\$

Your Answer

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