Floating Point Rounding

From Erlang Community

(Difference between revisions)
Revision as of 23:01, 3 September 2006 (edit)
Bfulgham (Talk | contribs)

← Previous diff
Revision as of 15:17, 24 September 2006 (edit) (undo)
Ayrnieu (Talk | contribs)
(these BIFs don't need erlang: ; change the tests a bit; remove bogus scheme reference to inexactness; remove redundant 'see also'; minor language)
Next diff →
Line 5: Line 5:
== Solution == == Solution ==
-Use one of the procedures erlang:round, ceiling, floor and erlang:trunc. Note, the standard Erlang distribution does not come with either floor or ceiling, but they can be easily implemented in terms of erlang:trunc: +Use one of the functions round/1, ceiling/1, floor/1 and trunc/1. Note, the standard Erlang distribution does not come with either floor/1 or ceiling/1, but they can be easily implemented in terms of trunc/1
<code> <code>
floor(X) -> floor(X) ->
- T = erlang:trunc(X),+ T = trunc(X),
- case (X - T) of+ case X - T < 0 of
- Neg when Neg < 0 -> T - 1;+ true -> T - 1;
- Pos when Pos > 0 -> T;+ false -> T
- _ -> T+
end. end.
ceiling(X) -> ceiling(X) ->
- T = erlang:trunc(X),+ T = trunc(X),
- case (X - T) of+ case X - T < 0 of
- Neg when Neg < 0 -> T;+ true -> T;
- Pos when Pos > 0 -> T + 1;+ false -> T + 1
- _ -> T+
end. end.
Line 28: Line 26:
2> ceiling(-4.3). 2> ceiling(-4.3).
-4 -4
-3> erlang:trunc(-4.3).+3> trunc(-4.3).
-4 -4
-4> erlang:round(-4.3).+4> round(-4.3).
-4 -4
5> floor(3.5). 5> floor(3.5).
Line 36: Line 34:
6> ceiling(3.5). 6> ceiling(3.5).
4 4
-7> erlang:trunc(3.5).+7> trunc(3.5).
3 3
-8> erlang:round(3.5).+8> round(3.5).
4 4
-9> erlang:round(7).+9> round(7).
7 7
</code> </code>
Line 46: Line 44:
The procedures all return integers. The procedures all return integers.
-erlang:round returns the closest integer to x, rounding to even when x is halfway between two integers. +round/1 returns the closest integer to x, rounding to even when x is halfway between two integers.
-floor returns the largest integer not larger than x. +floor/1 returns the largest integer not larger than x.
-ceiling returns the smallest integer not smaller than x. +ceiling/1 returns the smallest integer not smaller than x.
-erlang:trunc returns the integer closest to x whose absolute value is not larger than the absolute value of x. +trunc/1 returns the integer closest to x whose absolute value is not larger than the absolute value of x.
- +
-If the number given to one of the procedures is inexact?, then the result will also be inexact. +
- +
-See Also +
Further general information on math and rounding is available from: Further general information on math and rounding is available from:

Revision as of 15:17, 24 September 2006

Problem

You need to round a floating-point number to an integer.

Solution

Use one of the functions round/1, ceiling/1, floor/1 and trunc/1. Note, the standard Erlang distribution does not come with either floor/1 or ceiling/1, but they can be easily implemented in terms of trunc/1

floor(X) ->
    T = trunc(X),
    case X - T < 0 of
        true -> T - 1;
        false -> T
    end.

ceiling(X) ->
    T = trunc(X),
    case X - T < 0 of
        true -> T;
        false -> T + 1
    end.

1> floor(-4.3).
-5
2> ceiling(-4.3).
-4
3> trunc(-4.3).
-4
4> round(-4.3).
-4
5> floor(3.5).
3
6> ceiling(3.5).
4
7> trunc(3.5).
3
8> round(3.5).
4
9> round(7).
7

The procedures all return integers.

round/1 returns the closest integer to x, rounding to even when x is halfway between two integers. floor/1 returns the largest integer not larger than x. ceiling/1 returns the smallest integer not smaller than x. trunc/1 returns the integer closest to x whose absolute value is not larger than the absolute value of x.

Further general information on math and rounding is available from:

MathWorld definition of the Floor Function. MathWorld definition of the Ceiling Function

Erlang/OTP Projects
Personal tools