Floating Point Rounding

From Erlang Community

(Difference between revisions)
Revision as of 16:43, 3 December 2006 (edit)
Kaiserpanda (Talk | contribs)
m
← Previous diff
Current revision (19:25, 20 November 2009) (edit) (undo)
Seetho (Talk | contribs)

 
(2 intermediate revisions not shown.)
Line 8: Line 8:
<code> <code>
-floor(X) ->+floor(X) when X < 0 ->
T = trunc(X), T = trunc(X),
- case X - T < 0 of+ case X - T == 0 of
- true -> T - 1;+ true -> T;
- false -> T+ false -> T - 1
- end.+ end;
 +floor(X) ->
 + trunc(X).
 + 
 +ceiling(X) when X < 0 ->
 + trunc(X);
ceiling(X) -> ceiling(X) ->
T = trunc(X), T = trunc(X),
- case X - T < 0 of+ case X - T == 0 of
true -> T; true -> T;
false -> T + 1 false -> T + 1
end. end.
 +
1> floor(-4.3). 1> floor(-4.3).
Line 44: Line 50:
The procedures all return integers. The procedures all return integers.
-round/1 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.
 +trunc/1 returns the integer closest to x whose absolute value is not larger than the absolute value of x.
floor/1 returns the largest integer not larger than x. floor/1 returns the largest integer not larger than x.
ceiling/1 returns the smallest integer not smaller 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. + 
 +For the floor and ceiling functions implemented above, T will always be less than or equal to X. We just need to check if there is a difference between the two in which case we will either increase or decrease the truncation depending if we want to floor or ceiling X.
 + 
 +''(Correction to floor/1 and ceiling/1 to check for negative or positive values. 2009-11-21 seetho)''
Further general information on math and rounding is available from: Further general information on math and rounding is available from:
Line 56: Line 66:
[[Category:CookBook]][[Category:NumberRecipes]] [[Category:CookBook]][[Category:NumberRecipes]]
- 
- 
- 
-[http://www.casino-web-gambling.com/blackjack-tips/blackjack-hints.html blackjack hints] 
-[http://www.gambling-online-theory.com/online-casino/online-casino-for-cash.html online casino for cash] 
-[http://www.casino-games-wiki.com/index.php/casino_games_online casino games online] 
-[http://www.casino-theory.com/online-casino-bonus/free-online-casino-tournament.html free online casino tournament] 
-[http://www.casino-theory.com/online-casino-bonus/free-online-casino-tournament.html free online casino tournament] 
-[http://www.gambling-online-theory.com/casinos-portal/internet-casinos-sites.html internet casinos sites] 
-[http://www.casino-games-wiki.com/index.php/play_casino_games_games_online play casino games games online] 
-[http://www.casinos-go.com/online-casino-tips/index.html online casino tips] 
-[http://www.gambling-online-theory.com/casinos-portal/index.html casinos portal] 
-[http://www.gambling-online-theory.com/casinos-portal/casinos-games.html casinos games] 

Current revision

[edit] Problem

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

[edit] 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) when X < 0 ->
    T = trunc(X),
    case X - T == 0 of
        true -> T;
        false -> T - 1
    end;
floor(X) -> 
    trunc(X).


ceiling(X) when X < 0 ->
    trunc(X);
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. trunc/1 returns the integer closest to x whose absolute value is not larger than the absolute value of x. floor/1 returns the largest integer not larger than x. ceiling/1 returns the smallest integer not smaller than x.

For the floor and ceiling functions implemented above, T will always be less than or equal to X. We just need to check if there is a difference between the two in which case we will either increase or decrease the truncation depending if we want to floor or ceiling X.

(Correction to floor/1 and ceiling/1 to check for negative or positive values. 2009-11-21 seetho)

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