maph 0.8.1

Maths package for use with personal stuff, but like, you can totally use it! If you want! There's even some light documentation now!
Documentation
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
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::num::{ParseIntError};
use std::str::FromStr;
use std::ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign, Neg, BitXor};
use super::surd::{surd32, surd64};
use super::{Identity, Sqroot, Magnitude};

///Rational number type based on two u32s and a bool. Contains sign, numerator and denominator.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct r32 {
    ///Sign of the number.
    pub sign: bool,
    ///Numerator.
    pub n: u32,
    ///Denominator.
    pub d: u32
}
impl r32 {
    ///Returns a new r32 based on sign, numerator and denominator - doesn't try to simplify.
    pub fn new_raw(sign: bool, n: u32, d: u32) -> Self { Self { sign, n, d } }
    ///Returns a new r32 based on sign, numerator and denominator - will simplify down.
    pub fn new(sign: bool, n: u32, d: u32) -> Self {
        if d == 0 { panic!("Denominator can never be zero - tried to create fraction {}/{}", n, d); }
        let g = super::factors::gcd32(n, d);
        Self { sign, n: n/g, d: d/g } 
    }
    ///Tries to return a new r32 based on sign and two strings, the integer and fractional parts.
    pub fn int_dec(sign: bool, int: &str, dec: &str) -> Result<Self, ParseIntError> {
        let total = int.to_owned() + dec;
        Ok(Self::new(sign, total.parse::<u32>()?, 10_u32.pow(dec.len() as u32)))
    }
    ///Absolute value of the r32.
    pub fn abs(&self) -> r32 { r32::new(true, self.n, self.d) }
    ///Returns 1 if positive, -1 if negative.
    pub fn signum(&self) -> i32 { match self.sign { true => 1, false => -1 } }
    ///Returns the numerator as an i32 - will be negative if sign is negative.
    pub fn numerator_i32(&self) -> i32 { match self.sign { true => self.n as i32, false => -1 * self.n as i32 } }
    ///Returns the denominator as an i32 - will always be positive, though.
    pub fn denominator_i32(&self) -> i32 { self.d as i32 }
    ///Returns numerator as a u32.
    pub fn numerator_u32(&self) -> u32 { self.n }
    ///Returns denominator as a u32.
    pub fn denominator_u32(&self) -> u32 { self.d }
    ///Returns true if positive, false if negative.
    pub fn is_positive(&self) -> bool { self.sign }
    ///Returns true if negative, false if positive.
    pub fn is_negative(&self) -> bool { !self.sign }
    ///Returns reciprocal r32.
    pub fn reciprocal(&self) -> r32 { Self::new(self.sign, self.d, self.n) }
    ///Returns square root as a surd.
    pub fn surd_sqrt(&self) -> super::surd::surd32 {
        super::surd::surd32::new(Self::new(true, 1, self.d), self.n*self.d)
    }
}
impl Default for r32 {
    fn default() -> Self { Self::new(true, 0, 1) }
}
impl Identity for r32 {
    fn identity() -> Self { Self::new(true, 1, 1) }
}
impl Sqroot for r32 {
    type Output = surd32;
    fn sqroot(&self) -> surd32 {
        self.surd_sqrt()
    }
}
impl Magnitude for r32 {
    type Output = r32;
    fn mag(&self) -> r32 { self.abs() }
}
impl PartialOrd for r32 {
    fn partial_cmp(&self, other: &r32) -> Option<Ordering> {
        if self == other { return Some(Ordering::Equal); }
        else {
            let d = super::factors::lcm32(self.d, other.d);
            let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
            match (self.sign, !other.sign) {
                (true, true) => Some(Ordering::Greater),
                (true, false) => match n1 > n0 {
                    true => Some(Ordering::Less),
                    false => Some(Ordering::Greater),
                },
                (false, true) => match n0 > n1 {
                    true => Some(Ordering::Less),
                    false => Some(Ordering::Greater),
                },
                (false, false) => Some(Ordering::Less)
            }
        }
    }
}
impl Ord for r32 {
    fn cmp(&self, other: &Self) -> Ordering {
        if self == other { return Ordering::Equal; }
        else {
            match (self.sign, !other.sign) {
                (true, true) => Ordering::Greater,
                (false, false) => Ordering::Less,
                (true, false) => {
                    let d = super::factors::lcm32(self.d, other.d);
                    let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
                    match n1 > n0 {
                        true => Ordering::Less,
                        false => Ordering::Greater,
                    }
                },
                (false, true) => {
                    let d = super::factors::lcm32(self.d, other.d);
                    let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
                    match n0 > n1 {
                        true => Ordering::Less,
                        false => Ordering::Greater,
                    }
                },
            }
        }
    }
}
impl From<f32> for r32 {
    fn from(f: f32) -> r32 {
        let frac = f.fract();
        let (sign, dec) = match frac >= 0.0 {
            true => (true, frac),
            false => (false, -frac),
        };
        let n = dec.to_string().len().min(u16::MAX as usize);
        let m = 10.0_f64.powf(n as f64);
        let nf = f as f64 * m;
        Self::new_raw(sign, nf.round() as u32, m.round() as u32)
    }
}
impl From<(i32, i32)> for r32 {
    fn from(nd: (i32, i32)) -> r32 {
        let s = !nd.0.is_positive().bitxor(nd.1.is_positive());
        r32::new(s, nd.0.abs() as u32, nd.1.abs() as u32)
    }
}
impl From<u32> for r32 {
    fn from(u: u32) -> r32 {
        r32::new(true, u, 1)
    }
}
impl From<i32> for r32 {
    fn from(i: i32) -> r32 {
        r32::new(i.is_positive(), i.abs() as u32, 1)
    }
}
impl From<r32> for f32 {
    fn from(r: r32) -> f32 {
        match r.sign { true => r.n as f32 / r.d as f32, false => -(r.n as f32) / r.d as f32 }
    }
}
impl From<r32> for f64 {
    fn from(r: r32) -> f64 {
        match r.sign { true => r.n as f64 / r.d as f64, false => -(r.n as f64) / r.d as f64 }
    }
}
impl FromStr for r32 {
    type Err = ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (sign, string) = match s.strip_prefix('-') {
            Some(s0) => (false, s0),
            None => (true, s)
        };
        let parts: Vec<&str> = string.split('.').collect();
        match (parts.get(0), parts.get(1)) {
            (Some(int), Some(dec)) => r32::int_dec(sign, int, dec),
            _ => r32::int_dec(sign, string, "")
        }
    }
}
impl Display for r32 {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let sign = match self.sign { true => "", false => "-" };
        write!(f, "{}{}/{}", sign, self.n, self.d)
    }
}
impl Add<r32> for r32 {
    type Output = r32;
    fn add(self, other: r32) -> r32 {
        let d = super::factors::lcm32(self.d, other.d);
        let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
        match (self.sign, other.sign) {
            (true, true) => Self::new(true, n0 + n1, d),
            (true, false) => match n1 > n0 {
                true => Self::new(false, n1 - n0, d),
                false => Self::new(true, n0 - n1, d),
            },
            (false, true) => match n0 > n1 {
                true => Self::new(false, n0 - n1, d),
                false => Self::new(true, n1 - n0, d),
            },
            (false, false) => Self::new(false, n0 + n1, d)
        }
    }
}
impl Neg for r32 {
    type Output = r32;
    fn neg(self) -> r32 { r32::new(!self.sign, self.n, self.d) } 
}
impl Sub<r32> for r32 {
    type Output = r32;
    fn sub(self, other: r32) -> r32 {
        let d = super::factors::lcm32(self.d, other.d);
        let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
        match (self.sign, !other.sign) {
            (true, true) => Self::new(true, n0 + n1, d),
            (true, false) => match n1 > n0 {
                true => Self::new(false, n1 - n0, d),
                false => Self::new(true, n0 - n1, d),
            },
            (false, true) => match n0 > n1 {
                true => Self::new(false, n0 - n1, d),
                false => Self::new(true, n1 - n0, d),
            },
            (false, false) => Self::new(false, n0 + n1, d)
        }
    }
}
impl Mul<r32> for r32 {
    type Output = r32;
    fn mul(self, other: r32) -> r32 { r32::new(!self.sign.bitxor(other.sign), self.n*other.n, self.d*other.d) }
}
impl Div<r32> for r32 {
    type Output = r32;
    fn div(self, other: r32) -> r32 { r32::new(!self.sign.bitxor(other.sign), self.n*other.d, self.d*other.n) }
}
impl Mul<i32> for r32 {
    type Output = r32;
    fn mul(self, other: i32) -> r32 { r32::new(!other.is_positive().bitxor(self.sign), self.n*other.abs() as u32, self.d) }
}
impl Mul<u32> for r32 {
    type Output = r32;
    fn mul(self, other: u32) -> r32 { r32::new(self.sign, self.n*other, self.d) }
}
impl Div<i32> for r32 {
    type Output = r32;
    fn div(self, other: i32) -> r32 { r32::new(!other.is_positive().bitxor(self.sign), self.n, self.d*other.abs() as u32) }
}
impl Div<u32> for r32 {
    type Output = r32;
    fn div(self, other: u32) -> r32 { r32::new(self.sign, self.n, self.d*other) }
}
impl Add<&r32> for r32 { type Output = r32; fn add(self, other: &r32) -> r32 { self + *other } }
impl Add<r32> for &r32 { type Output = r32; fn add(self, other: r32) -> r32 { *self + other } }
impl Add<&r32> for &r32 { type Output = r32; fn add(self, other: &r32) -> r32 { *self + *other } }
impl AddAssign<r32> for r32 { fn add_assign(&mut self, other: r32) { *self = *self + other; } }
impl AddAssign<&r32> for r32 { fn add_assign(&mut self, other: &r32) { *self = *self + other; } }
impl Sub<&r32> for r32 { type Output = r32; fn sub(self, other: &r32) -> r32 { self - *other } }
impl Sub<r32> for &r32 { type Output = r32; fn sub(self, other: r32) -> r32 { *self - other } }
impl Sub<&r32> for &r32 { type Output = r32; fn sub(self, other: &r32) -> r32 { *self - *other } }
impl SubAssign<r32> for r32 { fn sub_assign(&mut self, other: r32) { *self = *self - other; } }
impl SubAssign<&r32> for r32 { fn sub_assign(&mut self, other: &r32) { *self = *self - other; } }
impl Mul<&r32> for r32 { type Output = r32; fn mul(self, other: &r32) -> r32 { self * *other } }
impl Mul<r32> for &r32 { type Output = r32; fn mul(self, other: r32) -> r32 { *self * other } }
impl Mul<&r32> for &r32 { type Output = r32; fn mul(self, other: &r32) -> r32 { *self * *other } }
impl MulAssign<r32> for r32 { fn mul_assign(&mut self, other: r32) { *self = *self * other; } }
impl MulAssign<&r32> for r32 { fn mul_assign(&mut self, other: &r32) { *self = *self * other; } }
impl Div<&r32> for r32 { type Output = r32; fn div(self, other: &r32) -> r32 { self / *other } }
impl Div<r32> for &r32 { type Output = r32; fn div(self, other: r32) -> r32 { *self / other } }
impl Div<&r32> for &r32 { type Output = r32; fn div(self, other: &r32) -> r32 { *self / *other } }
impl DivAssign<r32> for r32 { fn div_assign(&mut self, other: r32) { *self = *self / other; } }
impl DivAssign<&r32> for r32 { fn div_assign(&mut self, other: &r32) { *self = *self / other; } }
impl Mul<&i32> for r32 { type Output = r32; fn mul(self, other: &i32) -> r32 { self * *other } }
impl Mul<i32> for &r32 { type Output = r32; fn mul(self, other: i32) -> r32 { *self * other } }
impl Mul<&i32> for &r32 { type Output = r32; fn mul(self, other: &i32) -> r32 { *self * *other } }
impl MulAssign<i32> for r32 { fn mul_assign(&mut self, other: i32) { *self = *self * other; } }
impl MulAssign<&i32> for r32 { fn mul_assign(&mut self, other: &i32) { *self = *self * other; } }
impl Div<&i32> for r32 { type Output = r32; fn div(self, other: &i32) -> r32 { self / *other } }
impl Div<i32> for &r32 { type Output = r32; fn div(self, other: i32) -> r32 { *self / other } }
impl Div<&i32> for &r32 { type Output = r32; fn div(self, other: &i32) -> r32 { *self / *other } }
impl DivAssign<i32> for r32 { fn div_assign(&mut self, other: i32) { *self = *self / other; } }
impl DivAssign<&i32> for r32 { fn div_assign(&mut self, other: &i32) { *self = *self / other; } }
impl Mul<&u32> for r32 { type Output = r32; fn mul(self, other: &u32) -> r32 { self * *other } }
impl Mul<u32> for &r32 { type Output = r32; fn mul(self, other: u32) -> r32 { *self * other } }
impl Mul<&u32> for &r32 { type Output = r32; fn mul(self, other: &u32) -> r32 { *self * *other } }
impl MulAssign<u32> for r32 { fn mul_assign(&mut self, other: u32) { *self = *self * other; } }
impl MulAssign<&u32> for r32 { fn mul_assign(&mut self, other: &u32) { *self = *self * other; } }
impl Div<&u32> for r32 { type Output = r32; fn div(self, other: &u32) -> r32 { self / *other } }
impl Div<u32> for &r32 { type Output = r32; fn div(self, other: u32) -> r32 { *self / other } }
impl Div<&u32> for &r32 { type Output = r32; fn div(self, other: &u32) -> r32 { *self / *other } }
impl DivAssign<u32> for r32 { fn div_assign(&mut self, other: u32) { *self = *self / other; } }
impl DivAssign<&u32> for r32 { fn div_assign(&mut self, other: &u32) { *self = *self / other; } }

