mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
introduce CCFloat, add float functions to CCRandom
This commit is contained in:
parent
2ea9f04681
commit
88d266a8a8
5 changed files with 176 additions and 1 deletions
2
_oasis
2
_oasis
|
|
@ -46,7 +46,7 @@ Library "containers"
|
|||
Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap,
|
||||
CCMultiSet, CCBV, CCPrint, CCPersistentHashtbl, CCError,
|
||||
CCHeap, CCList, CCOpt, CCPair, CCFun, CCHash,
|
||||
CCKList, CCInt, CCBool, CCArray, CCOrd, CCIO,
|
||||
CCKList, CCInt, CCBool, CCFloat, CCArray, CCOrd, CCIO,
|
||||
CCRandom, CCKTree, CCTrie, CCString, CCHashtbl,
|
||||
CCFlatHashtbl, CCSexp, CCMap, CCCache
|
||||
BuildDepends: bytes
|
||||
|
|
|
|||
82
core/CCFloat.ml
Normal file
82
core/CCFloat.ml
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
(*
|
||||
copyright (c) 2013-2014, simon cruanes
|
||||
all rights reserved.
|
||||
|
||||
redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer. redistributions in binary
|
||||
form must reproduce the above copyright notice, this list of conditions and the
|
||||
following disclaimer in the documentation and/or other materials provided with
|
||||
the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*)
|
||||
|
||||
type t = float
|
||||
|
||||
|
||||
let nan = Pervasives.nan
|
||||
|
||||
let infinity = Pervasives.infinity
|
||||
let neg_infinity = Pervasives.neg_infinity
|
||||
|
||||
let max_value = infinity
|
||||
let min_value = neg_infinity
|
||||
|
||||
let max_finite_value = Pervasives.max_float
|
||||
|
||||
let is_nan x = (x : t) <> x
|
||||
|
||||
let add = (+.)
|
||||
let sub = (-.)
|
||||
let neg = (~-.)
|
||||
let abs = Pervasives.abs_float
|
||||
let scale = ( *. )
|
||||
|
||||
let min (x : t) y =
|
||||
if is_nan x || is_nan y then nan
|
||||
else if x < y then x else y
|
||||
|
||||
let max (x : t) y =
|
||||
if is_nan x || is_nan y then nan
|
||||
else if x > y then x else y
|
||||
|
||||
let equal (a:float) b = a=b
|
||||
|
||||
let hash = Hashtbl.hash
|
||||
let compare (a:float) b = Pervasives.compare a b
|
||||
|
||||
type 'a printer = Buffer.t -> 'a -> unit
|
||||
type 'a formatter = Format.formatter -> 'a -> unit
|
||||
type 'a random_gen = Random.State.t -> 'a
|
||||
|
||||
let pp buf = Printf.bprintf buf "%f"
|
||||
let print fmt = Format.pp_print_float fmt
|
||||
|
||||
let sign (a:float) =
|
||||
if a < 0.0 then -1
|
||||
else if a > 0.0 then 1
|
||||
else 0
|
||||
|
||||
let to_int (a:float) = Pervasives.int_of_float a
|
||||
let of_int (a:int) = Pervasives.float_of_int a
|
||||
|
||||
let to_string (a:float) = Pervasives.string_of_float a
|
||||
let of_string (a:string) = Pervasives.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
|
||||
|
||||
77
core/CCFloat.mli
Normal file
77
core/CCFloat.mli
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
(*
|
||||
copyright (c) 2013-2014, simon cruanes
|
||||
all rights reserved.
|
||||
|
||||
redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer. redistributions in binary
|
||||
form must reproduce the above copyright notice, this list of conditions and the
|
||||
following disclaimer in the documentation and/or other materials provided with
|
||||
the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*)
|
||||
|
||||
(** {1 Basic Float functions} *)
|
||||
|
||||
type t = float
|
||||
|
||||
val nan : t
|
||||
|
||||
val max_value : t
|
||||
val min_value : t
|
||||
|
||||
val max_finite_value : t
|
||||
|
||||
val is_nan : t -> bool
|
||||
|
||||
val add : t -> t -> t
|
||||
|
||||
val sub : t -> t -> t
|
||||
|
||||
val neg : t -> t
|
||||
|
||||
val abs : t -> t
|
||||
|
||||
val scale : t -> t -> t
|
||||
|
||||
val min : t -> t -> t
|
||||
|
||||
val max : t -> t -> t
|
||||
|
||||
val equal : t -> t -> bool
|
||||
|
||||
val compare : float -> float -> int
|
||||
|
||||
type 'a printer = Buffer.t -> 'a -> unit
|
||||
type 'a formatter = Format.formatter -> 'a -> unit
|
||||
type 'a random_gen = Random.State.t -> 'a
|
||||
|
||||
val pp : t printer
|
||||
val print : t formatter
|
||||
|
||||
val hash : t -> int
|
||||
|
||||
val random : t -> t random_gen
|
||||
val random_small : t random_gen
|
||||
val random_range : t -> t -> t random_gen
|
||||
|
||||
val sign : t -> int
|
||||
(** [sign t] is one of [-1, 0, 1] *)
|
||||
|
||||
val to_int : t -> int
|
||||
val of_int : int -> t
|
||||
|
||||
val to_string : t -> string
|
||||
val of_string : string -> t
|
||||
|
|
@ -66,6 +66,13 @@ let small_int = int 100
|
|||
|
||||
let int_range i j st = i + Random.State.int st (j-i+1)
|
||||
|
||||
let float f st = Random.State.float st f
|
||||
|
||||
let small_float = float 100.0
|
||||
|
||||
let float_range i j st = i +. Random.State.float st (j-.i+.1.)
|
||||
|
||||
|
||||
let replicate n g st =
|
||||
let rec aux acc n =
|
||||
if n = 0 then acc else aux (g st :: acc) (n-1)
|
||||
|
|
|
|||
|
|
@ -88,6 +88,14 @@ val int : int -> int t
|
|||
val int_range : int -> int -> int t
|
||||
(** Inclusive range *)
|
||||
|
||||
val small_float : float t
|
||||
|
||||
val float : float -> float t
|
||||
|
||||
val float_range : float -> float -> float t
|
||||
(** Inclusive range *)
|
||||
|
||||
|
||||
val split : int -> (int * int) option t
|
||||
(** Split a positive value [n] into [n1,n2] where [n = n1 + n2].
|
||||
@return [None] if the value is too small *)
|
||||
|
|
@ -135,3 +143,4 @@ val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
|||
val run : ?st:state -> 'a t -> 'a
|
||||
(** Using a random state (possibly the one in argument) run a generator *)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue