diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-09 21:43:45 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-09 21:43:45 +0100 |
commit | 678bb5c2500be0071117600f23e5a557c39fb403 (patch) | |
tree | 8d8210bf9736cb5820fb6b2f83de32cbf6c708a9 | |
parent | 163a82655b1b3649c9bff4db05f487db3a992a40 (diff) |
Added some functions
-rwxr-xr-x | dataType.ml | 5 | ||||
-rwxr-xr-x | dataType.mli | 1 | ||||
-rwxr-xr-x | evaluator.ml | 3 | ||||
-rwxr-xr-x | tests/dataType_test.ml | 28 |
4 files changed, 32 insertions, 5 deletions
diff --git a/dataType.ml b/dataType.ml index 8e5ead0..b5e077e 100755 --- a/dataType.ml +++ b/dataType.ml @@ -38,6 +38,11 @@ module Num = struct and den = Q.den t in
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
+
let ge = Q.geq
let ge = Q.geq
diff --git a/dataType.mli b/dataType.mli index 5397fdb..2589e68 100755 --- a/dataType.mli +++ b/dataType.mli @@ -44,6 +44,7 @@ module Num: sig val abs: t -> t
val floor: t -> t
+ val round_down: t -> t
val gcd: t -> t -> t
val lcm: t -> t -> t
diff --git a/evaluator.ml b/evaluator.ml index 0681183..d9fa754 100755 --- a/evaluator.ml +++ b/evaluator.ml @@ -308,6 +308,9 @@ let () = begin register3 "if" (t_bool, t_string, t_string) f_string if_;
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;
+
let module CompareBool = Make_Compare(D.Bool) in
CompareBool.register t_bool;
diff --git a/tests/dataType_test.ml b/tests/dataType_test.ml index f015e89..23ecf5d 100755 --- a/tests/dataType_test.ml +++ b/tests/dataType_test.ml @@ -34,6 +34,18 @@ let test_floor n1 expected ctx = begin 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
@@ -41,9 +53,15 @@ 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_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);
]
|