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