use std::fmt;
use std::error::Error;
use std::mem::MaybeUninit;
use crate::prim::{*, typ::*, mpz::*, mpq::*, randstate::*, gmp::*};
#[repr(C)]
pub struct __mpf_struct {
pub _mp_prec: int_t,
pub _mp_size: int_t,
pub _mp_exp: mp_exp_t,
pub _mp_d: *mut mp_limb_t
}
impl SNew for __mpf_struct {
#[inline]
fn new() -> Self {
unsafe {
let prec = MaybeUninit::<int_t>::uninit();
let sz = MaybeUninit::<int_t>::uninit();
let exp = MaybeUninit::<mp_exp_t>::uninit();
let d = MaybeUninit::<*mut mp_limb_t>::uninit();
__mpf_struct {
_mp_prec: prec.assume_init(),
_mp_size: sz.assume_init(),
_mp_exp: exp.assume_init(),
_mp_d: d.assume_init()
}
}
}
}
impl Drop for __mpf_struct {
fn drop(&mut self) {
self.clear()
}
}
impl AsPtr for __mpf_struct {}
impl AsPtrMut for __mpf_struct {}
impl __mpf_struct {
#[inline]
pub fn clear(&mut self) -> () {
mpf_clear(self)
}
#[inline]
pub fn init() -> Self {
let mut t = mpf_s::new();
mpf_init(&mut t);
t
}
#[inline]
pub fn init2(n: mp_bitcnt_t) -> Self {
let mut t = mpf_s::new();
mpf_init2(&mut t, n);
t
}
#[inline]
pub fn init_set(f: mpf_r) -> Self {
let mut t = mpf_s::new();
mpf_init_set(&mut t, f);
t
}
#[inline]
pub fn init_set_ui(u: ui_t) -> Self {
let mut t = mpf_s::new();
mpf_init_set_ui(&mut t, u);
t
}
#[inline]
pub fn init_set_si(s: si_t) -> Self {
let mut t = mpf_s::new();
mpf_init_set_si(&mut t, s);
t
}
#[inline]
pub fn init_set_d(d: double_t) -> Self {
let mut t = mpf_s::new();
mpf_init_set_d(&mut t, d);
t
}
#[inline]
pub fn init_set_str(s: &str, b: int_t) -> Self {
let mut t = mpf_s::new();
mpf_init_set_str(&mut t, s, b);
t
}
#[inline]
pub fn fmtstr(&self, b: int_t, d: mp_size_t) -> String {
mpf_get_fmtstr(self, b, d).expect("mpf fmtstr")
}
#[inline]
pub fn set(&mut self, f: mpf_r) -> &mut Self {
mpf_set(self, f);
self
}
#[inline]
pub fn set_ui(&mut self, u: ui_t) -> &mut Self {
mpf_set_ui(self, u);
self
}
#[inline]
pub fn set_si(&mut self, s: si_t) -> &mut Self {
mpf_set_si(self, s);
self
}
#[inline]
pub fn set_d(&mut self, d: double_t) -> &mut Self {
mpf_set_d(self, d);
self
}
#[inline]
pub fn set_z(&mut self, a: mpz_r) -> &mut Self {
mpf_set_z(self, a);
self
}
#[inline]
pub fn set_str(&mut self, s: &str, b: int_t) -> &mut Self {
mpf_set_str(self, s, b);
self
}
#[inline]
pub fn get_d(&self) -> double_t {
mpf_get_d(self)
}
#[inline]
pub fn get_ui(&self) -> ui_t {
mpf_get_ui(self)
}
#[inline]
pub fn get_si(&self) -> si_t {
mpf_get_si(self)
}
#[inline]
pub fn get_d_2exp(&self) -> (double_t, si_t) {
let mut e: si_t = 0;
let d = mpf_get_d_2exp(&mut e, self);
(d, e)
}
#[inline]
pub fn cmp(&self, g: mpf_r) -> int_t {
mpf_cmp(self, g)
}
#[inline]
pub fn cmp_d(&self, d: double_t) -> int_t {
mpf_cmp_d(self, d)
}
#[inline]
pub fn cmp_ui(&self, u: ui_t) -> int_t {
mpf_cmp_ui(self, u)
}
#[inline]
pub fn cmp_si(&self, s: si_t) -> int_t {
mpf_cmp_si(self, s)
}
#[inline]
pub fn cmp_z(&self, a: mpz_r) -> int_t {
mpf_cmp_z(self, a)
}
#[inline]
pub fn equal(&self, g: mpf_r, n: mp_bitcnt_t) -> int_t {
mpf_eq(self, g, n)
}
#[inline]
pub fn sgn(&self) -> int_t {
mpf_sgn(self)
}
#[inline]
pub fn reldiff(&self, e: mpf_r) -> Self {
let mut t = mpf_s::init();
mpf_reldiff(&mut t, self, e);
t
}
#[inline]
pub fn sqrt(&self) -> Self {
let mut t = mpf_s::init();
mpf_sqrt(&mut t, self);
t
}
#[inline]
pub fn sqrt_ui(u: ui_t) -> Self {
let mut t = mpf_s::init();
mpf_sqrt_ui(&mut t, u);
t
}
#[inline]
pub fn abs(&self) -> Self {
let mut t = mpf_s::init();
mpf_abs(&mut t, self);
t
}
#[inline]
pub fn neg(&self) -> Self {
let mut t = mpf_s::init();
mpf_neg(&mut t, self);
t
}
#[inline]
pub fn sub(&mut self, e: mpf_r) -> &mut Self {
mpf_sub(self, &mpf_s::init_set(self), e);
self
}
#[inline]
pub fn sub_ui(&mut self, u: ui_t) -> &mut Self {
mpf_sub_ui(self, &mpf_s::init_set(self), u);
self
}
#[inline]
pub fn ui_sub(&mut self, u: ui_t) -> &mut Self {
mpf_ui_sub(self, u, &mpf_s::init_set(self));
self
}
#[inline]
pub fn add(&mut self, e: mpf_r) -> &mut Self {
mpf_add(self, &mpf_s::init_set(self), e);
self
}
#[inline]
pub fn add_ui(&mut self, u: ui_t) -> &mut Self {
mpf_add_ui(self, &mpf_s::init_set(self), u);
self
}
#[inline]
pub fn mul(&mut self, e: mpf_r) -> &mut Self {
mpf_mul(self, &mpf_s::init_set(self), e);
self
}
#[inline]
pub fn mul_ui(&mut self, u: ui_t) -> &mut Self {
mpf_mul_ui(self, &mpf_s::init_set(self), u);
self
}
#[inline]
pub fn mul_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
mpf_mul_2exp(self, &mpf_s::init_set(self), n);
self
}
#[inline]
pub fn div(&mut self, e: mpf_r) -> &mut Self {
mpf_div(self, &mpf_s::init_set(self), e);
self
}
#[inline]
pub fn div_ui(&mut self, u: ui_t) -> &mut Self {
mpf_div_ui(self, &mpf_s::init_set(self), u);
self
}
#[inline]
pub fn ui_div(&mut self, u: ui_t) -> &mut Self {
mpf_ui_div(self, u, &mpf_s::init_set(self));
self
}
#[inline]
pub fn div_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
mpf_div_2exp(self, &mpf_s::init_set(self), n);
self
}
#[inline]
pub fn pow_ui(f: mpf_r, n: ui_t) -> Self {
let mut t = mpf_s::init(); mpf_pow_ui(&mut t, f, n);
t
}
#[inline]
pub fn ceil(&self) -> Self {
let mut t = mpf_s::init();
mpf_ceil(&mut t, self);
t
}
#[inline]
pub fn floor(&self) -> Self {
let mut t = mpf_s::init();
mpf_floor(&mut t, self);
t
}
#[inline]
pub fn trunc(&self) -> Self {
let mut t = mpf_s::init();
mpf_trunc(&mut t, self);
t
}
#[inline]
pub fn integer_p(&self) -> bool {
mpf_integer_p(self)
}
#[inline]
pub fn fits_ulong_p(&self) -> bool {
mpf_fits_ulong_p(self)
}
#[inline]
pub fn fits_slong_p(&self) -> bool {
mpf_fits_slong_p(self)
}
#[inline]
pub fn fits_uint_p(&self) -> bool {
mpf_fits_uint_p(self)
}
#[inline]
pub fn fits_sint_p(&self) -> bool {
mpf_fits_sint_p(self)
}
#[inline]
pub fn fits_ushort_p(&self) -> bool {
mpf_fits_ushort_p(self)
}
#[inline]
pub fn fits_sshort_p(&self) -> bool {
mpf_fits_sshort_p(self)
}
#[inline]
pub fn urandomb(state: randstate_t, nbits: mp_bitcnt_t) -> Self {
let mut t = mpf_s::init();
mpf_urandomb(&mut t, state, nbits);
t
}
#[inline]
pub fn random2(max_size: mp_size_t, e: mp_exp_t) -> Self {
let mut t = mpf_s::init();
mpf_random2(&mut t, max_size, e);
t
}
#[inline]
pub fn get_prec(&self) -> mp_bitcnt_t {
mpf_get_prec(self)
}
#[inline]
pub fn set_prec(&mut self, n: mp_bitcnt_t) -> () {
mpf_set_prec(self, n)
}
#[inline]
pub fn set_prec_raw(&mut self, n: mp_bitcnt_t) -> () {
mpf_set_prec_raw(self, n)
}
pub fn calc_bits_from_digits(d: mp_size_t) -> mp_bitcnt_t {
(10f64.log2() * (d + 1) as f64) as mp_bitcnt_t
}
pub fn calc_digits_from_bits(n: mp_bitcnt_t) -> mp_size_t {
(n as f64 / 10f64.log2()) as mp_size_t
}
#[inline]
pub fn init_set_z(a: mpz_r) -> Self {
let mut t = mpf_s::init();
mpf_set_z(&mut t, a);
t
}
#[inline]
pub fn init_set_q(q: mpq_r) -> Self {
mpf_s::init_set_z(q.numref()) / mpf_s::init_set_z(q.denref())
}
#[inline]
pub fn inv(&self) -> Self {
let mut t = mpf_s::init();
mpf_ui_div(&mut t, 1, self);
t
}
}
impl fmt::Debug for __mpf_struct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::from("");
unsafe {
let mut sz = self._mp_size;
sz = if sz < 0 { -sz } else { sz };
std::slice::from_raw_parts(self._mp_d, sz as usize).iter()
.for_each(|d| s += format!(" {:016x}", d).as_str())
}
write!(f, "{}, {}, {}{}", self._mp_prec, self._mp_size, self._mp_exp, s)
}
}
impl fmt::Display for __mpf_struct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.fmtstr(10, 20))
}
}
#[allow(non_camel_case_types)]
pub type mpf_s = __mpf_struct; #[allow(non_camel_case_types)]
pub type mpf_t<'a> = &'a mut mpf_s; #[allow(non_camel_case_types)]
pub type mpf_r<'a> = &'a mpf_s;
pub fn mpf_clears(vf: &mut Vec<mpf_t>) -> () {
vf.iter_mut().for_each(|f| {
unsafe { __gmpf_clear(*f) } })
}
#[inline]
pub fn mpf_clear(f: mpf_t) -> () {
unsafe { __gmpf_clear(f) }
}
pub fn mpf_inits(vf: &mut Vec<mpf_t>) -> () {
vf.iter_mut().for_each(|f| {
unsafe { __gmpf_init(*f) } })
}
#[inline]
pub fn mpf_init(f: mpf_t) -> () {
unsafe { __gmpf_init(f) }
}
#[inline]
pub fn mpf_init2(f: mpf_t, n: mp_bitcnt_t) -> () {
unsafe { __gmpf_init2(f, n) }
}
#[inline]
pub fn mpf_init_set(f: mpf_t, g: mpf_r) -> () {
unsafe { __gmpf_init_set(f, g) }
}
#[inline]
pub fn mpf_init_set_ui(f: mpf_t, u: ui_t) -> () {
unsafe { __gmpf_init_set_ui(f, u) }
}
#[inline]
pub fn mpf_init_set_si(f: mpf_t, s: si_t) -> () {
unsafe { __gmpf_init_set_si(f, s) }
}
#[inline]
pub fn mpf_init_set_d(f: mpf_t, d: double_t) -> () {
unsafe { __gmpf_init_set_d(f, d) }
}
#[inline]
pub fn mpf_init_set_str(f: mpf_t, s: &str, b: int_t) -> () {
mpf_init_set_str_u8z(f, to_u8z!(s), b)
}
#[inline]
pub fn mpf_init_set_str_u8z(f: mpf_t, s: &[u8], b: int_t) -> () {
unsafe { __gmpf_init_set_str(f, s as *const [u8] as *const u8, b) }
}
#[inline]
pub fn mpf_set(f: mpf_t, g: mpf_r) -> () {
unsafe { __gmpf_set(f, g) }
}
#[inline]
pub fn mpf_set_ui(f: mpf_t, u: ui_t) -> () {
unsafe { __gmpf_set_ui(f, u) }
}
#[inline]
pub fn mpf_set_si(f: mpf_t, s: si_t) -> () {
unsafe { __gmpf_set_si(f, s) }
}
#[inline]
pub fn mpf_set_d(f: mpf_t, d: double_t) -> () {
unsafe { __gmpf_set_d(f, d) }
}
#[inline]
pub fn mpf_set_z(f: mpf_t, a: mpz_r) -> () {
unsafe { __gmpf_set_z(f, a) }
}
#[inline]
pub fn mpf_set_str(f: mpf_t, s: &str, b: int_t) -> () {
mpf_set_str_u8z(f, to_u8z!(s), b)
}
#[inline]
pub fn mpf_set_str_u8z(f: mpf_t, s: &[u8], b: int_t) -> () {
unsafe { __gmpf_set_str(f, s as *const [u8] as *const u8, b) }
}
pub fn mpf_get_u8z<'a>(s: Option<&mut [u8]>,
e: &'a mut mp_exp_t, b: int_t, d: mp_size_t, f: &'a mpf_s) ->
Option<Vec<u8>> {
let ff = s == None;
unsafe {
let p = __gmpf_get_str(
match s { None => 0 as *mut u8, Some(s) => s as *mut [u8] as *mut u8 },
e, b, d, f);
u8zvec(p, ff)
}
}
pub fn mpf_get_str<'a>(s: Option<&mut String>,
e: &'a mut mp_exp_t, b: int_t, d: mp_size_t, f: &'a mpf_s) ->
Result<String, Box<dyn Error>> {
let r = mpf_get_u8z(
match s { None => None, Some(s) => Some(unsafe { s.as_bytes_mut() }) },
e, b, d, f);
match r {
None => Err("err mpf get str".into()),
Some(r) => Ok(String::from_utf8(r)?)
}
}
pub fn mpf_get_fmtstr<'a>(f: &'a mpf_s, b: int_t, d: mp_size_t) ->
Result<String, Box<dyn Error>> {
let e = &mut (0 as mp_exp_t);
let r = mpf_get_u8z(None, e, b, d, f);
match r {
None => Err("err mpf get fmtstr".into()),
Some(r) => {
let (l, z) = (r.len(), String::from("0"));
let (sign, o) = if f._mp_size < 0 { ("-", 1) } else { ("", 0) };
let (es, en) = if *e < 0 { ("-", -1) } else { ("+", 1) };
let s = if o == l { z } else { String::from_utf8(r[o..l].to_vec())? };
Ok(format!("{}0.{}e{}{}", sign, s, es, *e * en))
}
}
}
#[inline]
pub fn mpf_get_d(f: mpf_r) -> double_t {
unsafe { __gmpf_get_d(f) }
}
#[inline]
pub fn mpf_get_ui(f: mpf_r) -> ui_t {
unsafe { __gmpf_get_ui(f) }
}
#[inline]
pub fn mpf_get_si(f: mpf_r) -> si_t {
unsafe { __gmpf_get_si(f) }
}
#[inline]
pub fn mpf_get_d_2exp(e: &mut si_t, f: mpf_r) -> double_t {
unsafe { __gmpf_get_d_2exp(e, f) }
}
#[inline]
pub fn mpf_cmp(f: mpf_r, g: mpf_r) -> int_t {
unsafe { __gmpf_cmp(f, g) }
}
#[inline]
pub fn mpf_cmp_d(f: mpf_r, d: double_t) -> int_t {
unsafe { __gmpf_cmp_d(f, d) }
}
#[inline]
pub fn mpf_cmp_ui(f: mpf_r, u: ui_t) -> int_t {
unsafe { __gmpf_cmp_ui(f, u) }
}
#[inline]
pub fn mpf_cmp_si(f: mpf_r, s: si_t) -> int_t {
unsafe { __gmpf_cmp_si(f, s) }
}
#[inline]
pub fn mpf_cmp_z(f: mpf_r, a: mpz_r) -> int_t {
unsafe { __gmpf_cmp_z(f, a) }
}
#[inline]
pub fn mpf_eq(f: mpf_r, g: mpf_r, n: mp_bitcnt_t) -> int_t {
unsafe { __gmpf_eq(f, g, n) }
}
#[inline]
pub fn mpf_sgn(f: mpf_r) -> int_t {
let t = f._mp_size;
if t < 0 { -1 } else { if t > 0 { 1 } else { 0 } }
}
#[inline]
pub fn mpf_reldiff(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
unsafe { __gmpf_reldiff(g, f, e) }
}
#[inline]
pub fn mpf_sqrt(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_sqrt(g, f) }
}
#[inline]
pub fn mpf_sqrt_ui(g: mpf_t, u: ui_t) -> () {
unsafe { __gmpf_sqrt_ui(g, u) }
}
#[inline]
pub fn mpf_abs(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_abs(g, f) }
}
#[inline]
pub fn mpf_neg(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_neg(g, f) }
}
#[inline]
pub fn mpf_sub(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
unsafe { __gmpf_sub(g, f, e) }
}
#[inline]
pub fn mpf_sub_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
unsafe { __gmpf_sub_ui(g, f, u) }
}
#[inline]
pub fn mpf_ui_sub(g: mpf_t, u: ui_t, f: mpf_r) -> () {
unsafe { __gmpf_ui_sub(g, u, f) }
}
#[inline]
pub fn mpf_add(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
unsafe { __gmpf_add(g, f, e) }
}
#[inline]
pub fn mpf_add_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
unsafe { __gmpf_add_ui(g, f, u) }
}
#[inline]
pub fn mpf_mul(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
unsafe { __gmpf_mul(g, f, e) }
}
#[inline]
pub fn mpf_mul_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
unsafe { __gmpf_mul_ui(g, f, u) }
}
#[inline]
pub fn mpf_mul_2exp(g: mpf_t, f: mpf_r, n: mp_bitcnt_t) -> () {
unsafe { __gmpf_mul_2exp(g, f, n) }
}
#[inline]
pub fn mpf_div(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
unsafe { __gmpf_div(g, f, e) }
}
#[inline]
pub fn mpf_div_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
unsafe { __gmpf_div_ui(g, f, u) }
}
#[inline]
pub fn mpf_ui_div(g: mpf_t, u: ui_t, f: mpf_r) -> () {
unsafe { __gmpf_ui_div(g, u, f) }
}
#[inline]
pub fn mpf_div_2exp(g: mpf_t, f: mpf_r, n: mp_bitcnt_t) -> () {
unsafe { __gmpf_div_2exp(g, f, n) }
}
#[inline]
pub fn mpf_pow_ui(g: mpf_t, f: mpf_r, n: ui_t) -> () {
unsafe { __gmpf_pow_ui(g, f, n) }
}
#[inline]
pub fn mpf_ceil(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_ceil(g, f) }
}
#[inline]
pub fn mpf_floor(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_floor(g, f) }
}
#[inline]
pub fn mpf_trunc(g: mpf_t, f: mpf_r) -> () {
unsafe { __gmpf_trunc(g, f) }
}
#[inline]
pub fn mpf_integer_p(f: mpf_r) -> bool {
unsafe { __gmpf_integer_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_ulong_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_ulong_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_slong_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_slong_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_uint_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_uint_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_sint_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_sint_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_ushort_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_ushort_p(f) != 0 }
}
#[inline]
pub fn mpf_fits_sshort_p(f: mpf_r) -> bool {
unsafe { __gmpf_fits_sshort_p(f) != 0 }
}
#[inline]
pub fn mpf_urandomb(g: mpf_t, state: randstate_t, nbits: mp_bitcnt_t) -> () {
unsafe { __gmpf_urandomb(g, state, nbits) }
}
#[inline]
pub fn mpf_random2(g: mpf_t, max_size: mp_size_t, e: mp_exp_t) -> () {
unsafe { __gmpf_random2(g, max_size, e) }
}
#[inline]
pub fn mpf_get_default_prec() -> mp_bitcnt_t {
unsafe { __gmpf_get_default_prec() }
}
#[inline]
pub fn mpf_get_prec(f: mpf_r) -> mp_bitcnt_t {
unsafe { __gmpf_get_prec(f) }
}
#[inline]
pub fn mpf_set_default_prec(n: mp_bitcnt_t) -> () {
unsafe { __gmpf_set_default_prec(n) }
}
#[inline]
pub fn mpf_set_prec(f: mpf_t, n: mp_bitcnt_t) -> () {
unsafe { __gmpf_set_prec(f, n) }
}
#[inline]
pub fn mpf_set_prec_raw(f: mpf_t, n: mp_bitcnt_t) -> () {
unsafe { __gmpf_set_prec_raw(f, n) }
}