mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
add CCFloat.round
This commit is contained in:
parent
103963dc45
commit
d7e8cb24fd
2 changed files with 15 additions and 0 deletions
|
|
@ -60,6 +60,17 @@ let sign_exn (a:float) =
|
||||||
if is_nan a then raise (TrapNaN "sign_exn")
|
if is_nan a then raise (TrapNaN "sign_exn")
|
||||||
else compare a 0.
|
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
|
||||||
|
|
||||||
|
(*$=
|
||||||
|
2. (round 1.6)
|
||||||
|
1. (round 1.4)
|
||||||
|
0. (round 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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ val fsign : t -> t
|
||||||
(** [fsign x] is one of [-1., -0., +0., +1.], or [nan] if [x] is NaN.
|
(** [fsign x] is one of [-1., -0., +0., +1.], or [nan] if [x] is NaN.
|
||||||
@since 0.7 *)
|
@since 0.7 *)
|
||||||
|
|
||||||
|
val round : t -> t
|
||||||
|
(** [round f] returns the closest integer value, either above or below
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
exception TrapNaN of string
|
exception TrapNaN of string
|
||||||
val sign_exn : t -> int
|
val sign_exn : t -> int
|
||||||
(** [sign_exn x] will return the sign of [x] as [1, 0] or [-1], or raise an
|
(** [sign_exn x] will return the sign of [x] as [1, 0] or [-1], or raise an
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue