[erlang-questions] casting float to integer
Robert Virding
robert.virding@REDACTED
Sat May 12 12:36:56 CEST 2007
Can't you instead just have:
floor(V) when V >= 0 -> trunc(V);
floor(V) -> - trunc(-V) - 1.
Robert
Per Hedeland wrote:
> Håkan Stenholm <hokan.stenholm@REDACTED> wrote:
>> Nitin Verma wrote:
>>> Hi All,
>>>
>>> I am looking for a function/BIF that can help me.
>>>
>>> Problem: I need a float number's floored integer. That is if:
>>> A = f(5/2), then A = 2 and
>>> A = f(1000/501), then A = 1
>>>
>>> I found 'round/1' but it just rounds-off .
>
>> see erlang.erl documentation:
>>
>> -----------------------------------------------------------------------
>>
>> *|trunc(Number) -> int()|*
>
> But trunc is not the same as floor - though maybe Nitin actually wants
> trunc, or even div (which "truncates" the same way as trunc), as
> indicated by the examples. When I actually needed floor/1, I came up
> with this somewhat bizarre-looking definition:
>
> floor(V) when V >= 0 -> trunc(V);
> floor(V) -> trunc(V - trunc(V) + 1) + trunc(V) - 1.
>
> There may be a better way to do it...
>
> --Per Hedeland
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list