# [erlang-questions] casting float to integer

Nitin Verma nitin.matrix@REDACTED
Mon May 14 19:02:49 CEST 2007

```Thanx for all the replies, appreciate it. I am very new to functional
programming, and to Erlang. This all helped me a lot.

I saw something very weird.

( 2^(n-1) – 1 ) / 2^n + 1/2 < 1
---

1> Tc=(math:pow(2,52) - 1) / math:pow(2,53).
0.500000
2> Td= Tc + 1/2.
1.000000
3> trunc(Td).
0

This is correct.
---

1> Tc=(math:pow(2,53) - 1) / math:pow(2,54).
0.500000
2> Td=Tc + 1/2.
1.00000
3> trunc(Td).
1

eeh, what happened here?

On 5/12/07, Per Hedeland <per@REDACTED> wrote:
> Robert Virding <robert.virding@REDACTED> wrote:
> >
> >Can't you instead just have:
> >
> >floor(V) when V >= 0 -> trunc(V);
> >floor(V) -> - trunc(-V) - 1.
>
> No, try floor(-1.0).
>
> --Per
>
> >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
>

```