mpir/prim/
mpf.rs

1//! mpf
2//!
3
4use std::fmt;
5use std::error::Error;
6use std::collections::HashMap;
7
8use crate::prim::{*, typ::*, mpz::*, randstate::*, gmp::*}; // mpq::*
9use crate::util;
10
11/// __mpf_struct
12// not use #[derive(Clone)]
13#[repr(C)]
14pub struct __mpf_struct {
15  /// _mp_prec
16  pub _mp_prec: int_t,
17  /// _mp_size
18  pub _mp_size: int_t,
19  /// _mp_exp
20  pub _mp_exp: mp_exp_t,
21  /// _mp_d
22  pub _mp_d: *mut mp_limb_t
23}
24
25/// impl SNew
26impl SNew for __mpf_struct {
27  /// new
28  fn new() -> Self {
29    __mpf_struct {
30      _mp_prec: 0,
31      _mp_size: 0,
32      _mp_exp: 0,
33      _mp_d: 0 as *mut mp_limb_t
34    }
35  }
36}
37
38/// impl Drop
39impl Drop for __mpf_struct {
40  fn drop(&mut self) {
41    self.clear()
42  }
43}
44
45/// impl mpf_s
46impl __mpf_struct {
47  /// clear
48  pub fn clear(&mut self) -> () {
49    mpf_clear(self)
50  }
51
52  /// init create new instance
53  pub fn init() -> Self {
54    let mut t = mpf_s::new();
55    mpf_init(&mut t);
56    t
57  }
58
59  /// init2 with prec create new instance
60  pub fn init2(n: mp_bitcnt_t) -> Self {
61    let mut t = mpf_s::new();
62    mpf_init2(&mut t, n);
63    t
64  }
65
66  /// init_set create new instance
67  pub fn init_set(f: mpf_r) -> Self {
68    let mut t = mpf_s::new();
69    mpf_init_set(&mut t, f);
70    t
71  }
72
73  /// init_set_ui create new instance
74  pub fn init_set_ui(u: ui_t) -> Self {
75    let mut t = mpf_s::new();
76    mpf_init_set_ui(&mut t, u);
77    t
78  }
79
80  /// init_set_si create new instance
81  pub fn init_set_si(s: si_t) -> Self {
82    let mut t = mpf_s::new();
83    mpf_init_set_si(&mut t, s);
84    t
85  }
86
87  /// init_set_d create new instance
88  pub fn init_set_d(d: double_t) -> Self {
89    let mut t = mpf_s::new();
90    mpf_init_set_d(&mut t, d);
91    t
92  }
93
94  /// init_set_str create new instance
95  pub fn init_set_str(s: &str, b: int_t) -> Self {
96    let mut t = mpf_s::new();
97    mpf_init_set_str(&mut t, s, b);
98    t
99  }
100
101  /// fmtstr
102  pub fn fmtstr(&self, b: int_t, d: mp_size_t) -> String {
103    mpf_get_fmtstr(self, b, d).expect("mpf fmtstr")
104  }
105
106  /// set self = f
107  pub fn set(&mut self, f: mpf_r) -> &mut Self {
108    mpf_set(self, f);
109    self
110  }
111
112  /// set_ui self = u
113  pub fn set_ui(&mut self, u: ui_t) -> &mut Self {
114    mpf_set_ui(self, u);
115    self
116  }
117
118  /// set_si self = s
119  pub fn set_si(&mut self, s: si_t) -> &mut Self {
120    mpf_set_si(self, s);
121    self
122  }
123
124  /// set_d self = d
125  pub fn set_d(&mut self, d: double_t) -> &mut Self {
126    mpf_set_d(self, d);
127    self
128  }
129
130  /// set_z self = a
131  pub fn set_z(&mut self, a: mpz_r) -> &mut Self {
132    mpf_set_z(self, a);
133    self
134  }
135
136  /// set_str self from str
137  pub fn set_str(&mut self, s: &str, b: int_t) -> &mut Self {
138    mpf_set_str(self, s, b);
139    self
140  }
141
142  /// get_d (loss of digits)
143  pub fn get_d(&self) -> double_t {
144    mpf_get_d(self)
145  }
146
147  /// get_ui (loss of digits)
148  pub fn get_ui(&self) -> ui_t {
149    mpf_get_ui(self)
150  }
151
152  /// get_si (loss of digits)
153  pub fn get_si(&self) -> si_t {
154    mpf_get_si(self)
155  }
156
157  /// get_d_2exp (loss of digits)
158  pub fn get_d_2exp(&self) -> (double_t, si_t) {
159    let mut e: si_t = 0;
160    let d = mpf_get_d_2exp(&mut e, self);
161    (d, e)
162  }
163
164  /// cmp
165  pub fn cmp(&self, g: mpf_r) -> int_t {
166    mpf_cmp(self, g)
167  }
168
169  /// cmp_d
170  pub fn cmp_d(&self, d: double_t) -> int_t {
171    mpf_cmp_d(self, d)
172  }
173
174  /// cmp_ui
175  pub fn cmp_ui(&self, u: ui_t) -> int_t {
176    mpf_cmp_ui(self, u)
177  }
178
179  /// cmp_si
180  pub fn cmp_si(&self, s: si_t) -> int_t {
181    mpf_cmp_si(self, s)
182  }
183
184  /// cmp_z
185  pub fn cmp_z(&self, a: mpz_r) -> int_t {
186    mpf_cmp_z(self, a)
187  }
188
189  /// eq ***mathematically ill-defined and should not be used***
190  pub fn eq(&self, g: mpf_r, n: mp_bitcnt_t) -> int_t {
191    mpf_eq(self, g, n)
192  }
193
194  /// sgn
195  pub fn sgn(&self) -> int_t {
196    mpf_sgn(self)
197  }
198
199  /// reldiff returns abs(self - e) / self create new instance
200  pub fn reldiff(&self, e: mpf_r) -> Self {
201    let mut t = mpf_s::init(); // new();
202    mpf_reldiff(&mut t, self, e);
203    t
204  }
205
206  /// sqrt create new instance
207  pub fn sqrt(&self) -> Self {
208    let mut t = mpf_s::init(); // new();
209    mpf_sqrt(&mut t, self);
210    t
211  }
212
213  /// sqrt_ui create new instance
214  pub fn sqrt_ui(u: ui_t) -> Self {
215    let mut t = mpf_s::init(); // new();
216    mpf_sqrt_ui(&mut t, u);
217    t
218  }
219
220  /// abs create new instance
221  pub fn abs(&self) -> Self {
222    let mut t = mpf_s::init(); // new();
223    mpf_abs(&mut t, self);
224    t
225  }
226
227  /// neg create new instance
228  pub fn neg(&self) -> Self {
229    let mut t = mpf_s::init(); // new();
230    mpf_neg(&mut t, self);
231    t
232  }
233
234  /// sub self -= e
235  pub fn sub(&mut self, e: mpf_r) -> &mut Self {
236    mpf_sub(self, &mpf_s::init_set(self), e);
237    self
238  }
239
240  /// sub_ui self -= u
241  pub fn sub_ui(&mut self, u: ui_t) -> &mut Self {
242    mpf_sub_ui(self, &mpf_s::init_set(self), u);
243    self
244  }
245
246  /// ui_sub self = u - self
247  pub fn ui_sub(&mut self, u: ui_t) -> &mut Self {
248    mpf_ui_sub(self, u, &mpf_s::init_set(self));
249    self
250  }
251
252  /// add self += e
253  pub fn add(&mut self, e: mpf_r) -> &mut Self {
254    mpf_add(self, &mpf_s::init_set(self), e);
255    self
256  }
257
258  /// add_ui self += u
259  pub fn add_ui(&mut self, u: ui_t) -> &mut Self {
260    mpf_add_ui(self, &mpf_s::init_set(self), u);
261    self
262  }
263
264  /// mul self *= e
265  pub fn mul(&mut self, e: mpf_r) -> &mut Self {
266    mpf_mul(self, &mpf_s::init_set(self), e);
267    self
268  }
269
270  /// mul_ui self *= u
271  pub fn mul_ui(&mut self, u: ui_t) -> &mut Self {
272    mpf_mul_ui(self, &mpf_s::init_set(self), u);
273    self
274  }
275
276  /// mul_2exp self *= 2**n
277  pub fn mul_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
278    mpf_mul_2exp(self, &mpf_s::init_set(self), n);
279    self
280  }
281
282  /// div self /= e
283  pub fn div(&mut self, e: mpf_r) -> &mut Self {
284    mpf_div(self, &mpf_s::init_set(self), e);
285    self
286  }
287
288  /// div_ui self /= u
289  pub fn div_ui(&mut self, u: ui_t) -> &mut Self {
290    mpf_div_ui(self, &mpf_s::init_set(self), u);
291    self
292  }
293
294  /// ui_div self = u / self
295  pub fn ui_div(&mut self, u: ui_t) -> &mut Self {
296    mpf_ui_div(self, u, &mpf_s::init_set(self));
297    self
298  }
299
300  /// div_2exp self /= 2**n
301  pub fn div_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
302    mpf_div_2exp(self, &mpf_s::init_set(self), n);
303    self
304  }
305
306  /// pow_ui f**n create new instance
307  pub fn pow_ui(f: mpf_r, n: ui_t) -> Self {
308    let mut t = mpf_s::init(); // ***never*** use new();
309    mpf_pow_ui(&mut t, f, n);
310    t
311  }
312
313  /// ceil create new instance
314  pub fn ceil(&self) -> Self {
315    let mut t = mpf_s::init();
316    mpf_ceil(&mut t, self);
317    t
318  }
319
320  /// floor create new instance
321  pub fn floor(&self) -> Self {
322    let mut t = mpf_s::init();
323    mpf_floor(&mut t, self);
324    t
325  }
326
327  /// trunc create new instance
328  pub fn trunc(&self) -> Self {
329    let mut t = mpf_s::init();
330    mpf_trunc(&mut t, self);
331    t
332  }
333
334  /// integer_p
335  pub fn integer_p(&self) -> bool {
336    mpf_integer_p(self)
337  }
338
339  /// fits_ulong_p
340  pub fn fits_ulong_p(&self) -> bool {
341    mpf_fits_ulong_p(self)
342  }
343
344  /// fits_slong_p
345  pub fn fits_slong_p(&self) -> bool {
346    mpf_fits_slong_p(self)
347  }
348
349  /// fits_uint_p
350  pub fn fits_uint_p(&self) -> bool {
351    mpf_fits_uint_p(self)
352  }
353
354  /// fits_sint_p
355  pub fn fits_sint_p(&self) -> bool {
356    mpf_fits_sint_p(self)
357  }
358
359  /// fits_ushort_p
360  pub fn fits_ushort_p(&self) -> bool {
361    mpf_fits_ushort_p(self)
362  }
363
364  /// fits_sshort_p
365  pub fn fits_sshort_p(&self) -> bool {
366    mpf_fits_sshort_p(self)
367  }
368
369  /// urandomb (must init random state before) create new instance
370  pub fn urandomb(state: randstate_t, nbits: mp_bitcnt_t) -> Self {
371    let mut t = mpf_s::init();
372    mpf_urandomb(&mut t, state, nbits);
373    t
374  }
375
376  /// random2 create new instance
377  pub fn random2(max_size: mp_size_t, e: mp_exp_t) -> Self {
378    let mut t = mpf_s::init();
379    mpf_random2(&mut t, max_size, e);
380    t
381  }
382
383  /// get_prec
384  pub fn get_prec(&self) -> mp_bitcnt_t {
385    mpf_get_prec(self)
386  }
387
388  /// set_prec
389  pub fn set_prec(&mut self, n: mp_bitcnt_t) -> () {
390    mpf_set_prec(self, n)
391  }
392
393  /// set_prec_raw
394  pub fn set_prec_raw(&mut self, n: mp_bitcnt_t) -> () {
395    mpf_set_prec_raw(self, n)
396  }
397
398  /// calc_bits_from_digits
399  pub fn calc_bits_from_digits(d: mp_size_t) -> mp_bitcnt_t {
400    (10f64.log2() * (d + 1) as f64) as mp_bitcnt_t
401  }
402
403  /// calc_digits_from_bits
404  pub fn calc_digits_from_bits(n: mp_bitcnt_t) -> mp_size_t {
405    (n as f64 / 10f64.log2()) as mp_size_t
406  }
407
408  /// calc_pi_gauss_legendre create new instance
409  pub fn calc_pi_gauss_legendre(digits: mp_size_t) -> Self {
410    let recursion = digits.ilog2(); // or + 1
411    let a = &mut mpf_s::init_set_ui(1);
412    let b = &mut mpf_s::sqrt_ui(2);
413    let t = &mut mpf_s::init_set_ui(4);
414    let p = &mut mpf_s::init_set_ui(1);
415    let (a, b, t, _p) = (0..recursion).fold((a, b.ui_div(1), t.ui_div(1), p),
416      |(a, b, t, p), _k| {
417      let na = &mut mpf_s::init_set(a); // next a, keep a
418      na.add(b).div_ui(2);
419      let nb = &b.mul(a).sqrt(); // next b, b will be broken
420      t.sub(mpf_s::pow_ui(a.sub(na), 2).mul(p)); // modify t, a will be broken
421      (a.set(na), b.set(nb), t, p.mul_ui(2)) // modify p
422    });
423    let mut pi = mpf_s::pow_ui(a.add(b), 2);
424    pi.div_ui(4).div(t);
425    pi
426  }
427
428  /// calc_pi_euler create new instance ***CAUTION too slow digits >= 9***
429  pub fn calc_pi_euler(digits: mp_size_t) -> Self {
430    let mut pi = mpf_s::init_set_ui(1);
431    let g = &mut mpf_s::init_set_ui(0);
432    let d = 10usize.pow(digits as u32);
433    let mut ept = util::EraPrimeTableUI::new(d);
434    let _p = (0..ept.nprimes()).fold(&mut pi, |pi: mpf_t, k| {
435      let np = ept.nth_prime(k, 0); // must be in the table
436      let pp = &mpz_s::pow_ui(np, 2);
437      pi.mul(&mut g.set_z(pp).ui_div(1).ui_sub(1).ui_div(1));
438      pi
439    });
440    pi.mul_ui(6).sqrt()
441  }
442
443  /// calc_napier create new instance
444  pub fn calc_napier(x: mpf_r, digits: mp_size_t) -> Self {
445    // significant digits of calc napier by Stirling's approximation
446    let significant_digits_of_calc_napier = |n: f64| -> f64 {
447      let p = (2.0 * std::f64::consts::PI * n).sqrt().log10(); // .log(10.0)
448      let q = n * (n / std::f64::consts::E).log10(); // use preset napier f64
449      p + q - 1.0
450    };
451
452    let mut e = mpf_s::init_set_ui(0);
453//    e.set_str("2.71828182845904523536", 10); // when digits = 21
454    let g = &mut mpf_s::init_set_ui(0);
455    let m = &mut HashMap::<ui_t, mpz_s>::new();
456    let _s = (0..=digits as ui_t).try_fold(&mut e, |e: mpf_t, i| {
457      let d = if i == 0 { 0 } // 0.log10() is NaN
458        else { significant_digits_of_calc_napier(i as f64) as mp_size_t };
459//      println!("i {}, d {}", i, d);
460      let n = &mpz_s::fact_cached(i, m);
461      let f = &mut mpf_s::pow_ui(x, i);
462      e.add(f.div(g.set_z(n)));
463/*
464      if d >= digits || i == digits as ui_t {
465        println!("i {} g {} f {} e {}", i,
466         g.fmtstr(10, digits),
467         f.fmtstr(10, digits),
468         e.fmtstr(10, digits + 3));
469      }
470*/
471      if d >= digits { None } else { Some(e) }
472    }); // skip .ok_or_else(|| {...}) .expect(...) when break by None
473    e
474  }
475}
476
477/// impl Debug
478impl fmt::Debug for __mpf_struct {
479  /// fmt
480  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481    let mut s = String::from("");
482unsafe {
483    let mut sz = self._mp_size;
484    sz = if sz < 0 { -sz } else { sz };
485    std::slice::from_raw_parts(self._mp_d, sz as usize).iter()
486      .for_each(|d| s += format!(" {:016x}", d).as_str())
487}
488    write!(f, "{}, {}, {}{}", self._mp_prec, self._mp_size, self._mp_exp, s)
489  }
490}
491
492/// impl Display
493impl fmt::Display for __mpf_struct {
494  /// fmt
495  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
496    write!(f, "{}", self.fmtstr(10, 20))
497  }
498}
499
500/// mpf_s
501#[allow(non_camel_case_types)]
502pub type mpf_s = __mpf_struct; // [__mpf_struct; 1]
503/// mpf_t
504#[allow(non_camel_case_types)]
505pub type mpf_t<'a> = &'a mut mpf_s; // *mut mpf_s
506/// mpf_r
507#[allow(non_camel_case_types)]
508pub type mpf_r<'a> = &'a mpf_s; // *const mpf_s
509
510/// mpf_clears
511pub fn mpf_clears(vf: &mut Vec<mpf_t>) -> () {
512  vf.iter_mut().for_each(|f| {
513    unsafe { __gmpf_clear(*f) } // not use __gmpf_clears
514  })
515}
516
517/// mpf_clear
518#[inline]
519pub fn mpf_clear(f: mpf_t) -> () {
520  unsafe { __gmpf_clear(f) }
521}
522
523/// mpf_inits
524pub fn mpf_inits(vf: &mut Vec<mpf_t>) -> () {
525  vf.iter_mut().for_each(|f| {
526    unsafe { __gmpf_init(*f) } // not use __gmpf_inits
527  })
528}
529
530/// mpf_init
531#[inline]
532pub fn mpf_init(f: mpf_t) -> () {
533  unsafe { __gmpf_init(f) }
534}
535
536/// mpf_init2
537#[inline]
538pub fn mpf_init2(f: mpf_t, n: mp_bitcnt_t) -> () {
539  unsafe { __gmpf_init2(f, n) }
540}
541
542/// mpf_init_set
543#[inline]
544pub fn mpf_init_set(f: mpf_t, g: mpf_r) -> () {
545  unsafe { __gmpf_init_set(f, g) }
546}
547
548/// mpf_init_set_ui
549#[inline]
550pub fn mpf_init_set_ui(f: mpf_t, u: ui_t) -> () {
551  unsafe { __gmpf_init_set_ui(f, u) }
552}
553
554/// mpf_init_set_si
555#[inline]
556pub fn mpf_init_set_si(f: mpf_t, s: si_t) -> () {
557  unsafe { __gmpf_init_set_si(f, s) }
558}
559
560/// mpf_init_set_d
561#[inline]
562pub fn mpf_init_set_d(f: mpf_t, d: double_t) -> () {
563  unsafe { __gmpf_init_set_d(f, d) }
564}
565
566/// mpf_init_set_str
567#[inline]
568pub fn mpf_init_set_str(f: mpf_t, s: &str, b: int_t) -> () {
569  mpf_init_set_str_u8z(f, to_u8z!(s), b)
570}
571
572/// mpf_init_set_str_u8z
573#[inline]
574pub fn mpf_init_set_str_u8z(f: mpf_t, s: &[u8], b: int_t) -> () {
575  unsafe { __gmpf_init_set_str(f, s as *const [u8] as *const u8, b) }
576}
577
578/// mpf_set
579#[inline]
580pub fn mpf_set(f: mpf_t, g: mpf_r) -> () {
581  unsafe { __gmpf_set(f, g) }
582}
583
584/// mpf_set_ui
585#[inline]
586pub fn mpf_set_ui(f: mpf_t, u: ui_t) -> () {
587  unsafe { __gmpf_set_ui(f, u) }
588}
589
590/// mpf_set_si
591#[inline]
592pub fn mpf_set_si(f: mpf_t, s: si_t) -> () {
593  unsafe { __gmpf_set_si(f, s) }
594}
595
596/// mpf_set_d
597#[inline]
598pub fn mpf_set_d(f: mpf_t, d: double_t) -> () {
599  unsafe { __gmpf_set_d(f, d) }
600}
601
602/// mpf_set_z
603#[inline]
604pub fn mpf_set_z(f: mpf_t, a: mpz_r) -> () {
605  unsafe { __gmpf_set_z(f, a) }
606}
607
608/// mpf_set_str
609#[inline]
610pub fn mpf_set_str(f: mpf_t, s: &str, b: int_t) -> () {
611  mpf_set_str_u8z(f, to_u8z!(s), b)
612}
613
614/// mpf_set_str_u8z
615#[inline]
616pub fn mpf_set_str_u8z(f: mpf_t, s: &[u8], b: int_t) -> () {
617  unsafe { __gmpf_set_str(f, s as *const [u8] as *const u8, b) }
618}
619
620/// mpf_get_u8z
621pub fn mpf_get_u8z<'a>(s: Option<&mut [u8]>,
622  e: &'a mut mp_exp_t, b: int_t, d: mp_size_t, f: &'a mpf_s) ->
623  Option<Vec<u8>> {
624  let ff = s == None;
625unsafe {
626  let p = __gmpf_get_str(
627    match s { None => 0 as *mut u8, Some(s) => s as *mut [u8] as *mut u8 },
628    e, b, d, f);
629  u8zvec(p, ff) // return without follow code (trough check exp == 0 && "")
630/*
631  match u8zvec(p, ff) {
632  None => None,
633  Some(r) => {
634    if *e == 0 && r.len() == 0 { Some(vec![48u8]) }
635    else { Some(r) }
636  }
637  }
638*/
639}
640}
641
642/// mpf_get_str
643/// the length of mut String must larger than mpf_t display length
644pub fn mpf_get_str<'a>(s: Option<&mut String>,
645  e: &'a mut mp_exp_t, b: int_t, d: mp_size_t, f: &'a mpf_s) ->
646  Result<String, Box<dyn Error>> {
647  let r = mpf_get_u8z(
648    match s { None => None, Some(s) => Some(unsafe { s.as_bytes_mut() }) },
649    e, b, d, f);
650  match r {
651  None => Err("err mpf get str".into()),
652  Some(r) => Ok(String::from_utf8(r)?)
653  }
654}
655
656/// mpf_get_fmtstr
657pub fn mpf_get_fmtstr<'a>(f: &'a mpf_s, b: int_t, d: mp_size_t) ->
658  Result<String, Box<dyn Error>> {
659  let e = &mut (0 as mp_exp_t);
660  let r = mpf_get_u8z(None, e, b, d, f);
661  match r {
662  None => Err("err mpf get fmtstr".into()),
663  Some(r) => {
664    let (l, z) = (r.len(), String::from("0"));
665    let (sign, o) = if f._mp_size < 0 { ("-", 1) } else { ("", 0) };
666    let (es, en) = if *e < 0 { ("-", -1) } else { ("+", 1) };
667    let s = if o == l { z } else { String::from_utf8(r[o..l].to_vec())? };
668    Ok(format!("{}0.{}e{}{}", sign, s, es, *e * en))
669  }
670  }
671}
672
673/// mpf_get_d
674#[inline]
675pub fn mpf_get_d(f: mpf_r) -> double_t {
676  unsafe { __gmpf_get_d(f) }
677}
678
679/// mpf_get_ui
680#[inline]
681pub fn mpf_get_ui(f: mpf_r) -> ui_t {
682  unsafe { __gmpf_get_ui(f) }
683}
684
685/// mpf_get_si
686#[inline]
687pub fn mpf_get_si(f: mpf_r) -> si_t {
688  unsafe { __gmpf_get_si(f) }
689}
690
691/// mpf_get_d_2exp
692#[inline]
693pub fn mpf_get_d_2exp(e: &mut si_t, f: mpf_r) -> double_t {
694  unsafe { __gmpf_get_d_2exp(e, f) }
695}
696
697/// mpf_cmp
698#[inline]
699pub fn mpf_cmp(f: mpf_r, g: mpf_r) -> int_t {
700  unsafe { __gmpf_cmp(f, g) }
701}
702
703/// mpf_cmp_d
704#[inline]
705pub fn mpf_cmp_d(f: mpf_r, d: double_t) -> int_t {
706  unsafe { __gmpf_cmp_d(f, d) }
707}
708
709/// mpf_cmp_ui
710#[inline]
711pub fn mpf_cmp_ui(f: mpf_r, u: ui_t) -> int_t {
712  unsafe { __gmpf_cmp_ui(f, u) }
713}
714
715/// mpf_cmp_si
716#[inline]
717pub fn mpf_cmp_si(f: mpf_r, s: si_t) -> int_t {
718  unsafe { __gmpf_cmp_si(f, s) }
719}
720
721/// mpf_cmp_z
722#[inline]
723pub fn mpf_cmp_z(f: mpf_r, a: mpz_r) -> int_t {
724  unsafe { __gmpf_cmp_z(f, a) }
725}
726
727/// mpf_eq ***mathematically ill-defined and should not be used***
728#[inline]
729pub fn mpf_eq(f: mpf_r, g: mpf_r, n: mp_bitcnt_t) -> int_t {
730  unsafe { __gmpf_eq(f, g, n) }
731}
732
733/// mpf_sgn
734#[inline]
735pub fn mpf_sgn(f: mpf_r) -> int_t {
736//  unsafe { __gmpf_sgn(f) }
737  let t = f._mp_size;
738  if t < 0 { -1 } else { if t > 0 { 1 } else { 0 } }
739}
740
741/// mpf_reldiff
742#[inline]
743pub fn mpf_reldiff(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
744  unsafe { __gmpf_reldiff(g, f, e) }
745}
746
747/// mpf_sqrt
748#[inline]
749pub fn mpf_sqrt(g: mpf_t, f: mpf_r) -> () {
750  unsafe { __gmpf_sqrt(g, f) }
751}
752
753/// mpf_sqrt_ui
754#[inline]
755pub fn mpf_sqrt_ui(g: mpf_t, u: ui_t) -> () {
756  unsafe { __gmpf_sqrt_ui(g, u) }
757}
758
759/// mpf_abs
760#[inline]
761pub fn mpf_abs(g: mpf_t, f: mpf_r) -> () {
762  unsafe { __gmpf_abs(g, f) }
763}
764
765/// mpf_neg
766#[inline]
767pub fn mpf_neg(g: mpf_t, f: mpf_r) -> () {
768  unsafe { __gmpf_neg(g, f) }
769}
770
771/// mpf_sub g = f - e
772#[inline]
773pub fn mpf_sub(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
774  unsafe { __gmpf_sub(g, f, e) }
775}
776
777/// mpf_sub_ui g = f - u
778#[inline]
779pub fn mpf_sub_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
780  unsafe { __gmpf_sub_ui(g, f, u) }
781}
782
783/// mpf_ui_sub g = u - f
784#[inline]
785pub fn mpf_ui_sub(g: mpf_t, u: ui_t, f: mpf_r) -> () {
786  unsafe { __gmpf_ui_sub(g, u, f) }
787}
788
789/// mpf_add g = f + e
790#[inline]
791pub fn mpf_add(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
792  unsafe { __gmpf_add(g, f, e) }
793}
794
795/// mpf_add_ui g = f + u
796#[inline]
797pub fn mpf_add_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
798  unsafe { __gmpf_add_ui(g, f, u) }
799}
800
801/// mpf_mul g = f * e
802#[inline]
803pub fn mpf_mul(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
804  unsafe { __gmpf_mul(g, f, e) }
805}
806
807/// mpf_mul_ui g = f * u
808#[inline]
809pub fn mpf_mul_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
810  unsafe { __gmpf_mul_ui(g, f, u) }
811}
812
813/// mpf_mul_2exp g = f * 2**n
814#[inline]
815pub fn mpf_mul_2exp(g: mpf_t, f: mpf_r, n: mp_bitcnt_t) -> () {
816  unsafe { __gmpf_mul_2exp(g, f, n) }
817}
818
819/// mpf_div g = f / e
820#[inline]
821pub fn mpf_div(g: mpf_t, f: mpf_r, e: mpf_r) -> () {
822  unsafe { __gmpf_div(g, f, e) }
823}
824
825/// mpf_div_ui g = f / u
826#[inline]
827pub fn mpf_div_ui(g: mpf_t, f: mpf_r, u: ui_t) -> () {
828  unsafe { __gmpf_div_ui(g, f, u) }
829}
830
831/// mpf_ui_div g = u / f
832#[inline]
833pub fn mpf_ui_div(g: mpf_t, u: ui_t, f: mpf_r) -> () {
834  unsafe { __gmpf_ui_div(g, u, f) }
835}
836
837/// mpf_div_2exp g = f / 2**n
838#[inline]
839pub fn mpf_div_2exp(g: mpf_t, f: mpf_r, n: mp_bitcnt_t) -> () {
840  unsafe { __gmpf_div_2exp(g, f, n) }
841}
842
843/// mpf_pow_ui g = f**n
844#[inline]
845pub fn mpf_pow_ui(g: mpf_t, f: mpf_r, n: ui_t) -> () {
846  unsafe { __gmpf_pow_ui(g, f, n) }
847}
848
849/// mpf_ceil
850#[inline]
851pub fn mpf_ceil(g: mpf_t, f: mpf_r) -> () {
852  unsafe { __gmpf_ceil(g, f) }
853}
854
855/// mpf_floor
856#[inline]
857pub fn mpf_floor(g: mpf_t, f: mpf_r) -> () {
858  unsafe { __gmpf_floor(g, f) }
859}
860
861/// mpf_trunc
862#[inline]
863pub fn mpf_trunc(g: mpf_t, f: mpf_r) -> () {
864  unsafe { __gmpf_trunc(g, f) }
865}
866
867/// mpf_integer_p
868#[inline]
869pub fn mpf_integer_p(f: mpf_r) -> bool {
870  unsafe { __gmpf_integer_p(f) != 0 }
871}
872
873/// mpf_fits_ulong_p
874#[inline]
875pub fn mpf_fits_ulong_p(f: mpf_r) -> bool {
876  unsafe { __gmpf_fits_ulong_p(f) != 0 }
877}
878
879/// mpf_fits_slong_p
880#[inline]
881pub fn mpf_fits_slong_p(f: mpf_r) -> bool {
882  unsafe { __gmpf_fits_slong_p(f) != 0 }
883}
884
885/// mpf_fits_uint_p
886#[inline]
887pub fn mpf_fits_uint_p(f: mpf_r) -> bool {
888  unsafe { __gmpf_fits_uint_p(f) != 0 }
889}
890
891/// mpf_fits_sint_p
892#[inline]
893pub fn mpf_fits_sint_p(f: mpf_r) -> bool {
894  unsafe { __gmpf_fits_sint_p(f) != 0 }
895}
896
897/// mpf_fits_ushort_p
898#[inline]
899pub fn mpf_fits_ushort_p(f: mpf_r) -> bool {
900  unsafe { __gmpf_fits_ushort_p(f) != 0 }
901}
902
903/// mpf_fits_sshort_p
904#[inline]
905pub fn mpf_fits_sshort_p(f: mpf_r) -> bool {
906  unsafe { __gmpf_fits_sshort_p(f) != 0 }
907}
908
909/// mpf_urandomb (must init random state before)
910#[inline]
911pub fn mpf_urandomb(g: mpf_t, state: randstate_t, nbits: mp_bitcnt_t) -> () {
912  unsafe { __gmpf_urandomb(g, state, nbits) }
913}
914
915/// mpf_random2
916#[inline]
917pub fn mpf_random2(g: mpf_t, max_size: mp_size_t, e: mp_exp_t) -> () {
918  unsafe { __gmpf_random2(g, max_size, e) }
919}
920
921/// mpf_get_default_prec
922#[inline]
923pub fn mpf_get_default_prec() -> mp_bitcnt_t {
924  unsafe { __gmpf_get_default_prec() }
925}
926
927/// mpf_get_prec
928#[inline]
929pub fn mpf_get_prec(f: mpf_r) -> mp_bitcnt_t {
930  unsafe { __gmpf_get_prec(f) }
931}
932
933/// mpf_set_default_prec
934#[inline]
935pub fn mpf_set_default_prec(n: mp_bitcnt_t) -> () {
936  unsafe { __gmpf_set_default_prec(n) }
937}
938
939/// mpf_set_prec
940#[inline]
941pub fn mpf_set_prec(f: mpf_t, n: mp_bitcnt_t) -> () {
942  unsafe { __gmpf_set_prec(f, n) }
943}
944
945/// mpf_set_prec_raw
946#[inline]
947pub fn mpf_set_prec_raw(f: mpf_t, n: mp_bitcnt_t) -> () {
948  unsafe { __gmpf_set_prec_raw(f, n) }
949}