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
503
504
505
506
// Copyright © 2016–2019 University of Malta

// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.

/*!
Multi-precision floating-point numbers with correct rounding.

This module provides support for floating-point numbers of type
[`Float`].

[`Float`]: ../struct.Float.html
*/

pub(crate) mod arith;
pub(crate) mod big;
mod cmp;
mod ord;
#[cfg(feature = "serde")]
mod serde;
pub(crate) mod small;
mod traits;

use cast::cast;
pub use float::big::ParseFloatError;
pub use float::ord::OrdFloat;
pub use float::small::{SmallFloat, ToSmall};
use gmp_mpfr_sys::mpfr;
use std::mem;
use std::{i32, u32};

/**
Returns the minimum value for the exponent.

# Examples

```rust
use rug::float;
println!("Minimum exponent is {}", float::exp_min());
```
*/
#[inline]
pub fn exp_min() -> i32 {
    let min = unsafe { mpfr::get_emin() };
    if min > mpfr::exp_t::from(i32::MIN) {
        min as i32
    } else {
        i32::MIN
    }
}

/**
Returns the maximum value for the exponent.

# Examples

```rust
use rug::float;
println!("Maximum exponent is {}", float::exp_max());
```
*/
#[inline]
pub fn exp_max() -> i32 {
    let max = unsafe { mpfr::get_emax() };
    if max < mpfr::exp_t::from(i32::MAX) {
        max as i32
    } else {
        i32::MAX
    }
}

/**
Returns the minimum value for the precision.

# Examples

```rust
use rug::float;
println!("Minimum precision is {}", float::prec_min());
```
*/
#[inline]
pub fn prec_min() -> u32 {
    cast(mpfr::PREC_MIN)
}

/**
Returns the maximum value for the precision.

# Examples

```rust
use rug::float;
println!("Maximum precision is {}", float::prec_max());
```
*/
#[inline]
pub fn prec_max() -> u32 {
    let max = mpfr::PREC_MAX;
    if mem::size_of::<mpfr::prec_t>() <= mem::size_of::<u32>()
        || max < cast::<_, mpfr::prec_t>(u32::MAX)
    {
        max as u32
    } else {
        u32::MAX
    }
}

/**
The rounding methods for floating-point values.

When rounding to the nearest, if the number to be rounded is exactly
between two representable numbers, it is rounded to the even one, that
is, the one with the least significant bit set to zero.

# Examples

```rust
use rug::float::Round;
use rug::ops::AssignRound;
use rug::Float;
let mut f4 = Float::new(4);
f4.assign_round(10.4, Round::Nearest);
assert_eq!(f4, 10);
f4.assign_round(10.6, Round::Nearest);
assert_eq!(f4, 11);
f4.assign_round(-10.7, Round::Zero);
assert_eq!(f4, -10);
f4.assign_round(10.3, Round::Up);
assert_eq!(f4, 11);
```

Rounding to the nearest will round numbers exactly between two
representable numbers to the even one.

```rust
use rug::float::Round;
use rug::ops::AssignRound;
use rug::Float;
// 24 is 11000 in binary
// 25 is 11001 in binary
// 26 is 11010 in binary
// 27 is 11011 in binary
// 28 is 11100 in binary
let mut f4 = Float::new(4);
f4.assign_round(25, Round::Nearest);
assert_eq!(f4, 24);
f4.assign_round(27, Round::Nearest);
assert_eq!(f4, 28);
```
*/
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Round {
    /// Round towards the nearest.
    Nearest,
    /// Round towards zero.
    Zero,
    /// Round towards plus infinity.
    Up,
    /// Round towards minus infinity.
    Down,
    #[doc(hidden)]
    __Nonexhaustive,
}

impl Default for Round {
    #[inline]
    fn default() -> Round {
        Round::Nearest
    }
}

/**
The available floating-point constants.

# Examples

```rust
use rug::float::Constant;
use rug::Float;

let log2 = Float::with_val(53, Constant::Log2);
let pi = Float::with_val(53, Constant::Pi);
let euler = Float::with_val(53, Constant::Euler);
let catalan = Float::with_val(53, Constant::Catalan);

assert_eq!(log2.to_string_radix(10, Some(5)), "6.9315e-1");
assert_eq!(pi.to_string_radix(10, Some(5)), "3.1416");
assert_eq!(euler.to_string_radix(10, Some(5)), "5.7722e-1");
assert_eq!(catalan.to_string_radix(10, Some(5)), "9.1597e-1");
```
*/
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Constant {
    /// The logarithm of two, 0.693...
    Log2,
    /// The value of pi, 3.141...
    Pi,
    /// Euler’s constant, 0.577...
    Euler,
    /// Catalan’s constant, 0.915...
    Catalan,
    #[doc(hidden)]
    __Nonexhaustive,
}

/**
Special floating-point values.

# Examples

```rust
use rug::float::Special;
use rug::Float;

let zero = Float::with_val(53, Special::Zero);
let neg_zero = Float::with_val(53, Special::NegZero);
let infinity = Float::with_val(53, Special::Infinity);
let neg_infinity = Float::with_val(53, Special::NegInfinity);
let nan = Float::with_val(53, Special::Nan);

assert_eq!(zero, 0);
assert!(zero.is_sign_positive());
assert_eq!(neg_zero, 0);
assert!(neg_zero.is_sign_negative());
assert!(infinity.is_infinite());
assert!(infinity.is_sign_positive());
assert!(neg_infinity.is_infinite());
assert!(neg_infinity.is_sign_negative());
assert!(nan.is_nan());
```
*/
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Special {
    /// Positive zero.
    Zero,
    /// Negative zero.
    NegZero,
    /// Positive infinity.
    Infinity,
    /// Negative infinity.
    NegInfinity,
    /// Not a number.
    Nan,
    #[doc(hidden)]
    __Nonexhaustive,
}

#[cfg(test)]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::cyclomatic_complexity))]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp))]
pub(crate) mod tests {
    use float::{Round, Special};
    use gmp_mpfr_sys::{gmp, mpfr};
    #[cfg(feature = "rand")]
    use rand::{RandGen, RandState};
    use std::cmp::Ordering;
    use std::f64;
    use std::fmt::{Debug, Error as FmtError, Formatter};
    use {Assign, Float};

    #[derive(Clone, Copy)]
    pub enum Cmp {
        F64(f64),
        Nan(bool),
    }

    impl Cmp {
        pub fn inf(neg: bool) -> Cmp {
            Cmp::F64(if neg { f64::NEG_INFINITY } else { f64::INFINITY })
        }
    }

