add CCFloat.{fsign, sign_exn}

This commit is contained in:
Simon Cruanes 2014-12-14 20:04:04 +01:00
parent 44745e0da2
commit 7c09b694b9
2 changed files with 25 additions and 1 deletions

View file

@ -76,6 +76,17 @@ let sign (a:float) =
else if a > 0.0 then 1 else if a > 0.0 then 1
else 0 else 0
let fsign a =
if is_nan a then nan
else if a = 0. then a
else Pervasives.copysign 1. a
exception TrapNaN of string
let sign_exn (a:float) =
if is_nan a then raise (TrapNaN "sign")
else compare a 0.
let to_int (a:float) = Pervasives.int_of_float a let to_int (a:float) = Pervasives.int_of_float a
let of_int (a:int) = Pervasives.float_of_int a let of_int (a:int) = Pervasives.float_of_int a

View file

@ -77,7 +77,20 @@ val random_small : t random_gen
val random_range : t -> t -> t random_gen val random_range : t -> t -> t random_gen
val sign : t -> int val sign : t -> int
(** [sign t] is one of [-1, 0, 1] *) (** [sign t] is one of [-1, 0, 1], depending on how the float
compares to [0.]
@deprecated use {! fsign} or {!sign_exn} since it's more accurate *)
val fsign : t -> float
(** [fsign x] is one of [-1., -0., +0., +1.], or [nan] if [x] is NaN.
@since NEXT_RELEASE *)
exception TrapNaN of string
val sign_exn : t -> int
(** [sign_exn x] will return the sign of [x] as [1, 0] or [-1], or raise an
exception [TrapNaN] if [x] is a NaN.
Note that infinities have defined signs in OCaml.
@since NEXT_RELEASE *)
val to_int : t -> int val to_int : t -> int
val of_int : int -> t val of_int : int -> t