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
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//! Provides implementations of high-accuracy projectively-extended rational numbers
//! and macros for creating them
//!
//! Projectively-extended rationals differ from normal rationals because they have
//! a single, signless infinity and a single, signless zero. This means that `1/0`
//! can be defined as equal to `∞` and `1/∞` equal to `0`.
//!
//! # Infinity
//!
//! For unsigned numbers, `∞` is greater than every number, whereas with signed numbers,
//! `∞` is not comparable to any number but itself. This is because `∞` equals `-∞` so no
//! ordering can exist.
//!
//! # NaN
//!
//! `∞ + ∞`, `∞ - ∞`, `∞ * 0`, `0 * ∞`, `∞ / ∞`, and `0 / 0` are all `NaN`
//!
//! A value of `NaN` in any operation always returns `NaN`. `NaN` is not ordered and
//! is not equal to any number, including itself.
//!
//! # Panics
//!
//! No operation should ever panic. Operations that overflow round each input to a
//! simpler fraction until they can succeed. Any invalid operations should
//! return `NaN` instead of panicking.

#![deny(missing_docs, trivial_casts, unused_macros)]

use std::*;
use std::cmp::*;
use std::ops::*;

/// A macro for creating a new [signed rational](struct.Rational.html) using a given ratio or decimal
///
/// *Floating-point conversions may be wrong due to small rounding errors.*
///
/// # Alternatives
///
/// * `ratio!(n, d)` is equivalent to `Rational::from([n, d])`
/// * `ratio!(n)` is equivalent to `Rational::from(n)`
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # use extended_rational::Rational;
/// # fn main() {
/// let five_thirds = ratio!(5, 3);
/// let neg_seven_and_a_half = ratio!(-7.5);
///
/// assert_eq!(five_thirds, Rational::from([5, 3]));
/// assert_eq!(neg_seven_and_a_half, Rational::new(-15, 2));
/// # }
/// ```
#[macro_export]
macro_rules! ratio {
    ( $ n : expr , $ d : expr ) => ( $crate::Rational::from([$n, $d]) );
    ( $ n : expr ) => ( $crate::Rational::from($n) );
}

/// A macro for creating a new [unsigned rational](struct.URational.html) using a given ratio or decimal
///
/// *Floating-point conversions may be wrong due to small rounding errors.*
///
/// # Panics
///
/// On attempt to create negative rational. *Doesn't panic in optimized builds.*
///
/// # Alternatives
///
/// * `uratio!(n, d)` is equivalent to `ratio!(n, d).try_unsigned()`
/// * `uratio!(n)` is equivalent to `ratio!(n).try_unsigned()`
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # use extended_rational::URational;
/// # fn main() {
/// let five_thirds = uratio!(5, 3);
/// let seventeen = uratio!(17);
///
/// assert_eq!(five_thirds, URational::new(5, 3));
/// assert_eq!(seventeen, URational::new(17, 1));
/// # }
/// ```
#[macro_export]
macro_rules! uratio {
    ( $ n : expr , $ d : expr ) => {{
        $crate::Rational::from([$n, $d]).try_unsigned()
    }};
    ( $ n : expr ) => {{
        $crate::Rational::from($n).try_unsigned()
    }};
}

macro_rules! try_or {
    (continue $x: expr) => {
        match $x {
            Some(x) => x,
            None => continue,
        }
    };
    (return $r: expr; $x: expr) => {
        match $x {
            Some(x) => x,
            None => return $r,
        }
    };
    (return $x: expr) => {
        match $x {
            Some(x) => x,
            None => return None,
        }
    };
}

macro_rules! impl_u_from {
    ($($t: ty)+) => {
        $(
            impl From<$t> for URational {
                /// Create a new unsigned rational with the given value.
                fn from(n: $t) -> URational {
                    URational::new(n as u64, 1)
                }
            }
            impl From<($t, $t)> for URational {
                /// Create a new unsigned rational with the given numerator and denominator tuple.
                fn from(tuple: ($t, $t)) -> URational {
                    let (n, d) = tuple;
                    URational::new(n as u64, d as u64)
                }
            }
            impl From<[$t; 2]> for URational {
                /// Create a new unsigned rational with the given numerator and denominator array.
                fn from(array: [$t; 2]) -> URational {
                    URational::new(array[0] as u64, array[1] as u64)
                }
            }
            impl From<$t> for Rational {
                /// Create a new signed rational with the given value.
                fn from(n: $t) -> Rational {
                    Rational::from(URational::from(n))
                }
            }
            impl From<($t, $t)> for Rational {
                /// Create a new signed rational with the given numerator and denominator tuple.
                fn from(tuple: ($t, $t)) -> Rational {
                    Rational::from(URational::from(tuple))
                }
            }
            impl From<[$t; 2]> for Rational {
                /// Create a new signed rational with the given numerator and denominator array.
                fn from(array: [$t; 2]) -> Rational {
                    Rational::from(URational::from(array))
                }
            }
        )+
    }
}

macro_rules! impl_from {
    ($($t: ty)+) => {
        $(
            impl From<$t> for Rational {
                /// Creates a new signed rational with the given value.
                fn from(n: $t) -> Rational {
                    Rational::new(n as i64, 1)
                }
            }
            impl From<($t, $t)> for Rational {
                /// Creates a new signed rational with the given numerator and denominator tuple.
                fn from(tuple: ($t, $t)) -> Rational {
                    let (n, d) = tuple;
                    Rational::new(n as i64, d as i64)
                }
            }
            impl From<[$t; 2]> for Rational {
                /// Creates a new signed rational with the given numerator and denominator array.
                fn from(array: [$t; 2]) -> Rational {
                    Rational::new(array[0] as i64, array[1] as i64)
                }
            }
        )+
    }
}

macro_rules! impl_float {
    ($($t: ty [$total: expr, $sig: expr])+) => {
        $(
            impl From<URational> for $t {
                /// Creates an approximation of the given unsigned rational.
                fn from(r: URational) -> $t {
                    (r.numerator as $t) / (r.denominator as $t)
                }
            }
            impl From<Rational> for $t {
                /// Creates an approximation of the given signed rational.
                fn from(r: Rational) -> $t {
                    (if r.negative { -1.0 } else { 1.0 }) * (r.unsigned.numerator as $t) / (r.unsigned.denominator as $t)
                }
            }
            impl From<$t> for Rational {
                /// Attempts to approximate the given floating-point number with a signed rational.
                ///
                /// ## Rounding
                ///
                /// * If the exponent is too large, `∞` will be returned
                /// * If the exponent is too small, `0` will be returned
                fn from(f: $t) -> Rational {
                    match f.classify() {
                        std::num::FpCategory::Infinite => return Rational::infinity(),
                        std::num::FpCategory::Nan => return Rational::nan(),
                        std::num::FpCategory::Zero | std::num::FpCategory::Subnormal => return Rational::zero(),
                        _ => (),
                    }

                    let bits = f.to_bits() as u64;
                    let neg = (bits >> ($total - 1)) == 1;
                    let exponent = ((bits >> $sig) & ((1u64 << ($total - 1 - $sig)) - 1)) as i32 - ((1i32 << ($total - 2 - $sig)) - 1) - $sig;
                    let significand = (1u64 << $sig) + (bits & ((1u64 << $sig) - 1));

                    if exponent < 0 {
                        if let Some(modifier) = 1u64.checked_shl(-exponent as u32) {
                            Rational::new_raw(URational::new(significand, modifier), neg)
                        } else {
                            Rational::zero()
                        }
                    } else {
                        if let Some(sig_mod) = significand.checked_shl(exponent as u32) {
                            Rational::new_raw(URational::new(sig_mod, 1), neg)
                        } else {
                            Rational::infinity()
                        }
                    }
                }
            }
        )+
    }
}

macro_rules! impl_ops {
    ($rational: ident; $assign: ident $non: ident, $assign_name: ident $non_name: ident, $f: ident $($b: expr)*) => {
        impl $assign for $rational {
            #[inline]
            fn $assign_name(&mut self, mut other: $rational) {
                self.$f(&mut other $(, $b)*);
            }
        }
        impl $non for $rational {
            type Output = $rational;

            #[inline]
            fn $non_name(self, mut other: $rational) -> $rational {
                let mut r = self;
                r.$f(&mut other$(, $b)*);
                r
            }
        }
    };
    ($assign: ident $non: ident, $assign_name: ident $non_name: ident, $f: ident $($b: expr)*) => {
        impl_ops!(URational; $assign $non, $assign_name $non_name, $f $($b)*);
        impl_ops!(Rational; $assign $non, $assign_name $non_name, $f $($b)*);
    }
}

/// Returns the greatest common divisor of two numbers.
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        let c = b;
        b = a % b;
        a = c;
    }
    a
}

