mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-06 11:15:43 -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
|
include NUM
|
||||||
|
|
||||||
val succ : t -> t
|
val succ : t -> t
|
||||||
|
val gcd : t -> t -> t
|
||||||
end
|
end
|
||||||
|
|
||||||
module type RATIONAL = sig
|
module type RATIONAL = sig
|
||||||
|
|
@ -58,6 +59,15 @@ module type RATIONAL = sig
|
||||||
val is_int : t -> bool
|
val is_int : t -> bool
|
||||||
(** Is this a proper integer? *)
|
(** 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
|
val pp_approx : int -> Format.formatter -> t -> unit
|
||||||
(** Pretty print rational with given amount of precision
|
(** Pretty print rational with given amount of precision
|
||||||
(for example as a floating point number) *)
|
(for example as a floating point number) *)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,11 @@ module Rational
|
||||||
let minus_infinity = Q.minus_inf
|
let minus_infinity = Q.minus_inf
|
||||||
let is_real = Q.is_real
|
let is_real = Q.is_real
|
||||||
let is_int q = is_real q && Z.(equal (denum q) one)
|
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)
|
let pp_approx n out q = Format.fprintf out "%*.1f" n (Q.to_float q)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue