1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use libc::{c_int, c_ulong, c_long, c_double, c_void};
use gmp::mpz::{Mpz, mpz_ptr, mpz_srcptr};
use gmp::mpq::{Mpq, mpq_srcptr};
use gmp::mpf::{Mpf, mpf_ptr, mpf_srcptr};
use std::mem::uninitialized;
use std::cmp;
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
use std::convert::{From, Into};
use std::ops::{Add, Sub, Mul, Div, Neg};

type mpfr_prec_t = c_long;
type mpfr_sign_t = c_int;
type mpfr_exp_t = c_long;

#[repr(C)]
pub enum mpfr_rnd_t {
	MPFR_RNDN=0,  /* round to nearest, with ties to even */
	MPFR_RNDZ,    /* round toward zero */
	MPFR_RNDU,    /* round toward +Inf */
	MPFR_RNDD,    /* round toward -Inf */
	MPFR_RNDA,    /* round away from zero */
	MPFR_RNDF,    /* faithful rounding (not implemented yet) */
	MPFR_RNDNA=-1 /* round to nearest, with ties away from zero (mpfr_rouund) */
}

#[repr(C)]
pub struct mpfr_struct {
    _mpfr_prec: mpfr_prec_t,
    _mpfr_sign: mpfr_sign_t,
    _mpfr_exp: mpfr_exp_t,
    _mpfr_d: *mut c_void
}

pub type mpfr_srcptr = *const mpfr_struct;
pub type mpfr_ptr = *mut mpfr_struct;