/// Returns the least common multiple of two numbers
/// or `None` if the calculation overflows.
pub fn lcm(a: u64, b: u64) -> Option<u64> {
    a.checked_mul(b/gcd(a, b))
}

/// A type representing an unsigned projectively-extended rational number
///
/// Subtracting a large number from a smaller one always returns `0` unless
/// the larger number is `∞`.
///
/// # Examples
///
/// ```
/// use extended_rational::URational;
///
/// let a = URational::new(3, 17);
/// let b = URational::new(4, 7);
///
/// assert_eq!(a+b, URational::new(89, 119));
/// ```
///
/// Use the [uratio!](macro.uratio.html) macro for more convenient use.
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # #[allow(unused_variables)]
/// # fn main() {
/// let a = uratio!(3, 17);
/// let b = uratio!(4, 7);
/// # }
/// ```
///
/// Or for easy conversions from primitive types.
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # #[allow(unused_variables)]
/// # fn main() {
/// let a = uratio!(343.863);
/// let b = uratio!(2u8);
/// # }
/// ```
#[derive(Copy, Clone)]
pub struct URational {
    numerator: u64,
    denominator: u64,
}

impl URational {
    /// Create a new unsigned rational with the given numerator and denominator.
    pub fn new(numerator: u64, denominator: u64) -> URational {
        let mut r = URational {
            numerator,
            denominator,
        };
        r.simplify();
        r
    }

    /// Returns the numerator of this rational.
    #[inline(always)] pub fn numerator(self) -> u64 { self.numerator }

    /// Returns the numerator of this rational mutably.
    #[inline(always)] pub fn numerator_mut(&mut self) -> &mut u64 { &mut self.numerator }

    /// Returns the denominator of this rational.
    #[inline(always)] pub fn denominator(self) -> u64 { self.denominator }

    /// Returns the denominator of this rational mutably.
    #[inline(always)] pub fn denominator_mut(&mut self) -> &mut u64 { &mut self.denominator }

    /// Returns the smallest value an unsigned rational can store.
    #[inline(always)] pub fn min_value() -> URational { URational { numerator: 0, denominator: 1 } }

    /// Returns the smallest non-zero value an unsigned rational can store.
    #[inline(always)] pub fn min_pos_value() -> URational { URational { numerator: 1, denominator: u64::MAX } }

    /// Returns the largest value an unsigned rational can store.
    #[inline(always)] pub fn max_value() -> URational { URational { numerator: u64::MAX, denominator: 1 } }

    /// Returns an unsigned rational representing `NaN`.
    #[inline(always)] pub fn nan() -> URational { URational { numerator: 0, denominator: 0 } }

    /// Returns an unsigned rational representing `0`.
    #[inline(always)] pub fn zero() -> URational { URational { numerator: 0, denominator: 1 } }

    /// Returns an unsigned rational representing `∞`.
    #[inline(always)] pub fn infinity() -> URational { URational { numerator: 1, denominator: 0 } }

    /// Returns an unsigned rational representing `1`.
    #[inline(always)] pub fn one() -> URational { URational { numerator: 1, denominator: 1 } }

    /// Returns `true` if this rational is `NaN` and `false` otherwise.
    #[inline] pub fn is_nan(self) -> bool { self.numerator == 0 && self.denominator == 0 }

    /// Returns `true` if this rational is `0` and `false` otherwise.
    #[inline] pub fn is_zero(self) -> bool { self.numerator == 0 && self.denominator != 0 }

    /// Returns `true` if this rational is `∞` and `false` otherwise.
    #[inline] pub fn is_infinity(self) -> bool { self.numerator != 0 && self.denominator == 0 }

    /// Returns `true` if this rational is a signed number (not `NaN`, `0`, or `∞`) and `false` otherwise.
    #[inline] pub fn is_signed(self) -> bool { self.numerator != 0 && self.denominator != 0 }

    /// Returns the reciprocal of this rational.
    #[inline] pub fn reciprocal(self) -> URational { URational { numerator: self.denominator, denominator: self.numerator } }

    /// Returns the complexity of this rational (max of numerator and denominator).
    #[inline] pub fn complexity(self) -> u64 { max(self.numerator, self.denominator) }

    /// Returns this rational with no fractional component by rounding down.
    pub fn floor(self) -> URational {
        if self.denominator != 0 {
            URational {
                numerator: self.numerator / self.denominator,
                denominator: 1,
            }
        } else {
            self
        }
    }

