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
use std::cmp;
use std::fmt;
use std::ops;

pub static ZERO: Fraction = Fraction { numerator: 0, denominator: 1, };

pub struct Fraction {
    /// The numerator
    pub numerator: i64,
    /// The denominator
    pub denominator: i64,
}

impl Fraction {
    /// Generates a new `Fraction`
    /// 
    /// It returns a `Fraction` in its fully reduced form,
    /// after calculating the greatest common divisor. 
    /// 
    /// # Panics
    /// Panics if `denominator` is zero (0). 
    /// 
    pub fn new(mut numerator: i64, mut denominator: i64) -> Fraction {
        if denominator == 0 {
            panic!("Zero is an invalid denominator!");
        }

        if denominator < 0 {
            numerator *= -1;
            denominator *= -1;
        }

        let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);

        Fraction {
            numerator: numerator / greatest_common_divisor,
            denominator: denominator / greatest_common_divisor,
        }
    }

    /// Generates a new `Fraction` from an `f64`
    /// 
    /// It returns a `Fraction` in its fully reduced form,
    /// after calculating the greatest common divisor. 
    /// 
    /// Useful for when calculating square-roots,
    /// sine, cosine, tangent functions and more. 
    /// 
    /// # Panics
    /// Panics if the numerator is too large, 'attempt to multiply with overflow'. 
    /// 
    pub fn new_from_decimal(decimal: f64) -> Fraction {
        let denominator = 100_000_f64;
        let numerator = (decimal * denominator).round();

        Fraction::new(numerator as i64, denominator as i64)
    }

    /// Returns the greatest common divisor, as an `i64`. 
    /// 
    /// # Examples
    /// ```
    /// # extern crate oxygen_quark as quark;
    /// # use quark::fraction::Fraction;
    /// # fn main() {
    /// let n1 = 50;
    /// let n2 = 20;
    /// # assert_eq!(10, Fraction::greatest_common_divisor(n1, n2));
    /// # assert_eq!(10, Fraction::greatest_common_divisor(n2, n1));
    /// 
    /// // Prints out "10"
    /// println!("{}", Fraction::greatest_common_divisor(n1, n2));
    /// # }
    /// ```
    /// 
    pub fn greatest_common_divisor(numerator: i64, denominator: i64) -> i64 {
        let mut x = numerator;
        let mut y = denominator;
        while y != 0 {
            let t = y;
            y = x % y;
            x = t;
        }
        x
    }

    /// Returns the reciprocal of the `Fraction` it's called on.
    /// 
    /// Just moves the denominator to the numerator position and
    /// vice versa.
    /// 
    /// # Panics
    /// Panics when being called on a `Fraction` with numerator 0. 
    /// 
    pub fn reciprocal(self) -> Fraction {
        Fraction::new(self.denominator, self.numerator)
    }

    /// Returns the square-root of a `Fraction`. Result is a `Fraction`
    /// 
    pub fn sqrt(&self) -> Fraction {
        let mut decimal_mode = false;
        let numerator = match self.numerator {
            1 => 1,
            4 => 2,
            9 => 3,
            16 => 4,
            25 => 5,
            36 => 6,
            49 => 7,
            64 => 8,
            81 => 9,
            100 => 10,
            121 => 11,
            144 => 12,
            169 => 13,
            196 => 14,
            225 => 15,
            256 => 16,
            289 => 17,
            324 => 18,
            361 => 19,
            400 => 20,
            441 => 21,
            484 => 22,
            529 => 23,
            576 => 24,
            625 => 25,
            676 => 26,
            729 => 27,
            784 => 28,
            841 => 29,
            900 => 30,
            _ => {
                decimal_mode = true;
                self.numerator
            },
        };

        let denominator = match self.denominator {
            1 => 1,
            4 => 2,
            9 => 3,
            16 => 4,
            25 => 5,
            36 => 6,
            49 => 7,
            64 => 8,
            81 => 9,
            100 => 10,
            121 => 11,
            144 => 12,
            169 => 13,
            196 => 14,
            225 => 15,
            256 => 16,
            289 => 17,
            324 => 18,
            361 => 19,
            400 => 20,
            441 => 21,
            484 => 22,
            529 => 23,
            576 => 24,
            625 => 25,
            676 => 26,
            729 => 27,
            784 => 28,
            841 => 29,
            900 => 30,
            _ => {
                decimal_mode = true;
                self.denominator
            },
        };

        if decimal_mode {
            Fraction::new_from_decimal((numerator as f64 / denominator as f64).sqrt())
        } else {
            Fraction::new(numerator, denominator)
        }
    }

    /// Returns an approximated `Fraction` of the cosine (cos) of the `Fraction`
    /// 
    pub fn cosine(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).cos())
    }

    /// Returns an approximated `Fraction` of the arc cosine (cos^-1) of the `Fraction`
    /// 
    pub fn arc_cosine(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).acos())
    }

    /// Returns an approximated `Fraction` of the sine (sin) of the `Fraction`
    /// 
    pub fn sine(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).sin())
    }

    /// Returns an approximated `Fraction` of the arc sine (sin^-1) of the `Fraction`
    /// 
    pub fn arc_sine(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).asin())
    }

    /// Returns an approximated `Fraction` of the tangent (tan) of the `Fraction`
    /// 
    pub fn tangent(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).tan())
    }

    /// Returns an approximated `Fraction` of the arc tangent (tan^-1) of the `Fraction`
    /// 
    pub fn arc_tangent(self) -> Fraction {
        let numerator = self.numerator as f64;
        let denominator = self.denominator as f64;
        Fraction::new_from_decimal((numerator / denominator).atan())
    }
}

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