///Rational number type based on two u64s and a bool. Contains sign, numerator and denominator.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct r64 {
    ///Sign of the number.
    pub sign: bool,
    ///Numerator.
    pub n: u64,
    ///Denominator.
    pub d: u64
}
impl r64 {
    ///Returns a new r64 based on sign, numerator and denominator - doesn't try to simplify.
    pub fn new_raw(sign: bool, n: u64, d: u64) -> Self { Self { sign, n, d } }
    ///Returns a new r64 based on sign, numerator and denominator - will simplify down.
    pub fn new(sign: bool, n: u64, d: u64) -> Self { 
        if d == 0 { panic!("Denominator can never be zero."); }
        let g = super::factors::gcd64(n, d);
        Self { sign, n: n/g, d: d/g } 
    }
    ///Tries to return a new r64 based on sign and two strings, the integer and fractional parts.
    pub fn int_dec(sign: bool, int: &str, dec: &str) -> Result<Self, ParseIntError> {
        let total = int.to_owned() + dec;
        Ok(Self::new(sign, total.parse::<u64>()?, 10_u64.pow(dec.len() as u32)))
    }
    ///Absolute value of the r64.
    pub fn abs(&self) -> r64 { r64::new(true, self.n, self.d) }
    ///Returns 1 if positive, -1 if negative.
    pub fn signum(&self) -> i64 { match self.sign { true => 1, false => -1 } }
    ///Returns the numerator as an i64 - will be negative if sign is negative.
    pub fn numerator_i64(&self) -> i64 { match self.sign { true => self.n as i64, false => -1 * self.n as i64 } }
    ///Returns the denominator as an i64 - will always be positive, though.
    pub fn denominator_i64(&self) -> i64 { self.d as i64 }
    ///Returns numerator as a u64.
    pub fn numerator_u64(&self) -> u64 { self.n }
    ///Returns denominator as a u64.
    pub fn denominator_u64(&self) -> u64 { self.d }
    ///Returns true if positive, false if negative.
    pub fn is_positive(&self) -> bool { self.sign }
    ///Returns true if negative, false if positive.
    pub fn is_negative(&self) -> bool { !self.sign }
    ///Returns reciprocal r64.
    pub fn reciprocal(&self) -> r64 { Self::new(self.sign, self.d, self.n) }
    ///Returns square root as a surd.
    pub fn surd_sqrt(&self) -> super::surd::surd64 {
        super::surd::surd64::new(Self::new(true, 1, self.d), self.n*self.d)
    }
}
impl Default for r64 {
    fn default() -> Self { Self::new(true, 0, 1) }
}
impl Identity for r64 {
    fn identity() -> Self { Self::new(true, 1, 1) }
}
impl Sqroot for r64 {
    type Output = surd64;
    fn sqroot(&self) -> surd64 {
        self.surd_sqrt()
    }
}
impl Magnitude for r64 {
    type Output = r64;
    fn mag(&self) -> r64 { self.abs() }
}
impl PartialOrd for r64 {
    fn partial_cmp(&self, other: &r64) -> Option<Ordering> {
        if self == other { return Some(Ordering::Equal); }
        else {
            let d = super::factors::lcm64(self.d, other.d);
            let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
            match (self.sign, !other.sign) {
                (true, true) => Some(Ordering::Greater),
                (true, false) => match n1 > n0 {
                    true => Some(Ordering::Less),
                    false => Some(Ordering::Greater),
                },
                (false, true) => match n0 > n1 {
                    true => Some(Ordering::Less),
                    false => Some(Ordering::Greater),
                },
                (false, false) => Some(Ordering::Less)
            }
        }
    }
}
impl Ord for r64 {
    fn cmp(&self, other: &Self) -> Ordering {
        if self == other { return Ordering::Equal; }
        else {
            match (self.sign, !other.sign) {
                (true, true) => Ordering::Greater,
                (false, false) => Ordering::Less,
                (true, false) => {
                    let d = super::factors::lcm64(self.d, other.d);
                    let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
                    match n1 > n0 {
                        true => Ordering::Less,
                        false => Ordering::Greater,
                    }
                },
                (false, true) => {
                    let d = super::factors::lcm64(self.d, other.d);
                    let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
                    match n0 > n1 {
                        true => Ordering::Less,
                        false => Ordering::Greater,
                    }
                },
            }
        }
    }
}
impl From<f64> for r64 {
    fn from(f: f64) -> r64 {
        let frac = f.fract();
        let (sign, dec) = match frac >= 0.0 {
            true => (true, frac),
            false => (false, -frac),
        };
        r64::new(sign, (f.abs() - dec) as u64, dec as u64)
    }
}
impl From<(i64, i64)> for r64 {
    fn from(nd: (i64, i64)) -> r64 {
        let s = !nd.0.is_positive().bitxor(nd.1.is_positive());
        r64::new(s, nd.0.abs() as u64, nd.1.abs() as u64)
    }
}
impl From<u64> for r64 {
    fn from(u: u64) -> r64 {
        r64::new(true, u, 1)
    }
}
impl From<i64> for r64 {
    fn from(i: i64) -> r64 {
        r64::new(i.is_positive(), i.abs() as u64, 1)
    }
}
impl From<r64> for f32 {
    fn from(r: r64) -> f32 {
        match r.sign { true => r.n as f32 / r.d as f32, false => -(r.n as f32) / r.d as f32 }
    }
}
impl From<r64> for f64 {
    fn from(r: r64) -> f64 {
        match r.sign { true => r.n as f64 / r.d as f64, false => -(r.n as f64) / r.d as f64 }
    }
}
impl FromStr for r64 {
    type Err = ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (sign, string) = match s.strip_prefix('-') {
            Some(s0) => (false, s0),
            None => (true, s)
        };
        let parts: Vec<&str> = string.split('.').collect();
        match (parts.get(0), parts.get(1)) {
            (Some(int), Some(dec)) => r64::int_dec(sign, int, dec),
            _ => r64::int_dec(sign, string, "")
        }
    }
}
impl Display for r64 {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let sign = match self.sign { true => "", false => "-" };
        write!(f, "{}{}/{}", sign, self.n, self.d)
    }
}
impl Add<r64> for r64 {
    type Output = r64;
    fn add(self, other: r64) -> r64 {
        let d = super::factors::lcm64(self.d, other.d);
        let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
        match (self.sign, other.sign) {
            (true, true) => Self::new(true, n0 + n1, d),
            (true, false) => match n1 > n0 {
                true => Self::new(false, n1 - n0, d),
                false => Self::new(true, n0 - n1, d),
            },
            (false, true) => match n0 > n1 {
                true => Self::new(false, n0 - n1, d),
                false => Self::new(true, n1 - n0, d),
            },
            (false, false) => Self::new(false, n0 + n1, d)
        }
    }
}
impl Neg for r64 {
    type Output = r64;
    fn neg(self) -> r64 { r64::new(!self.sign, self.n, self.d) } 
}
impl Sub<r64> for r64 {
    type Output = r64;
    fn sub(self, other: r64) -> r64 {
        let d = super::factors::lcm64(self.d, other.d);
        let (n0, n1) = ((self.n * (d/self.d)), (other.n * (d/other.d)));
        match (self.sign, !other.sign) {
            (true, true) => Self::new(true, n0 + n1, d),
            (true, false) => match n1 > n0 {
                true => Self::new(false, n1 - n0, d),
                false => Self::new(true, n0 - n1, d),
            },
            (false, true) => match n0 > n1 {
                true => Self::new(false, n0 - n1, d),
                false => Self::new(true, n1 - n0, d),
            },
            (false, false) => Self::new(false, n0 + n1, d)
        }
    }
}
impl Mul<r64> for r64 {
    type Output = r64;
    fn mul(self, other: r64) -> r64 { r64::new(!self.sign.bitxor(other.sign), self.n*other.n, self.d*other.d) }
}
impl Div<r64> for r64 {
    type Output = r64;
    fn div(self, other: r64) -> r64 { r64::new(!self.sign.bitxor(other.sign), self.n*other.d, self.d*other.n) }
}
impl Mul<i64> for r64 {
    type Output = r64;
    fn mul(self, other: i64) -> r64 { r64::new(!other.is_positive().bitxor(self.sign), self.n*other.abs() as u64, self.d) }
}
impl Mul<u64> for r64 {
    type Output = r64;
    fn mul(self, other: u64) -> r64 { r64::new(self.sign, self.n*other, self.d) }
}
impl Div<i64> for r64 {
    type Output = r64;
    fn div(self, other: i64) -> r64 { r64::new(!other.is_positive().bitxor(self.sign), self.n, self.d*other.abs() as u64) }
}
impl Div<u64> for r64 {
    type Output = r64;
    fn div(self, other: u64) -> r64 { r64::new(self.sign, self.n, self.d*other) }
}
impl Add<&r64> for r64 { type Output = r64; fn add(self, other: &r64) -> r64 { self + *other } }
impl Add<r64> for &r64 { type Output = r64; fn add(self, other: r64) -> r64 { *self + other } }
impl Add<&r64> for &r64 { type Output = r64; fn add(self, other: &r64) -> r64 { *self + *other } }
impl AddAssign<r64> for r64 { fn add_assign(&mut self, other: r64) { *self = *self + other; } }
impl AddAssign<&r64> for r64 { fn add_assign(&mut self, other: &r64) { *self = *self + other; } }
impl Sub<&r64> for r64 { type Output = r64; fn sub(self, other: &r64) -> r64 { self - *other } }
impl Sub<r64> for &r64 { type Output = r64; fn sub(self, other: r64) -> r64 { *self - other } }
impl Sub<&r64> for &r64 { type Output = r64; fn sub(self, other: &r64) -> r64 { *self - *other } }
impl SubAssign<r64> for r64 { fn sub_assign(&mut self, other: r64) { *self = *self - other; } }
impl SubAssign<&r64> for r64 { fn sub_assign(&mut self, other: &r64) { *self = *self - other; } }
impl Mul<&r64> for r64 { type Output = r64; fn mul(self, other: &r64) -> r64 { self * *other } }
impl Mul<r64> for &r64 { type Output = r64; fn mul(self, other: r64) -> r64 { *self * other } }
impl Mul<&r64> for &r64 { type Output = r64; fn mul(self, other: &r64) -> r64 { *self * *other } }
impl MulAssign<r64> for r64 { fn mul_assign(&mut self, other: r64) { *self = *self * other; } }
impl MulAssign<&r64> for r64 { fn mul_assign(&mut self, other: &r64) { *self = *self * other; } }
impl Div<&r64> for r64 { type Output = r64; fn div(self, other: &r64) -> r64 { self / *other } }
impl Div<r64> for &r64 { type Output = r64; fn div(self, other: r64) -> r64 { *self / other } }
impl Div<&r64> for &r64 { type Output = r64; fn div(self, other: &r64) -> r64 { *self / *other } }
impl DivAssign<r64> for r64 { fn div_assign(&mut self, other: r64) { *self = *self / other; } }
impl DivAssign<&r64> for r64 { fn div_assign(&mut self, other: &r64) { *self = *self / other; } }
impl Mul<&i64> for r64 { type Output = r64; fn mul(self, other: &i64) -> r64 { self * *other } }
impl Mul<i64> for &r64 { type Output = r64; fn mul(self, other: i64) -> r64 { *self * other } }
impl Mul<&i64> for &r64 { type Output = r64; fn mul(self, other: &i64) -> r64 { *self * *other } }
impl MulAssign<i64> for r64 { fn mul_assign(&mut self, other: i64) { *self = *self * other; } }
impl MulAssign<&i64> for r64 { fn mul_assign(&mut self, other: &i64) { *self = *self * other; } }
impl Div<&i64> for r64 { type Output = r64; fn div(self, other: &i64) -> r64 { self / *other } }
impl Div<i64> for &r64 { type Output = r64; fn div(self, other: i64) -> r64 { *self / other } }
impl Div<&i64> for &r64 { type Output = r64; fn div(self, other: &i64) -> r64 { *self / *other } }
impl DivAssign<i64> for r64 { fn div_assign(&mut self, other: i64) { *self = *self / other; } }
impl DivAssign<&i64> for r64 { fn div_assign(&mut self, other: &i64) { *self = *self / other; } }
impl Mul<&u64> for r64 { type Output = r64; fn mul(self, other: &u64) -> r64 { self * *other } }
impl Mul<u64> for &r64 { type Output = r64; fn mul(self, other: u64) -> r64 { *self * other } }
impl Mul<&u64> for &r64 { type Output = r64; fn mul(self, other: &u64) -> r64 { *self * *other } }
impl MulAssign<u64> for r64 { fn mul_assign(&mut self, other: u64) { *self = *self * other; } }
impl MulAssign<&u64> for r64 { fn mul_assign(&mut self, other: &u64) { *self = *self * other; } }
impl Div<&u64> for r64 { type Output = r64; fn div(self, other: &u64) -> r64 { self / *other } }
impl Div<u64> for &r64 { type Output = r64; fn div(self, other: u64) -> r64 { *self / other } }
impl Div<&u64> for &r64 { type Output = r64; fn div(self, other: &u64) -> r64 { *self / *other } }
impl DivAssign<u64> for r64 { fn div_assign(&mut self, other: u64) { *self = *self / other; } }
impl DivAssign<&u64> for r64 { fn div_assign(&mut self, other: &u64) { *self = *self / other; } }