    /// Returns this rational with no fractional component by rounding.
    pub fn round(self) -> URational {
        if self.denominator != 0 {
            if (self.numerator % self.denominator) > self.denominator/2 {
                URational {
                    numerator: (self.numerator / self.denominator) + 1,
                    denominator: 1,
                }
            } else {
                URational {
                    numerator: self.numerator / self.denominator,
                    denominator: 1,
                }
            }
        } else {
            self
        }
    }

    /// Returns this rational with no fractional component by rounding up.
    pub fn ceil(self) -> URational {
        if self.denominator != 0 {
            if (self.numerator % self.denominator) != 0 {
                URational {
                    numerator: (self.numerator / self.denominator) + 1,
                    denominator: 1,
                }
            } else {
                URational {
                    numerator: self.numerator / self.denominator,
                    denominator: 1,
                }
            }
        } else {
            self
        }
    }

    /// Computes `self + other`, returning `None` if rounding occurred.
    pub fn add_exact(mut self, mut other: URational) -> Option<URational> {
        if self.add_sub_exact(&mut other, false) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self - other`, returning `None` if rounding occurred.
    pub fn sub_exact(mut self, mut other: URational) -> Option<URational> {
        if self.add_sub_exact(&mut other, true) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self * other`, returning `None` if rounding occurred.
    pub fn mul_exact(mut self, mut other: URational) -> Option<URational> {
        if self.mul_div_exact(&mut other, false) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self / other`, returning `None` if rounding occurred.
    pub fn div_exact(mut self, mut other: URational) -> Option<URational> {
        if self.mul_div_exact(&mut other, true) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self % other`, returning `None` if rounding occurred.
    pub fn rem_exact(mut self, mut other: URational) -> Option<URational> {
        if self.rem_div_exact(&mut other) {
            Some(self)
        } else {
            None
        }
    }

    fn simplify(&mut self) {
        let common = gcd(self.numerator, self.denominator);
        if common > 1 {
            self.numerator /= common;
            self.denominator /= common;
        }
    }

    fn shift_partial(a: &mut u64) {
        if *a & 0b11 == 0b11 {
            *a >>= 1;
            *a += 1;
        } else {
            *a >>= 1;
        }
    }

    fn shift(&mut self) {
        URational::shift_partial(&mut self.numerator);
        URational::shift_partial(&mut self.denominator);
        self.simplify();
    }

    fn add_sub_exact(&mut self, other: &mut URational, sub: bool) -> bool {
        if sub && other > self && !other.is_infinity() {
            *self = URational::zero();
            return true;
        } else if self.denominator == 0 && other.denominator == 0 {
            *self = URational::nan();
            return true;
        } else if self.denominator == 0 {
            return true;
        } else if other.denominator == 0 {
            *self = *other;
            return true;
        } else if self.denominator == other.denominator {
            self.numerator = if sub {
                try_or!(return false; self.numerator.checked_sub(other.numerator))
            } else {
                try_or!(return false; self.numerator.checked_add(other.numerator))
            };
            self.simplify();
            return true;
        }
        let common = try_or!(return false; lcm(self.denominator, other.denominator));
        let self_mul = common / self.denominator;
        let other_mul = common / other.denominator;
        let n0 = try_or!(return false; self.numerator.checked_mul(self_mul));
        let n1 = try_or!(return false; other.numerator.checked_mul(other_mul));
        self.numerator = if sub {
            try_or!(return false; n0.checked_sub(n1))
        } else {
            try_or!(return false; n0.checked_add(n1))
        };
        self.denominator = common;
        self.simplify();
        true
    }

    fn mul_div_exact(&mut self, other: &mut URational, div: bool) -> bool {
        if div {
            *other = other.reciprocal();
        }
        if self.is_nan() {
            return true;
        } else if other.is_nan() || (self.is_infinity() && other.is_zero()) || (self.is_zero() && other.is_infinity()) {
            *self = URational::nan();
            return true;
        } else if !self.is_signed() {
            return true;
        } else if !other.is_signed() {
            *self = *other;
            return true;
        }
        let ndc = gcd(self.numerator, other.denominator);
        self.numerator /= ndc;
        other.denominator /= ndc;
        let dnc = gcd(self.denominator, other.numerator);
        self.denominator /= dnc;
        other.numerator /= dnc;
        let n = try_or!(return false; self.numerator.checked_mul(other.numerator));
        self.denominator = try_or!(return false; self.denominator.checked_mul(other.denominator));
        self.numerator = n;
        true
    }

    fn rem_div_exact(&mut self, other: &mut URational) -> bool {
        if self.is_nan() {
            return true;
        } else if other.is_nan() {
            *self = URational::nan();
            return true;
        } else if other.is_zero() || self.is_infinity() {
            *self = URational::zero();
            return true;
        } else if other.is_infinity() {
            return true;
        }
        if self.denominator == other.denominator {
            self.numerator = try_or!(return false; self.numerator.checked_rem(other.numerator));
            self.simplify();
            return true;
        }
        let common = try_or!(return false; lcm(self.denominator, other.denominator));
        let self_mul = common / self.denominator;
        let other_mul = common / other.denominator;
        let n0 = try_or!(return false; self.numerator.checked_mul(self_mul));
        let n1 = try_or!(return false; other.numerator.checked_mul(other_mul));
        self.numerator = try_or!(return false; n0.checked_rem(n1));
        self.denominator = common;
        self.simplify();
        true
    }

    fn add_sub(&mut self, other: &mut URational, sub: bool) {
        let mut first = true;
        loop {
            if first {
                first = false;
            } else if self >= other {
                self.shift();
            } else {
                other.shift();
            }
            if URational::add_sub_exact(self, other, sub) {
                return;
            }
        }
    }

    fn mul_div(&mut self, other: &mut URational, div: bool) {
        let mut first = true;
        loop {
            if first {
                first = false;
            } else if self >= other {
                self.shift();
            } else {
                other.shift();
            }
            if URational::mul_div_exact(self, other, div) {
                return;
            }
        }
    }

    fn rem_div(&mut self, other: &mut URational) {
        let mut first = true;
        loop {
            if first {
                first = false;
            } else if self >= other {
                self.shift();
            } else {
                other.shift();
            }
            if URational::rem_div_exact(self, other) {
                return;
            }
        }
    }
}

impl Default for URational {
    fn default() -> URational {
        URational::zero()
    }
}

impl PartialEq for URational {
    fn eq(&self, other: &URational) -> bool {
        self.numerator == other.numerator && self.denominator == other.denominator && (self.numerator != 0 || self.denominator != 0) && (other.numerator != 0 || other.denominator != 0)
    }
}

impl PartialOrd for URational {
    fn partial_cmp(&self, other: &URational) -> Option<Ordering> {
        if self.eq(other) {
            Some(Ordering::Equal)
        } else if (self.numerator == 0 && self.denominator == 0) || (other.numerator == 0 && other.denominator == 0) {
            None
        } else {
            let mut a = *self;
            let mut b = *other;
            let mut first = true;
            loop {
                if first {
                    first = false;
                } else {
                    a.shift();
                    b.shift();
                }
                let nd = try_or!(continue a.numerator.checked_mul(b.denominator));
                let dn = try_or!(continue a.denominator.checked_mul(b.numerator));
                if nd > dn {
                    return Some(Ordering::Greater)
                } else {
                    return Some(Ordering::Less)
                }
            }
        }
    }
}

impl fmt::Display for URational {
    /// Formats the rational.
    ///
    /// # Style
    ///
    /// * `NaN`, `∞`, and whole numbers are written directly
    /// * Ratios with complexities less than 100 are written as fractions (`n/d`)
    /// * All other numbers are written as decimals
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        if self.denominator == 1 {
            write!(f, "{}", self.numerator)
        } else if self.denominator == 0 {
            if self.numerator == 0 {
                write!(f, "NaN")
            } else {
                write!(f, "∞")
            }
        } else if self.complexity() < 100 {
            write!(f, "{}/{}", self.numerator, self.denominator)
        } else {
            write!(f, "{}", self.numerator as f64 / self.denominator as f64)
        }
    }
}

impl fmt::Debug for URational {
    /// Formats the rational.
    ///
    /// # Style
    ///
    /// All numbers are written as fractions in parentheses `(n/d)`
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "({}/{})", self.numerator, self.denominator)
    }
}