impl Eq for Fraction {}

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

impl Ord for Fraction {
    fn cmp(&self, other: &Fraction) -> cmp::Ordering {
        if ((self.numerator == other.numerator || self.numerator < other.numerator) && self.denominator > other.denominator)
        || (self.numerator < other.numerator && self.denominator == other.denominator) {
            return cmp::Ordering::Less;
        } else if (self.numerator >= other.numerator && self.denominator == other.denominator)
                || ((self.numerator == other.numerator || self.numerator > other.numerator) && self.denominator < other.denominator) {
            return cmp::Ordering::Greater;
        }

        cmp::Ordering::Equal
    }
}

impl fmt::Debug for Fraction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.numerator == self.denominator || self.numerator == 0 || self.denominator == 1 {
            if self.numerator < 0 {
                write!(f, "{}", self.numerator)
            } else {
                write!(f, "+{}", self.numerator)
            }
        } else {
            if self.numerator < 0 {
                write!(f, "-[{}/{}]", self.numerator * -1, self.denominator)
            } else {
                write!(f, "+[{}/{}]", self.numerator, self.denominator)
            }
        }
    }
}

impl fmt::Display for Fraction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.numerator == self.denominator || self.numerator == 0 || self.denominator == 1 {
            if self.numerator < 0 {
                write!(f, "{}", self.numerator)
            } else {
                write!(f, "+{}", self.numerator)
            }
        } else {
            if self.numerator < 0 {
                write!(f, "-({}/{})", self.numerator * -1, self.denominator)
            } else {
                write!(f, "+({}/{})", self.numerator, self.denominator)
            }
        }
    }
}

impl ops::Add for Fraction {
    type Output = Fraction;
    fn add(self, other: Fraction) -> Fraction {
        if self.denominator == other.denominator {
            Fraction::new(self.numerator + other.numerator, self.denominator)
        } else {
            Fraction::new(self.numerator * other.denominator + other.numerator * self.denominator, self.denominator * other.denominator)
        }
    }
}

impl ops::AddAssign for Fraction {
    fn add_assign(&mut self, other: Fraction) {
        let numerator = self.numerator * other.denominator + other.numerator * self.denominator;
        let denominator = self.denominator * other.denominator;
        
        let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);
        self.numerator = numerator / greatest_common_divisor;
        self.denominator = denominator / greatest_common_divisor;
    }
}

impl ops::Sub for Fraction {
    type Output = Fraction;
    fn sub(self, other: Fraction) -> Fraction {
        if self.denominator == other.denominator {
            Fraction::new(self.numerator - other.numerator, self.denominator)
        } else {
            Fraction::new(self.numerator * other.denominator - other.numerator * self.denominator, self.denominator * other.denominator)
        }
    }
}

impl ops::SubAssign for Fraction {
    fn sub_assign(&mut self, other: Fraction) {
        if self.denominator == other.denominator {
            let numerator = self.numerator - other.numerator;
            let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, self.denominator);

            self.numerator = numerator / greatest_common_divisor;
            self.denominator /= greatest_common_divisor;
        } else {
            let numerator = self.numerator * other.denominator - other.numerator * self.denominator;
            let denominator = self.denominator * other.denominator;
            
            let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);
            self.numerator = numerator / greatest_common_divisor;
            self.denominator = denominator / greatest_common_divisor;
        }
    }
}

