diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-10 15:03:16 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-22 10:12:59 +0100 |
commit | 79f1dddf8958a65ce57ac4601a36289019b5f384 (patch) | |
tree | 24b706c3a91d14e928a843c410f2c69fe8514aa2 | |
parent | 678bb5c2500be0071117600f23e5a557c39fb403 (diff) |
Added round function
-rwxr-xr-x | dataType.ml | 28 | ||||
-rwxr-xr-x | dataType.mli | 1 | ||||
-rwxr-xr-x | evaluator.ml | 5 | ||||
-rwxr-xr-x | evaluator.mli | 2 | ||||
-rwxr-xr-x | readme.rst | 121 | ||||
-rwxr-xr-x | tests/dataType_test.ml | 49 |
6 files changed, 128 insertions, 78 deletions
diff --git a/dataType.ml b/dataType.ml index b5e077e..f30dd8c 100755 --- a/dataType.ml +++ b/dataType.ml @@ -27,6 +27,8 @@ module Num = struct include Q
+ let is_integer t = (Q.den t) == Z.one
+
let eq = Q.equal
let neq a b = not (Q.equal a b)
@@ -36,12 +38,32 @@ module Num = struct let floor t =
let num = Q.num t
and den = Q.den t in
- Q.of_bigint @@ Z.fdiv num den
+
+ if is_integer t then
+ Q.of_bigint num
+ else
+ Q.of_bigint @@ Z.fdiv num den
let round_down t =
let num = Q.num t
and den = Q.den t in
- Q.of_bigint @@ Z.div num den
+
+ if is_integer t then
+ Q.of_bigint num
+ else
+ Q.of_bigint @@ Z.div num den
+
+ let round t =
+ if is_integer t then
+ t
+ else
+ let t' = match Q.sign t with
+ | 1 -> Q.add t @@ Q.of_ints 1 2
+ | -1 -> Q.add t @@ Q.of_ints (-1) 2
+ | _ -> t in
+ let num = Q.num t'
+ and den = Q.den t' in
+ Q.of_bigint (Z.div num den)
let ge = Q.geq
@@ -49,8 +71,6 @@ module Num = struct let le = Q.leq
- let is_integer t = (Q.den t) == Z.one
-
let pow t q_factor = begin
if is_integer q_factor then
diff --git a/dataType.mli b/dataType.mli index 2589e68..5c89c98 100755 --- a/dataType.mli +++ b/dataType.mli @@ -43,6 +43,7 @@ module Num: sig val abs: t -> t
+ val round: t -> t
val floor: t -> t
val round_down: t -> t
diff --git a/evaluator.ml b/evaluator.ml index d9fa754..12d60b8 100755 --- a/evaluator.ml +++ b/evaluator.ml @@ -82,9 +82,7 @@ module C = Catalog.Make(Data) type t = C.t
-let (catalog:C.t ref) = ref C.empty
-
-let get_catalog () = !catalog
+let catalog = C.empty
let repr = C.repr
@@ -310,6 +308,7 @@ let () = begin register1 "abs" t_int f_number D.Num.abs;
register1 "int" t_int f_number D.Num.floor;
register1 "rounddown" t_int f_number D.Num.round_down;
+ register1 "round" t_int f_number D.Num.round;
let module CompareBool = Make_Compare(D.Bool) in
diff --git a/evaluator.mli b/evaluator.mli index b296b90..ce5db7b 100755 --- a/evaluator.mli +++ b/evaluator.mli @@ -4,8 +4,6 @@ val eval: (ScTypes.refs -> ScTypes.result option ScTypes.Refs.range) -> ScTypes. val repr: Format.formatter -> t -> unit
-val get_catalog: unit -> t
-
(** Type definitions *)
type 'a typ
@@ -9,40 +9,15 @@ Compilation =========== -licht requires ocaml >= 4.03 and ncurses +licht requires ocaml 4.04 and ncurses .. code-block:: console # sudo aptitude install opam libncures-dev libiconv-dev - $ opam install ocamlbuild curses camlzip ezxmlm ounit text menhir calendar + $ opam install ocamlbuild curses camlzip ezxmlm ounit text menhir zarith $ make -Tester avec un encoding non UTF_8 -================================= - - -Lancement du terminal avec l'encoding - -.. code-block:: console - - LANG=fr_FR.iso8859-1 xterm -en iso8859-1 - - -Définir l'encoding suivant : - -.. code-block:: console - - export LC_ALL=fr_FR.iso8859-1 - -Pour la définir avec rxvt : - -.. code-block:: console - - export LC_CTYPE=fr_FR.ISO8859-1 - export LANG=fr_FR.iso8859-1 - printf "\33]701;$LC_CTYPE\007" - ===== Usage ===== @@ -156,8 +131,8 @@ Undefined If a reference point to an an empty cell, the content will be interpreted as Undefined. Any formula impliyng Undefined will return Error -Formulas -======== +Functions +========= You can enter a formula by starting the value with `=`. The evaluation always expands the reference to other cells. @@ -183,14 +158,26 @@ Function Value Boolean ------- -=============== =============================== -Function Value -=============== =============================== -`true()` True -`false()` False -`not(Bool)` True if the parameter is False, - False if the parameter is True -=============== =============================== +================= =============================== +Function Value +================= =============================== +`true()` True +`false()` False +`not(Bool)` True if the parameter is False, + False if the parameter is True +`and(Bool; Bool)` Logical and +`or(Bool; Bool)` Logical or +`xor(Bool; Bool)` Logical exclusive or +================= =============================== + +Condition +~~~~~~~~~ + +========================= =========================================== +Function Value +========================= =========================================== +`if(Bool`; *x* ; *y* `)` Return *x* if `Bool` is Thue, otherwise *y* +========================= =========================================== Numeric ------- @@ -198,8 +185,62 @@ Numeric =================== ===================================== Function Value =================== ===================================== -*x* `+` *y* Add two values -*x* `**` *y* Compute *x* ^ *y* -`sum(Numeric List)` Compute the sum of the list. +*x* `+` *y* Addition +*x* `-` *y* Difference +*x* `*` *y* Multiplication +*x* `/` *y* Division +*x* `^` *y* Compute *x* ^ *y* +`gcd(` *x*; *y* `)` Greatest common divisor +`lcm(` *x*; *y* `)` Lowest common multiple `rnd()` A random number between 0 and 1 +`sqrt(Numeric)` Square root +`exp(Numeric)` Exponential +`ln(Numeric)` Natural logarithm +`abs(Numeric)` Absolute value =================== ===================================== + +Rounding +~~~~~~~~ + +The table show the differents results for the rounding functions + +===================== === === ==== ==== +Function 1.2 1.8 -1.2 -1.8 +===================== === === ==== ==== +`int(Numeric)` 1 1 -2 -2 +`rounddown(Numeric)` 1 1 -1 -1 +`round(Numeric)` 1 2 -1 -2 +===================== === === ==== ==== + +Operations on lists +~~~~~~~~~~~~~~~~~~~ + +=================== ===================================== +Function Value +=================== ===================================== +`sum(Numeric List)` Compute the sum of the list. +`min(Numeric List)` Get the minimum value +`max(Numeric List)` Get the maximum value +=================== ===================================== + +Trigonometry +~~~~~~~~~~~~ + +All the trigonometry functions works in radian. + +===================== ===================================== +Function Value +===================== ===================================== +`pi()` Get the value for :math:`\pi` +`cos(Numeric)` Get the cosine for the value +`sin(Numeric)` Get the sine for the value +`tan(Numeric)` Get the tangent for the value +`acos(Numeric)` Arc cosine +`asin(Numeric)` Arc sine +`atan(Numeric)` Arc tangent +`cosh(Numeric)` Hyperbolic cosine +`sinh(Numeric)` Hyperbolic sine +`tanh(Numeric)` Hyperbolic tangent +`atan2(` *x*; *y* `)` Arc tangent of *x* / *y* +===================== ===================================== + diff --git a/tests/dataType_test.ml b/tests/dataType_test.ml index 23ecf5d..3900f28 100755 --- a/tests/dataType_test.ml +++ b/tests/dataType_test.ml @@ -22,46 +22,37 @@ let test_num_sub n1 n2 result ctx = begin (N.to_int @@ N.sub n1 n2)
end
-let test_floor n1 expected ctx = begin
-
- let result = N.to_int @@ N.floor n1 in
+let test_round func number expected ctx = begin
+ let result = N.to_int @@ func number in
assert_equal
~msg:(Printf.sprintf "Expected %d but got %d" expected result)
~cmp:(=)
expected
result
-
end
-let test_round_down n1 expected ctx = begin
-
- let result = N.to_int @@ N.round_down n1 in
-
- assert_equal
- ~msg:(Printf.sprintf "Expected %d but got %d" expected result)
- ~cmp:(=)
- expected
- result
-
-end
-
-
-
let n1 = N.of_int 1
let n2 = N.of_int 2
let num_tests = "num_test">::: [
- "test_add" >:: test_num_add n1 n1 2;
- "test_mult" >:: test_num_mult n2 n1 2;
- "test_sub" >:: test_num_sub n1 n1 0;
- "test_floor1" >:: test_floor (N.of_float 1.2) 1;
- "test_floor2" >:: test_floor (N.of_float (-1.2)) (-2);
- "test_floor3" >:: test_floor (N.of_float 1.8) 1;
- "test_floor4" >:: test_floor (N.of_float (-1.8)) (-2);
- "test_round_down1" >:: test_round_down (N.of_float 1.2) 1;
- "test_round_down2" >:: test_round_down (N.of_float (-1.2)) (-1);
- "test_round_down3" >:: test_round_down (N.of_float 1.8) 1;
- "test_round_down4" >:: test_round_down (N.of_float (-1.8)) (-1);
+ "test_add" >:: test_num_add n1 n1 2;
+ "test_mult" >:: test_num_mult n2 n1 2;
+ "test_sub" >:: test_num_sub n1 n1 0;
+
+ "test_floor1" >:: test_round N.floor (N.of_float 1.2) 1;
+ "test_floor2" >:: test_round N.floor (N.of_float (-1.2)) (-2);
+ "test_floor3" >:: test_round N.floor (N.of_float 1.8) 1;
+ "test_floor4" >:: test_round N.floor (N.of_float (-1.8)) (-2);
+
+ "test_round_down1" >:: test_round N.round_down (N.of_float 1.2) 1;
+ "test_round_down2" >:: test_round N.round_down (N.of_float (-1.2)) (-1);
+ "test_round_down3" >:: test_round N.round_down (N.of_float 1.8) 1;
+ "test_round_down4" >:: test_round N.round_down (N.of_float (-1.8)) (-1);
+
+ "test_round" >:: test_round N.round (N.of_float 1.2) 1;
+ "test_round" >:: test_round N.round (N.of_float (-1.2)) (-1);
+ "test_round" >:: test_round N.round (N.of_float 1.8) 2;
+ "test_round" >:: test_round N.round (N.of_float (-1.8)) (-2);
]
|