/// A type representing a signed projectively-extended rational number
///
/// # Examples
///
/// ```
/// use extended_rational::Rational;
///
/// let a = Rational::new(3, 17);
/// let b = Rational::new(-4, 7);
///
/// assert_eq!(a+b, Rational::new(-47, 119));
/// ```
///
/// Use the [ratio!](macro.ratio.html) macro for more convenient use.
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # #[allow(unused_variables)]
/// # fn main() {
/// let a = ratio!(3, 17);
/// let b = ratio!(-4, 7);
/// # }
/// ```
///
/// Or for easy conversions from primitive types.
///
/// ```
/// # #[macro_use]
/// # extern crate extended_rational;
/// # #[allow(unused_variables)]
/// # fn main() {
/// let a = ratio!(-77.332);
/// let b = ratio!(2u8);
/// # }
/// ```
#[derive(Copy, Clone)]
pub struct Rational {
    unsigned: URational,
    negative: bool,
}

impl Rational {
    /// Create a new signed rational with the given numerator and denominator.
    pub fn new(numerator: i64, denominator: i64) -> Rational {
        let take_sign = |signed: i64| {
            (
                if signed == i64::MIN {
                    i64::MAX as u64 + 1
                } else {
                    signed.abs() as u64
                },
                signed < 0,
            )
        };
        let (n, sn) = take_sign(numerator);
        let (d, sd) = take_sign(denominator);
        Rational::new_raw(URational::new(n, d), sn != sd)
    }

