package prbnmcn-basic-structures

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file reals.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Float : Basic_intf.Reals with type t = float = struct
  include Float

  let pp = Format.pp_print_float

  let of_int = float_of_int

  let lebesgue rng_state = Random.State.float rng_state 1.0 [@@inline]

  let npow x n = x ** of_int n

  let neg x = ~-.x [@@inline]

  let ( / ) = ( /. )

  let ( * ) = ( *. )

  let ( - ) = ( -. )

  let ( + ) = ( +. )

  let ( < ) (x : float) (y : float) = x < y [@@inline]

  let ( <= ) (x : float) (y : float) = x <= y [@@inline]

  let ( > ) (x : float) (y : float) = x > y [@@inline]

  let ( >= ) (x : float) (y : float) = x >= y [@@inline]

  let ( = ) (x : float) (y : float) = x = y [@@inline]

  let ( <> ) (x : float) (y : float) = x <> y [@@inline]

  let one = 1.0

  let to_float x = x [@@inline]

  let of_float x = x [@@inline]
end

module Rational : Basic_intf.Reals with type t = Q.t = struct
  let lebesgue rng_state = Q.of_float (Random.State.float rng_state 1.0)

  let rec qpow x n =
    if n < 0 then invalid_arg "pow"
    else if n = 0 then Q.one
    else if n = 1 then x
    else
      let p = qpow x (n / 2) in
      if n mod 2 = 0 then Q.mul p p else Q.(mul x (mul p p))

  let npow x n =
    if n = 0 then Q.one
    else if n > 0 then qpow x n
    else Q.(div one (qpow x (Int.neg n)))

  include Q

  let hash = Hashtbl.hash

  let pp = Q.pp_print
end