From 8841e36296a3ebdccfcb4b334bc9407c585cd67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Hedenstro=CC=88m?= <erik@erlang.ninja> Date: Fri, 18 Mar 2016 12:10:42 +0100 Subject: [PATCH] Added math module --- rebar.config | 2 ++ src/tsuru.app.src | 2 +- src/tsuru_math.erl | 23 +++++++++++++++++++++++ test/tsuru_math_test.erl | 13 +++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/tsuru_math.erl create mode 100644 test/tsuru_math_test.erl diff --git a/rebar.config b/rebar.config index 009fb75..526bffc 100644 --- a/rebar.config +++ b/rebar.config @@ -1,3 +1,5 @@ +{global_rebar_dir, ".rebar3"}. + {relx, [ {release, {tsuru, semver}, [tsuru]}, {dev_mode, true}, diff --git a/src/tsuru.app.src b/src/tsuru.app.src index 0669c25..aca9ef5 100644 --- a/src/tsuru.app.src +++ b/src/tsuru.app.src @@ -1,5 +1,5 @@ {application, tsuru, [{description, "A collection of useful tools for Erlang applications"}, - {vsn, "1.2.1"}, + {vsn, "1.3.0"}, {applications, [kernel, stdlib]}, {modules, []}, {registered, []}, diff --git a/src/tsuru_math.erl b/src/tsuru_math.erl new file mode 100644 index 0000000..3c6492c --- /dev/null +++ b/src/tsuru_math.erl @@ -0,0 +1,23 @@ +-module(tsuru_math). + +-export([floor/1, ceiling/1]). + +-spec floor(number()) -> integer(). +floor(X) when X < 0 -> + T = trunc(X), + case X - T == 0 of + true -> T; + false -> T - 1 + end; +floor(X) -> + trunc(X). + +-spec ceiling(number()) -> integer(). +ceiling(X) when X < 0 -> + trunc(X); +ceiling(X) -> + T = trunc(X), + case X - T == 0 of + true -> T; + false -> T + 1 + end. diff --git a/test/tsuru_math_test.erl b/test/tsuru_math_test.erl new file mode 100644 index 0000000..4612294 --- /dev/null +++ b/test/tsuru_math_test.erl @@ -0,0 +1,13 @@ +-module(tsuru_math_test). + +-include_lib("eunit/include/eunit.hrl"). + +floor_test() -> + ?assertEqual(-5, tsuru_math:floor(-5.0)), + ?assertEqual(2, tsuru_math:floor(2.3)), + ?assertEqual(-4, tsuru_math:floor(-3.9)). + +ceiling_test() -> + ?assertEqual(5, tsuru_math:ceiling(5.0)), + ?assertEqual(3, tsuru_math:ceiling(2.3)), + ?assertEqual(-3, tsuru_math:ceiling(-3.9)). -- GitLab