#[link(name = "mpfr")]
extern "C" {
	// Initialization
    fn mpfr_init(x: mpfr_ptr);
	fn mpfr_init2(x: mpfr_ptr, prec: mpfr_prec_t);
	fn mpfr_clear(x: mpfr_ptr);
	fn mpfr_set_default_prec(prec: mpfr_prec_t);
	fn mpfr_get_default_prec() -> mpfr_prec_t;
	fn mpfr_set_prec(x: mpfr_ptr, prec: mpfr_prec_t);
	fn mpfr_get_prec(x: mpfr_srcptr) -> mpfr_prec_t;
	
	// Assignment
	fn mpfr_set(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_ui(rop: mpfr_ptr, op: c_ulong, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_si(rop: mpfr_ptr, op: c_long, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_d(rop: mpfr_ptr, op: c_double, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_z(rop: mpfr_ptr, op: mpz_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_q(rop: mpfr_ptr, op: mpq_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_f(rop: mpfr_ptr, op: mpf_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_ui_2exp(rop: mpfr_ptr, op: c_ulong, e: mpfr_exp_t, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_si_2exp(rop: mpfr_ptr, op: c_long, e: mpfr_exp_t, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_z_2exp(rop: mpfr_ptr, op: mpz_srcptr, e: mpfr_exp_t, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_set_nan(x: mpfr_ptr);
	fn mpfr_set_inf(x: mpfr_ptr, sign: c_int);
	fn mpfr_set_zero(x: mpfr_ptr, sign: c_int);
	
	// Conversion
	fn mpfr_get_ui(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_ulong;
	fn mpfr_get_si(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_long;
	fn mpfr_get_d(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_double;
	fn mpfr_get_z(rop: mpz_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_get_f(rop: mpf_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
	
	// Comparison
	fn mpfr_cmp(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
	fn mpfr_cmp_ui(op1: mpfr_srcptr, op2: c_ulong) -> c_int;
	
	// Arithmetic
	fn mpfr_add(rop: mpfr_ptr, op1: mpfr_srcptr, op2: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_sub(rop: mpfr_ptr, op1: mpfr_srcptr, op2: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_mul(rop: mpfr_ptr, op1: mpfr_srcptr, op2: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_mul_d(rop: mpfr_ptr, op1: mpfr_srcptr, op2: c_double, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_div(rop: mpfr_ptr, op1: mpfr_srcptr, op2: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_div_d(rop: mpfr_ptr, op1: mpfr_srcptr, op2: c_double, rnd: mpfr_rnd_t) -> c_int;
	fn mpfr_neg(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;

    // Rounding
    fn mpfr_floor(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    fn mpfr_ceil(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    fn mpfr_round(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;

    // Functions
    fn mpfr_sqrt(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_cbrt(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_root(rop: mpfr_ptr, op: mpfr_srcptr, k: c_ulong, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_pow(rop: mpfr_ptr, op1: mpfr_srcptr, op2: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_exp(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    fn mpfr_log(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
}

pub struct Mpfr {
    pub mpfr: mpfr_struct,
}

unsafe impl Send for Mpfr { }

impl Drop for Mpfr {
    fn drop(&mut self) { unsafe { mpfr_clear(&mut self.mpfr) } }
}

impl Clone for Mpfr {
    fn clone(&self) -> Mpfr {
        let mut mpfr = Mpfr::new2(self.get_prec());
        mpfr.set(self);
        mpfr
    }
}

impl Mpfr {
    pub fn new() -> Mpfr {
        unsafe {
            let mut mpfr = uninitialized();
            mpfr_init(&mut mpfr);
            Mpfr { mpfr: mpfr }
        }
    }

    pub fn new2(precision: usize) -> Mpfr {
        unsafe {
            let mut mpfr = uninitialized();
            mpfr_init2(&mut mpfr, precision as mpfr_prec_t);
            Mpfr { mpfr: mpfr }
        }
    }
    
    pub fn set(&mut self, other: &Mpfr) {
    	unsafe {
    		mpfr_set(&mut self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
    	}
    }
    
    pub fn new_u64_2exp(base: u64, exp: i32) -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_ui_2exp(&mut mpfr.mpfr, base as c_ulong, exp as mpfr_exp_t, mpfr_rnd_t::MPFR_RNDN);
    		mpfr
    	}
    }
    
    pub fn new_i64_2exp(base: i64, exp: i32) -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_si_2exp(&mut mpfr.mpfr, base as c_long, exp as mpfr_exp_t, mpfr_rnd_t::MPFR_RNDN);
    		mpfr
    	}
    }
    
    pub fn new_mpz_2exp(base: &Mpz, exp: i32) -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_z_2exp(&mut mpfr.mpfr, &base.mpz, exp as mpfr_exp_t, mpfr_rnd_t::MPFR_RNDN);
    		mpfr
    	}
    }
    
    pub fn zero(sign: i32) -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_zero(&mut mpfr.mpfr, sign as c_int);
    		mpfr
    	}
    }
    
    pub fn inf(sign: i32) -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_inf(&mut mpfr.mpfr, sign as c_int);
    		mpfr
    	}
    }
    
    pub fn nan() -> Mpfr {
    	unsafe {
    		let mut mpfr = Mpfr::new();
    		mpfr_set_nan(&mut mpfr.mpfr);
    		mpfr
    	}
    }
    
    pub fn get_default_prec() -> usize {
    	unsafe {
    		mpfr_get_default_prec() as usize
    	}
    }
    
    pub fn set_default_prec(precision: usize) {
    	unsafe {
    		mpfr_set_default_prec(precision as mpfr_prec_t);
    	}
    }
    
    pub fn get_prec(&self) -> usize {
    	unsafe {
    		mpfr_get_prec(&self.mpfr) as usize
    	}
    }
    
    pub fn set_prec(&mut self, precision: usize) {
    	unsafe {
    		mpfr_set_prec(&mut self.mpfr, precision as mpfr_prec_t);
    	}
    }

    // Rounding

    pub fn floor(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_floor(&mut res.mpfr, &self.mpfr);
            res
        }
    }

    pub fn ceil(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_ceil(&mut res.mpfr, &self.mpfr);
            res
        }
    }

    pub fn round(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_round(&mut res.mpfr, &self.mpfr);
            res
        }
    }

    // Mathematical functions

    pub fn sqrt(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_sqrt(&mut res.mpfr, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }

    pub fn cbrt(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_cbrt(&mut res.mpfr, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }

    pub fn root(&self, k: u64) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_root(&mut res.mpfr, &self.mpfr, k as c_ulong, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }

    pub fn pow(&self, other: &Mpfr) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_pow(&mut res.mpfr, &self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }

    pub fn exp(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_exp(&mut res.mpfr, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }

    pub fn log(&self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_log(&mut res.mpfr, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl Eq for Mpfr { }
impl PartialEq for Mpfr {
	fn eq(&self, other: &Mpfr) -> bool {
		unsafe {
			mpfr_cmp(&self.mpfr, &other.mpfr) == 0
		}
	}
}

impl Ord for Mpfr {
    fn cmp(&self, other: &Mpfr) -> Ordering {
        let cmp = unsafe { mpfr_cmp(&self.mpfr, &other.mpfr) };
        if cmp == 0 {
            Ordering::Equal
        } else if cmp > 0 {
            Ordering::Greater
        } else {
            Ordering::Less
        }
    }
}

impl PartialOrd for Mpfr {
    fn partial_cmp(&self, other: &Mpfr) -> Option<Ordering> {
    	Some(self.cmp(other))
    }
}

// Conversions

impl From<i64> for Mpfr {
	fn from(x: i64) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_si(&mut mpfr.mpfr, x as c_long, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl From<u64> for Mpfr {
	fn from(x: u64) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_ui(&mut mpfr.mpfr, x as c_ulong, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl From<f64> for Mpfr {
	fn from(x: f64) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_d(&mut mpfr.mpfr, x as c_double, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl From<Mpz> for Mpfr {
	fn from(x: Mpz) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_z(&mut mpfr.mpfr, &x.mpz, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl From<Mpq> for Mpfr {
	fn from(x: Mpq) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_q(&mut mpfr.mpfr, &x.mpq, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl From<Mpf> for Mpfr {
	fn from(x: Mpf) -> Mpfr {
        unsafe {
            let mut mpfr = Mpfr::new();
            mpfr_set_f(&mut mpfr.mpfr, &x.mpf, mpfr_rnd_t::MPFR_RNDN);
            mpfr
        }
	}
}

impl<'a> Into<i64> for &'a Mpfr {
	fn into(self) -> i64 {
		unsafe {
			mpfr_get_si(&self.mpfr, mpfr_rnd_t::MPFR_RNDN) as i64
		}
	}
}

impl<'a> Into<u64> for &'a Mpfr {
	fn into(self) -> u64 {
		unsafe {
			mpfr_get_ui(&self.mpfr, mpfr_rnd_t::MPFR_RNDN) as u64
		}
	}
}

impl<'a> Into<f64> for &'a Mpfr {
	fn into(self) -> f64 {
		unsafe {
			mpfr_get_d(&self.mpfr, mpfr_rnd_t::MPFR_RNDN) as f64
		}
	}
}

impl<'a> Into<Mpz> for &'a Mpfr {
	fn into(self) -> Mpz {
		unsafe {
			let mut result = Mpz::new(); 
			mpfr_get_z(&mut result.mpz, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
			result
		}
	}
}

impl<'a> Into<Mpf> for &'a Mpfr {
	fn into(self) -> Mpf {
		unsafe {
			let mut result = Mpf::new(self.get_prec()); 
			mpfr_get_f(&mut result.mpf, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
			result
		}
	}
}

impl<'a, 'b> Add<&'a Mpfr> for &'b Mpfr {
    type Output = Mpfr;
    fn add(self, other: &Mpfr) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(cmp::max(self.get_prec(), other.get_prec()) );
            mpfr_add(&mut res.mpfr, &self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'a, 'b> Sub<&'a Mpfr> for &'b Mpfr {
    type Output = Mpfr;
    fn sub(self, other: &Mpfr) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(cmp::max(self.get_prec(), other.get_prec()));
            mpfr_sub(&mut res.mpfr, &self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'a, 'b> Mul<&'a Mpfr> for &'b Mpfr {
    type Output = Mpfr;
    fn mul(self, other: &Mpfr) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(cmp::max(self.get_prec(), other.get_prec()));
            mpfr_mul(&mut res.mpfr, &self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'a> Mul<f64> for &'a Mpfr {
    type Output = Mpfr;
    fn mul(self, other: f64) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_mul_d(&mut res.mpfr, &self.mpfr, other as c_double, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'a, 'b> Div<&'a Mpfr> for &'b Mpfr {
    type Output = Mpfr;
    fn div(self, other: &Mpfr) -> Mpfr {
        unsafe {
            if mpfr_cmp_ui(&other.mpfr, 0) == 0 {
                panic!("divide by zero")
            }

            let mut res = Mpfr::new2(cmp::max(self.get_prec(), other.get_prec()) );
            mpfr_div(&mut res.mpfr, &self.mpfr, &other.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'a> Div<f64> for &'a Mpfr {
    type Output = Mpfr;
    fn div(self, other: f64) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_div_d(&mut res.mpfr, &self.mpfr, other as c_double, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

impl<'b> Neg for &'b Mpfr {
    type Output = Mpfr;
    fn neg(self) -> Mpfr {
        unsafe {
            let mut res = Mpfr::new2(self.get_prec());
            mpfr_neg(&mut res.mpfr, &self.mpfr, mpfr_rnd_t::MPFR_RNDN);
            res
        }
    }
}

gen_overloads!(Mpfr);