    impl Debug for Cmp {
        fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
            match *self {
                Cmp::F64(ref val) => val.fmt(f),
                Cmp::Nan(negative) => {
                    let s = if negative { "-NaN" } else { "NaN" };
                    s.fmt(f)
                }
            }
        }
    }

    impl PartialEq<Cmp> for Float {
        fn eq(&self, other: &Cmp) -> bool {
            match *other {
                Cmp::F64(ref f) => self.eq(f),
                Cmp::Nan(negative) => {
                    self.is_nan() && self.is_sign_negative() == negative
                }
            }
        }
    }

    #[test]
    fn check_from_str() {
        assert!(
            Float::with_val(53, Float::parse("-0").unwrap()).is_sign_negative()
        );
        assert!(
            Float::with_val(53, Float::parse("+0").unwrap()).is_sign_positive()
        );
        assert!(
            Float::with_val(53, Float::parse("1e1000").unwrap()).is_finite()
        );
        let huge_hex = "1@99999999999999999999999999999999";
        assert!(Float::with_val(53, Float::parse_radix(huge_hex, 16).unwrap())
            .is_infinite());

        let bad_strings = [
            ("", 10),
            ("-", 10),
            ("+", 10),
            (".", 10),
            ("inf", 11),
            ("@ nan @", 10),
            ("inf", 16),
            ("1.1.", 10),
            ("1e", 10),
            ("e10", 10),
            (".e10", 10),
            ("1e1.", 10),
            ("1e1e1", 10),
            ("1e+-1", 10),
            ("1e-+1", 10),
            ("+-1", 10),
            ("-+1", 10),
            ("infinit", 10),
            ("1@1a", 16),
            ("9", 9),
            ("nan(20) x", 10),
        ];
        for &(s, radix) in bad_strings.iter() {
            assert!(
                Float::parse_radix(s, radix).is_err(),
                "{} parsed correctly",
                s
            );
        }
        let good_strings = [
            ("INF", 10, Cmp::inf(false)),
            ("- @iNf@", 16, Cmp::inf(true)),
            ("+0e99", 2, Cmp::F64(0.0)),
            ("-9.9e1", 10, Cmp::F64(-99.0)),
            ("-.99e+2", 10, Cmp::F64(-99.0)),
            ("+99.e+0", 10, Cmp::F64(99.0)),
            ("-99@-1", 10, Cmp::F64(-9.9f64)),
            ("-a_b__.C_d_E_@3", 16, Cmp::F64(f64::from(-0xabcde))),
            ("1e1023", 2, Cmp::F64(2.0f64.powi(1023))),
            (" NaN() ", 10, Cmp::Nan(false)),
            (" + NaN (20 Number_Is) ", 10, Cmp::Nan(false)),
            (" - @nan@", 2, Cmp::Nan(true)),
        ];
        for &(s, radix, f) in good_strings.iter() {
            match Float::parse_radix(s, radix) {
                Ok(ok) => assert_eq!(Float::with_val(53, ok), f),
                Err(_err) => panic!("could not parse {}", s),
            }
        }
    }

    #[test]
    fn check_clamping() {
        let mut f = Float::new(4);

        // Both 1.00002 and 1.00001 are rounded to 1.0 with the same
        // rounding direction, so these work even though min > max.

        f.assign(-1);
        let dir = f.clamp_round(&1.00002, &1.00001, Round::Down);
        assert_eq!(f, 1.0);
        assert_eq!(dir, Ordering::Less);

        f.assign(-1);
        let dir = f.clamp_round(&1.00002, &1.00001, Round::Up);
        assert_eq!(f, 1.125);
        assert_eq!(dir, Ordering::Greater);

        f.assign(2);
        let dir = f.clamp_round(&1.00002, &1.00001, Round::Down);
        assert_eq!(f, 1.0);
        assert_eq!(dir, Ordering::Less);

        f.assign(2);
        let dir = f.clamp_round(&1.00002, &1.00001, Round::Up);
        assert_eq!(f, 1.125);
        assert_eq!(dir, Ordering::Greater);
    }

    #[test]
    #[should_panic(expected = "minimum larger than maximum")]
    fn check_clamping_panic() {
        let mut f = Float::new(4);
        f.assign(-1);
        // Both 1.00001 and 0.99999 would be rounded to 1.0, but one
        // would be larger and the other would be smaller.
        f.clamp(&1.00001, &0.99999);
    }

    #[test]
    fn check_formatting() {
        let mut f = Float::with_val(53, Special::Zero);
        assert_eq!(format!("{}", f), "0.0");
        assert_eq!(format!("{:?}", f), "0.0");
        assert_eq!(format!("{:+?}", f), "+0.0");
        f.assign(Special::NegZero);
        assert_eq!(format!("{}", f), "-0.0");
        assert_eq!(format!("{:?}", f), "-0.0");
        assert_eq!(format!("{:+?}", f), "-0.0");
        f.assign(Special::Infinity);
        assert_eq!(format!("{}", f), "inf");
        assert_eq!(format!("{:+}", f), "+inf");
        assert_eq!(format!("{:x}", f), "@inf@");
        f.assign(Special::NegInfinity);
        assert_eq!(format!("{}", f), "-inf");
        assert_eq!(format!("{:x}", f), "-@inf@");
        f.assign(Special::Nan);
        assert_eq!(format!("{}", f), "NaN");
        assert_eq!(format!("{:+}", f), "+NaN");
        assert_eq!(format!("{:x}", f), "@NaN@");
        f = -f;
        assert_eq!(format!("{}", f), "-NaN");
        assert_eq!(format!("{:x}", f), "-@NaN@");
        f.assign(-27);
        assert_eq!(format!("{:.2}", f), "-2.7e1");
        assert_eq!(format!("{:.4?}", f), "-2.700e1");
        assert_eq!(format!("{:.4e}", f), "-2.700e1");
        assert_eq!(format!("{:.4E}", f), "-2.700E1");
        assert_eq!(format!("{:.8b}", f), "-1.1011000e4");
        assert_eq!(format!("{:.3b}", f), "-1.11e4");
        assert_eq!(format!("{:#.8b}", f), "-0b1.1011000e4");
        assert_eq!(format!("{:.2o}", f), "-3.3e1");
        assert_eq!(format!("{:#.2o}", f), "-0o3.3e1");
        assert_eq!(format!("{:.2x}", f), "-1.b@1");
        assert_eq!(format!("{:.2X}", f), "-1.B@1");
        assert_eq!(format!("{:12.1x}", f), "      -1.b@1");
        assert_eq!(format!("{:012.3X}", f), "-000001.B0@1");
        assert_eq!(format!("{:#012.2x}", f), "-0x00001.b@1");
        assert_eq!(format!("{:#12.2X}", f), "    -0x1.B@1");
    }

    #[test]
    fn check_assumptions() {
        assert_eq!(unsafe { mpfr::custom_get_size(64) }, 8);
        assert!(
            unsafe { mpfr::custom_get_size(32) } <= gmp::NUMB_BITS as usize
        );
    }

    #[cfg(feature = "rand")]
    struct OnesZerosRand {
        one_words: u32,
    }

    #[cfg(feature = "rand")]
    impl RandGen for OnesZerosRand {
        fn gen(&mut self) -> u32 {
            if self.one_words > 0 {
                self.one_words -= 1;
                !0
            } else {
                0
            }
        }
    }

    #[cfg(feature = "rand")]
    #[test]
    fn check_nan_random_bits() {
        // Least significant 64 bits (two 32-bit words) of mantissa
        // will be ones, all others will be zeros. With 256 bits of
        // precision, the "random" number will be 0.0{192}1{64}. This
        // will be normalized to 0.1{64} * 2^-192.
        for i in 0..2 {
            let mut zeros_ones = OnesZerosRand { one_words: 2 };
            let mut rand = RandState::new_custom(&mut zeros_ones);
            let save_emin;
            unsafe {
                save_emin = mpfr::get_emin();
                mpfr::set_emin(-192 + i);
            }
            let f = Float::with_val(256, Float::random_bits(&mut rand));
            if i == 0 {
                assert_eq!(f, Float::with_val(64, !0u64) >> 256);
            } else {
                assert!(f.is_nan());
            }
            unsafe {
                mpfr::set_emin(save_emin);
            }
        }
    }
}