impl ops::Mul for Fraction {
    type Output = Fraction;
    fn mul(self, other: Fraction) -> Fraction {
        Fraction::new(self.numerator * other.numerator, self.denominator * other.denominator)
    }
}

impl ops::MulAssign for Fraction {
    fn mul_assign(&mut self, other: Fraction) {
        let numerator = self.numerator * other.numerator;
        let denominator = self.denominator * other.denominator;

        let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);

        self.numerator = numerator / greatest_common_divisor;
        self.denominator = denominator / greatest_common_divisor;
    }
}

impl ops::Div for Fraction {
    type Output = Fraction;
    fn div(self, other: Fraction) -> Fraction {
        Fraction::new(self.numerator * other.denominator, self.denominator * other.numerator)
    }
}

impl ops::DivAssign for Fraction {
    fn div_assign(&mut self, other: Fraction) {
        let numerator = self.numerator * other.denominator;
        let denominator = self.denominator * other.numerator;

        let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);

        self.numerator = numerator / greatest_common_divisor;
        self.denominator = denominator / greatest_common_divisor;
    }
}

impl ops::Neg for Fraction {
    type Output = Fraction;
    fn neg(self) -> Fraction {
        Fraction::new(-self.numerator, self.denominator)
    }
}

impl Copy for Fraction {}

impl Clone for Fraction {
    fn clone(&self) -> Fraction {
        *self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn new_test() {
        let fraction1 = Fraction {
            numerator: 23,
            denominator: 21,
        };

        let fraction2 = Fraction::new(23, 21);

        assert_eq!(fraction1, fraction2);
    }

    #[test]
    fn addition_test() {
        let fraction1 = Fraction::new(5, 2);
        let fraction2 = Fraction::new(4, 3);

        let result_fraction = Fraction::new(23, 6);

        assert_eq!(fraction1 + fraction2, result_fraction);
    }

    #[test]
    fn subtraction_test() {
        let fraction1 = Fraction::new(5, 2);
        let fraction2 = Fraction::new(4, 3);

        let result_fraction1 = Fraction::new(7, 6);
        let result_fraction2 = Fraction::new(-7, 6);

        assert_eq!(fraction1 - fraction2, result_fraction1);
        assert_eq!(fraction2 - fraction1, result_fraction2);
    }

    #[test]
    fn multiplication_test() {
        let fraction1 = Fraction::new(5, 2);
        let fraction2 = Fraction::new(4, 3);

        let result_fraction = Fraction::new(10, 3);

        assert_eq!(fraction1 * fraction2, result_fraction);
    }

    #[test]
    fn division_test() {
        let fraction1 = Fraction::new(5, 2);
        let fraction2 = Fraction::new(4, 3);

        let result_fraction = Fraction::new(15, 8);

        assert_eq!(fraction1 / fraction2, result_fraction);
    }

    #[test]
    fn greatest_common_divisor_test() {
        let numerator = 50;
        let denominator = 20;

        let greatest_common_divisor = Fraction::greatest_common_divisor(numerator, denominator);

        assert_eq!(10, greatest_common_divisor);
    }

    #[test]
    fn negation_test() {
        let fraction = -Fraction::new(5, 2);
        let result_fraction = Fraction::new(-5, 2);

        assert_eq!(fraction, result_fraction);
    }

    #[test]
    fn cosine_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(41267, 50000);

        assert_eq!(result, (fraction1 / fraction2).cosine());
    }

    #[test]
    fn arc_cosine_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(9273, 10000);

        assert_eq!(result, (fraction1 / fraction2).arc_cosine());
    }

    #[test]
    fn sine_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(3529, 6250);

        assert_eq!(result, (fraction1 / fraction2).sine());
    }

    #[test]
    fn arc_sine_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(1287, 2000);

        assert_eq!(result, (fraction1 / fraction2).arc_sine());
    }

    #[test]
    fn tangent_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(34207, 50000);

        assert_eq!(result, (fraction1 / fraction2).tangent());
    }

    #[test]
    fn arc_tangent_test() {
        let fraction1 = Fraction::new(3, 1);
        let fraction2 = Fraction::new(5, 1);

        let result = Fraction::new(27021, 50000);

        assert_eq!(result, (fraction1 / fraction2).arc_tangent());
    }
}