Source file modulo__Divisible.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
exception FinitePositiveExpected

let round_down_to (a: Q.t) (m: Q.t) : Q.t =
  if (Q.equal a (Q.of_bigint Z.zero))
  then a
  else
    begin
      let q' = Q__Q.floor (Q.(/) a m) in
      let r = (Q.( * ) (Q.of_bigint q') m) in r end

let round_up_to (a: Q.t) (m: Q.t) : Q.t =
  let res = round_down_to (Q.(-) (Q.of_bigint Z.zero) a) m in
  (Q.(-) (Q.of_bigint Z.zero) res)

let divisible (a: Q.t) (b: Q.t) : bool =
  let b1 = (Z.divisible (a.Q.num) (b.Q.num)) in
  if b1
  then
    let b2 = (Z.divisible (b.Q.den) (a.Q.den)) in if b2 then true else false
  else false

let mult_cst_divisible (a: Q.t) (q: Q.t) : Q.t = (Q.( * ) a q)

let union_divisible (a: Q.t) (b: Q.t) : Q.t =
  let n = (Z.gcd (a.Q.num) (b.Q.num)) in
  let d = (Z.lcm (a.Q.den) (b.Q.den)) in
  ({ Q.num = n; Q.den = d })

let inter_divisible (a: Q.t) (b: Q.t) : Q.t =
  let n = (Z.gcd (a.Q.den) (b.Q.den)) in
  let d = (Z.lcm (a.Q.num) (b.Q.num)) in
  ({ Q.num = d; Q.den = n })