Source file CCFloat.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
(* This file is free software, part of containers. See file "license" for more details. *)

include Float

module Infix = struct
  let ( = ) : t -> t -> bool = Stdlib.( = )
  let ( <> ) : t -> t -> bool = Stdlib.( <> )
  let ( < ) : t -> t -> bool = Stdlib.( < )
  let ( > ) : t -> t -> bool = Stdlib.( > )
  let ( <= ) : t -> t -> bool = Stdlib.( <= )
  let ( >= ) : t -> t -> bool = Stdlib.( >= )
  let ( ~- ) : t -> t = Stdlib.( ~-. )
  let ( + ) : t -> t -> t = Stdlib.( +. )
  let ( - ) : t -> t -> t = Stdlib.( -. )
  let ( * ) : t -> t -> t = Stdlib.( *. )
  let ( / ) : t -> t -> t = Stdlib.( /. )
end

include Infix

[@@@ocaml.warning "-32"]

let max_value = infinity
let min_value = neg_infinity
let max_finite_value = Stdlib.max_float
let scale = ( *. )

[@@@ocaml.warning "+32"]

type 'a printer = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a

let pp = Format.pp_print_float

let fsign a =
  if is_nan a then
    nan
  else if a = 0. then
    a
  else
    Stdlib.copysign 1. a

exception TrapNaN of string

let sign_exn (a : float) =
  if is_nan a then
    raise (TrapNaN "sign_exn")
  else
    compare a 0.

let of_string_exn (a : string) = Stdlib.float_of_string a
let random n st = Random.State.float st n
let random_small = random 100.0
let random_range i j st = i +. random (j -. i) st
let equal_precision ~epsilon a b = abs_float (a -. b) < epsilon
let classify = Stdlib.classify_float