[erlang-questions] Floor Function?

Bob Ippolito <>
Mon Mar 16 06:50:38 CET 2009


On Sun, Mar 15, 2009 at 9:10 PM, Richard O'Keefe <> wrote:
>
> On 14 Mar 2009, at 7:17 am, Bob Ippolito wrote:
>>
>> int_ceil(X) ->
>>   T = trunc(X),
>>   case (X - T) of
>>       Neg when Neg < 0 -> T;
>>       Pos when Pos > 0 -> T + 1;
>>       _ -> T
>>   end.
>
> Why isn't this
>
> int_ceil(X) ->
>    T = trunc(X),
>    if X > T -> T + 1
>     ; true  -> T
>    end.
>
> int_floor(X) ->
>    T = trunc(X),
>    if X < T -> T - 1
>     ; true  -> T
>    end.
>
> There doesn't seem to be any reason to compute X-T.

I didn't say it was optimal, it's just an implementation. It isn't
something we use very often, so we didn't spend much time on it
besides verify that it was implemented correctly.

-bob



More information about the erlang-questions mailing list