    /// Create a new signed rational with the given unsigned rational and sign.
    pub fn new_raw(unsigned: URational, negative: bool) -> Rational {
        Rational {
            unsigned,
            negative: if unsigned.is_signed() {
                negative
            } else {
                false
            },
        }
    }

    /// Returns the underlying numerator of this rational.
    #[inline(always)] pub fn numerator(self) -> u64 { self.unsigned.numerator }

    /// Returns the underlying numerator of this rational mutably.
    #[inline(always)] pub fn numerator_mut(&mut self) -> &mut u64 { &mut self.unsigned.numerator }

    /// Returns the underlying denominator of this rational.
    #[inline(always)] pub fn denominator(self) -> u64 { self.unsigned.denominator }

    /// Returns the underlying denominator of this rational mutably.
    #[inline(always)] pub fn denominator_mut(&mut self) -> &mut u64 { &mut self.unsigned.denominator }

    /// Returns the underlying sign of this rational.
    #[inline(always)] pub fn sign(self) -> bool { self.negative }

    /// Returns the underlying sign of this rational mutably.
    #[inline(always)] pub fn sign_mut(&mut self) -> &mut bool { &mut self.negative }

    /// Returns the underlying unsigned rational of this rational.
    #[inline(always)] pub fn unsigned(self) -> URational { self.unsigned }

    /// Returns the underlying unsigned rational of this rational mutably.
    #[inline(always)] pub fn unsigned_mut(&mut self) -> &mut URational { &mut self.unsigned }

    /// Returns the underlying unsigned rational of this rational, panicking if sign is negative.
    ///
    /// *Does not panic in optimized builds.*
    #[inline]
    pub fn try_unsigned(self) -> URational {
        debug_assert!(!self.negative, "cannot create a URational with a negative sign.");
        self.unsigned
    }

    /// Returns the smallest value a signed rational can store.
    #[inline(always)] pub fn min_value() -> Rational { Rational { unsigned: URational { numerator: u64::MAX, denominator: 1 }, negative: true } }

    /// Returns the smallest positive value a signed rational can store.
    #[inline(always)] pub fn min_pos_value() -> Rational { Rational { unsigned: URational { numerator: 1, denominator: u64::MAX }, negative: false } }

    /// Returns the largest negative value a signed rational can store.
    #[inline(always)] pub fn max_neg_value() -> Rational { Rational { unsigned: URational { numerator: 1, denominator: u64::MAX }, negative: true } }

    /// Returns the largest value a signed rational can store.
    #[inline(always)] pub fn max_value() -> Rational { Rational { unsigned: URational { numerator: u64::MAX, denominator: 1 }, negative: false } }

    /// Returns a signed rational representing `NaN`.
    #[inline(always)] pub fn nan() -> Rational { Rational { unsigned: URational { numerator: 0, denominator: 0 }, negative: false } }

    /// Returns a signed rational representing `0`.
    #[inline(always)] pub fn zero() -> Rational { Rational { unsigned: URational { numerator: 0, denominator: 1 }, negative: false } }

    /// Returns a signed rational representing `∞`.
    #[inline(always)] pub fn infinity() -> Rational { Rational { unsigned: URational { numerator: 1, denominator: 0 }, negative: false } }

