mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-06 03:05:31 -05:00
feat(arith): more functions in Q
This commit is contained in:
parent
f1f1967059
commit
7f2e92fe88
2 changed files with 15 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ module type INT = sig
|
|||
include NUM
|
||||
|
||||
val succ : t -> t
|
||||
val gcd : t -> t -> t
|
||||
end
|
||||
|
||||
module type RATIONAL = sig
|
||||
|
|
@ -58,6 +59,15 @@ module type RATIONAL = sig
|
|||
val is_int : t -> bool
|
||||
(** Is this a proper integer? *)
|
||||
|
||||
val as_int : t -> bigint option
|
||||
(** Convert to an integer if it's one, return [None] otherwise *)
|
||||
|
||||
val floor : t -> bigint
|
||||
(** Integer equal or below *)
|
||||
|
||||
val ceil : t -> bigint
|
||||
(** Integer equal or above *)
|
||||
|
||||
val pp_approx : int -> Format.formatter -> t -> unit
|
||||
(** Pretty print rational with given amount of precision
|
||||
(for example as a floating point number) *)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,11 @@ module Rational
|
|||
let minus_infinity = Q.minus_inf
|
||||
let is_real = Q.is_real
|
||||
let is_int q = is_real q && Z.(equal (denum q) one)
|
||||
let as_int q = if is_int q then Some (to_bigint q) else None
|
||||
let floor q = Q.to_bigint q
|
||||
let ceil q =
|
||||
let n = Q.to_bigint q in
|
||||
if is_int q then n else Z.(n + one)
|
||||
|
||||
let pp_approx n out q = Format.fprintf out "%*.1f" n (Q.to_float q)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue