diff --git a/src/core/CCFloat.ml b/src/core/CCFloat.ml index 7b62f832..72c241bb 100644 --- a/src/core/CCFloat.ml +++ b/src/core/CCFloat.ml @@ -1,13 +1,6 @@ (* This file is free software, part of containers. See file "license" for more details. *) -type t = float - -type fpclass = Stdlib.fpclass = - | FP_normal - | FP_subnormal - | FP_zero - | FP_infinite - | FP_nan +include Float module Infix = struct let ( = ) : t -> t -> bool = Stdlib.( = ) @@ -27,47 +20,11 @@ include Infix [@@@ocaml.warning "-32"] -let nan = Stdlib.nan -let infinity = Stdlib.infinity -let neg_infinity = Stdlib.neg_infinity let max_value = infinity let min_value = neg_infinity let max_finite_value = Stdlib.max_float -let epsilon = Stdlib.epsilon_float -let pi = 0x1.921fb54442d18p+1 -let is_nan x = Stdlib.(classify_float x = Stdlib.FP_nan) -let add = ( +. ) -let sub = ( -. ) -let mul = ( *. ) -let div = ( /. ) -let neg = ( ~-. ) -let abs = Stdlib.abs_float let scale = ( *. ) -let min (x : t) y = - match Stdlib.classify_float x, Stdlib.classify_float y with - | FP_nan, _ -> y - | _, FP_nan -> x - | _ -> - if x < y then - x - else - y - -let max (x : t) y = - match Stdlib.classify_float x, Stdlib.classify_float y with - | FP_nan, _ -> y - | _, FP_nan -> x - | _ -> - if x > y then - x - else - y - -let equal (a : float) b = a = b -let hash : t -> int = Hashtbl.hash -let compare (a : float) b = Stdlib.compare a b - [@@@ocaml.warning "+32"] type 'a printer = Format.formatter -> 'a -> unit @@ -91,22 +48,7 @@ let sign_exn (a : float) = else compare a 0. -let round x = - let low = floor x in - let high = ceil x in - if x -. low > high -. x then - high - else - low - -let to_int (a : float) = Stdlib.int_of_float a -let of_int (a : int) = Stdlib.float_of_int a -let to_string (a : float) = Stdlib.string_of_float a let of_string_exn (a : string) = Stdlib.float_of_string a - -let of_string_opt (a : string) = - try Some (Stdlib.float_of_string a) with Failure _ -> None - 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 diff --git a/src/core/CCFloat.mli b/src/core/CCFloat.mli index 52709194..ee97e6d3 100644 --- a/src/core/CCFloat.mli +++ b/src/core/CCFloat.mli @@ -3,17 +3,8 @@ (** Basic operations on floating-point numbers @since 0.6.1 *) -type t = float - -type fpclass = Stdlib.fpclass = - | FP_normal - | FP_subnormal - | FP_zero - | FP_infinite - | FP_nan - -val nan : t -(** [nan] is Not a Number (NaN). Equal to {!Stdlib.nan}. *) +include module type of Float +(** @inline *) val max_value : t (** [max_value] is Positive infinity. Equal to {!Stdlib.infinity}. *) @@ -24,50 +15,13 @@ val min_value : t val max_finite_value : t (** [max_finite_value] is the largest finite float value. Equal to {!Stdlib.max_float}. *) -val epsilon : t -(** [epsilon] is the smallest positive float x such that [1.0 +. x <> 1.0]. - Equal to {!Stdlib.epsilon_float}. *) - -val pi : t -(** [pi] is the constant pi. The ratio of a circumference to its diameter. - @since 3.0 *) - -val is_nan : t -> bool -(** [is_nan f] returns [true] if f is NaN, [false] otherwise. *) - -val add : t -> t -> t -(** [add x y] is equal to [x +. y]. *) - -val sub : t -> t -> t -(** [sub x y] is equal to [x -. y]. *) - -val neg : t -> t -(** [neg x] is equal to [~-. x]. *) - -val abs : t -> t -(** [abs x] is the absolute value of the floating-point number [x]. - Equal to {!Stdlib.abs_float}. *) - val scale : t -> t -> t (** [scale x y] is equal to [x *. y]. *) -val min : t -> t -> t -(** [min x y] returns the min of the two given values [x] and [y]. *) - -val max : t -> t -> t -(** [max x y] returns the max of the two given values [x] and [y]. *) - -val equal : t -> t -> bool -(** [equal x y] is [true] if [x] and [y] are the same. *) - -val compare : t -> t -> int -(** [compare x y] is {!Stdlib.compare x y}. *) - type 'a printer = Format.formatter -> 'a -> unit type 'a random_gen = Random.State.t -> 'a val pp : t printer -val hash : t -> int val random : t -> t random_gen val random_small : t random_gen val random_range : t -> t -> t random_gen @@ -76,11 +30,6 @@ val fsign : t -> t (** [fsign x] is one of [-1., -0., +0., +1.], or [nan] if [x] is NaN. @since 0.7 *) -val round : t -> t -(** [round x] returns the closest integer value, either above or below. - For [n + 0.5], [round] returns [n]. - @since 0.20 *) - exception TrapNaN of string val sign_exn : t -> int @@ -89,23 +38,11 @@ val sign_exn : t -> int Note that infinities have defined signs in OCaml. @since 0.7 *) -val to_int : t -> int -(** Alias to {!int_of_float}. - Unspecified if outside of the range of integers. *) - -val of_int : int -> t -(** Alias to {!float_of_int}. *) - -val to_string : t -> string - val of_string_exn : string -> t (** Alias to {!float_of_string}. @raise Failure in case of failure. @since 1.2 *) -val of_string_opt : string -> t option -(** @since 3.0 *) - val equal_precision : epsilon:t -> t -> t -> bool (** Equality with allowed error up to a non negative epsilon value. *) diff --git a/tests/core/t_float.ml b/tests/core/t_float.ml index 0575b80c..20bdda47 100644 --- a/tests/core/t_float.ml +++ b/tests/core/t_float.ml @@ -2,10 +2,10 @@ open CCFloat module T = (val Containers_testlib.make ~__FILE__ ()) include T;; -t @@ fun () -> max nan 1. = 1.;; -t @@ fun () -> min nan 1. = 1.;; -t @@ fun () -> max 1. nan = 1.;; -t @@ fun () -> min 1. nan = 1.;; +t @@ fun () -> is_nan (max nan 1.);; +t @@ fun () -> is_nan (min nan 1.);; +t @@ fun () -> is_nan (max 1. nan);; +t @@ fun () -> is_nan (min 1. nan);; q Q.(pair float float)