    /// Returns a signed rational representing `1`.
    #[inline(always)] pub fn one() -> Rational { Rational { unsigned: URational { numerator: 1, denominator: 1 }, negative: false } }

    /// Returns a signed rational representing `-1`.
    #[inline(always)] pub fn negative_one() -> Rational { Rational { unsigned: URational { numerator: 1, denominator: 1 }, negative: true } }

    /// Returns `true` if this rational is `NaN` and `false` otherwise.
    #[inline(always)] pub fn is_nan(self) -> bool { self.unsigned.is_nan() }

    /// Returns `true` if this rational is `0` and `false` otherwise.
    #[inline(always)] pub fn is_zero(self) -> bool { self.unsigned.is_zero() }

    /// Returns `true` if this rational is `∞` and `false` otherwise.
    #[inline(always)] pub fn is_infinity(self) -> bool { self.unsigned.is_infinity() }

    /// Returns `true` if this rational is a signed number (not `NaN`, `0`, or `∞`) and `false` otherwise.
    #[inline(always)] pub fn is_signed(self) -> bool { self.unsigned.is_signed() }

    /// Returns `true` if this rational is a negative number and `false` otherwise.
    #[inline(always)] pub fn is_negative(self) -> bool { self.negative }

    /// Returns the reciprocal of this rational.
    #[inline] pub fn reciprocal(self) -> Rational { Rational { unsigned: self.unsigned.reciprocal(), negative: self.negative } }

    /// Returns the negative reciprocal of this rational.
    #[inline] pub fn negative_reciprocal(self) -> Rational { Rational::new_raw( self.unsigned.reciprocal(), !self.negative) }

    /// Returns the complexity of this rational (max of absolute values of numerator and denominator).
    #[inline(always)] pub fn complexity(self) -> u64 { self.unsigned.complexity() }

    /// Returns `true` if this rational is a positive number and `false` otherwise.
    #[inline] pub fn is_positive(self) -> bool { self.unsigned.is_signed() && !self.negative }

    /// Returns this rational with no fractional component by rounding towards zero.
    #[inline]
    pub fn floor(self) -> Rational {
        Rational {
            unsigned: self.unsigned.floor(),
            negative: self.negative,
        }
    }

    /// Returns this rational with no fractional component by rounding.
    #[inline]
    pub fn round(self) -> Rational {
        Rational {
            unsigned: self.unsigned.round(),
            negative: self.negative,
        }
    }

    /// Returns this rational with no fractional component by rounding away from zero.
    #[inline]
    pub fn ceil(self) -> Rational {
        Rational {
            unsigned: self.unsigned.ceil(),
            negative: self.negative,
        }
    }

    /// Returns this rational without a negative sign.
    #[inline]
    pub fn abs(self) -> Rational {
        Rational {
            unsigned: self.unsigned,
            negative: false,
        }
    }

