aboutsummaryrefslogtreecommitdiff
path: root/dataType.ml
diff options
context:
space:
mode:
Diffstat (limited to 'dataType.ml')
-rwxr-xr-xdataType.ml28
1 files changed, 24 insertions, 4 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