    /// Computes `self + other`, returning `None` if rounding occurred.
    pub fn add_exact(mut self, mut other: Rational) -> Option<Rational> {
        if self.add_sub_exact(&mut other, false) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self - other`, returning `None` if rounding occurred.
    pub fn sub_exact(mut self, mut other: Rational) -> Option<Rational> {
        if self.add_sub_exact(&mut other, true) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self * other`, returning `None` if rounding occurred.
    pub fn mul_exact(mut self, mut other: Rational) -> Option<Rational> {
        if self.mul_div_exact(&mut other, false) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self / other`, returning `None` if rounding occurred.
    pub fn div_exact(mut self, mut other: Rational) -> Option<Rational> {
        if self.mul_div_exact(&mut other, true) {
            Some(self)
        } else {
            None
        }
    }

    /// Computes `self % other`, returning `None` if rounding occurred.
    pub fn rem_exact(mut self, mut other: Rational) -> Option<Rational> {
        if self.rem_div_exact(&mut other) {
            Some(self)
        } else {
            None
        }
    }

    fn check_sign(&mut self) {
        if !self.unsigned.is_signed() {
            self.negative = false;
        }
    }

    fn add_sub_exact(&mut self, other: &mut Rational, sub: bool) -> bool {
        let negative = other.negative != sub;
        if self.negative != negative {
            if self.unsigned >= other.unsigned {
                if !self.unsigned.add_sub_exact(&mut other.unsigned, true) {
                    return false;
                }
            } else {
                self.negative = negative;
                if !other.unsigned.add_sub_exact(&mut self.unsigned, true) {
                    return false;
                }
                self.unsigned = other.unsigned;
            };
        } else {
            if !self.unsigned.add_sub_exact(&mut other.unsigned, false) {
                return false;
            }
        }
        self.check_sign();
        true
    }

    fn mul_div_exact(&mut self, other: &mut Rational, div: bool) -> bool {
        self.negative = self.negative != other.negative;
        if self.unsigned.mul_div_exact(&mut other.unsigned, div) {
            self.check_sign();
            true
        } else {
            false
        }
    }

    fn rem_div_exact(&mut self, other: &mut Rational) -> bool {
        if self.unsigned.rem_div_exact(&mut other.unsigned) {
            self.check_sign();
            true
        } else {
            false
        }
    }

    fn add_sub(&mut self, other: &mut Rational, sub: bool) {
        let negative = other.negative != sub;
        if self.negative != negative {
            if self.unsigned >= other.unsigned {
                self.unsigned.add_sub(&mut other.unsigned, true);
            } else {
                self.negative = negative;
                other.unsigned.add_sub(&mut self.unsigned, true);
                self.unsigned = other.unsigned;
            };
        } else {
            self.unsigned.add_sub(&mut other.unsigned, false);
        }
        self.check_sign();
    }

    fn mul_div(&mut self, other: &mut Rational, div: bool) {
        self.negative = self.negative != other.negative;
        self.unsigned.mul_div(&mut other.unsigned, div);
        self.check_sign();
    }

    fn rem_div(&mut self, other: &mut Rational) {
        self.unsigned.rem_div(&mut other.unsigned);
        self.check_sign();
    }
}

impl Default for Rational {
    fn default() -> Rational {
        Rational::zero()
    }
}

impl PartialEq for Rational {
    fn eq(&self, other: &Rational) -> bool {
        self.unsigned == other.unsigned && (self.negative == other.negative || !(self.is_signed() || other.is_signed()))
    }
}

impl PartialOrd for Rational {
    fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
        if self.eq(other) {
            Some(Ordering::Equal)
        } else if self.is_infinity() || other.is_infinity() {
            None
        } else if let Some(ordering) = self.unsigned.partial_cmp(&other.unsigned) {
            if self.negative != other.negative {
                if self.negative {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            } else if self.negative {
                if ordering == Ordering::Greater {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            } else {
                Some(ordering)
            }
        } else {
            None
        }
    }
}

impl fmt::Display for Rational {
    /// Formats the rational.
    ///
    /// # Style
    ///
    /// * `NaN`, `∞`, and whole numbers are written directly
    /// * Ratios with complexities less than 100 are written as fractions (`n/d`)
    /// * All other numbers are written as decimals
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        if self.negative {
            write!(f, "-{}", self.unsigned)
        } else {
            write!(f, "{}", self.unsigned)
        }
    }
}

impl fmt::Debug for Rational {
    /// Formats the rational.
    ///
    /// # Style
    ///
    /// All numbers are written as fractions in parentheses `(n/d)`
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        if self.negative {
            write!(f, "(-{}/{})", self.unsigned.numerator, self.unsigned.denominator)
        } else {
            write!(f, "({}/{})", self.unsigned.numerator, self.unsigned.denominator)
        }
    }
}

impl_ops!(AddAssign Add, add_assign add, add_sub false);
impl_ops!(SubAssign Sub, sub_assign sub, add_sub true);
impl_ops!(MulAssign Mul, mul_assign mul, mul_div false);
impl_ops!(DivAssign Div, div_assign div, mul_div true);
impl_ops!(RemAssign Rem, rem_assign rem, rem_div);

impl Neg for Rational {
    type Output = Rational;

    fn neg(self) -> Rational {
        Rational {
            unsigned: self.unsigned,
            negative: !self.negative,
        }
    }
}

impl From<URational> for Rational {
    /// Create a new signed rational from an unsigned rational.
    fn from(r: URational) -> Rational {
        Rational::new_raw(r, false)
    }
}

impl From<(URational, bool)> for Rational {
    /// Create a new signed rational from an unsigned rational and a sign.
    fn from(tuple: (URational, bool)) -> Rational {
        let (unsigned, negative) = tuple;
        Rational::new_raw(unsigned, negative)
    }
}

impl_float!(f64 [64, 52] f32 [32, 23]);

impl_u_from!(u64 u32 u16 u8);

impl_from!(i64 i32 i16 i8);