qfall-math 0.1.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
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
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
// Copyright © 2023 Marcel Luca Schmidt, Sven Moog
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementations to create a [`Q`] value from other types.
//!
//! The explicit functions contain the documentation.

use super::Q;
use crate::{
    error::{MathError, StringConversionError},
    integer::Z,
    macros::from::{from_trait, from_type},
    traits::{AsInteger, Pow},
};
use flint_sys::{
    fmpq::{fmpq, fmpq_canonicalise, fmpq_clear, fmpq_get_d, fmpq_set_str},
    fmpz::{fmpz_init_set, fmpz_is_zero},
};
use std::{ffi::CString, str::FromStr};

impl Q {
    /// Creates a rational number of type [`Q`] from a [`f64`].
    /// This function works with the exact float it received as input.
    /// Many numbers like `0.1` are not exactly representable as floats and
    /// will therefore not be instantiated as `1/10`.
    ///
    /// Input parameters:
    /// - `value`: the value the rational number will have, provided as a [`f64`]
    ///
    /// Returns a [`Q`].
    ///
    /// # Examples
    /// ```rust
    /// use qfall_math::rational::Q;
    ///
    /// let a: Q = Q::from_f64(0.3);
    /// let a: Q = Q::from_f64(-123.4567);
    /// ```
    pub fn from_f64(value: f64) -> Self {
        let bits: u64 = value.to_bits();
        let sign = if bits >> 63 == 0 {
            Q::ONE
        } else {
            Q::MINUS_ONE
        };
        let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
        let mantissa = if exponent == 0 {
            (bits & 0xfffffffffffff) << 1
        } else {
            // prepend one bit to the mantissa because the most significant bit is implicit
            (bits & 0xfffffffffffff) | 0x10000000000000
        };

        // -1023 because of the offset representation of the exponent
        // -52 because the mantissa is 52 bit long
        exponent -= 1023 + 52;
        let shift = match exponent {
            // This could be optimized with `fmpz_lshift_mpn` once it is part of flint_sys.
            e if e >= 1 => Q::from(2).pow(e).unwrap(),
            e => Q::from((1, 2)).pow(e.abs()).unwrap(),
        };

        sign * Z::from(mantissa) * shift
    }

    from_type!(f32, f64, Q, Q::from_f64);
}

impl<IntegerNumerator: AsInteger, IntegerDenominator: AsInteger>
    From<(IntegerNumerator, IntegerDenominator)> for Q
{
    /// Creates a [`Q`] from two integers.
    ///
    /// Parameters:
    /// - `num`: the value of the numerator.
    /// - `den`: the value of the denominator.
    ///
    /// Returns a [`Q`].
    ///
    /// # Examples
    /// ```rust
    /// use qfall_math::rational::Q;
    /// use qfall_math::integer::Z;
    ///
    /// let a = Q::from((42, &2));
    /// let b = Q::from((Z::from(84), 4));
    ///
    /// assert_eq!(a, b);
    /// ```
    ///
    /// # Panics ...
    /// - if the denominator is zero.
    fn from((num, den): (IntegerNumerator, IntegerDenominator)) -> Self {
        unsafe {
            let num = num.into_fmpz();
            let den = den.into_fmpz();

            assert_ne!(den.0, 0, "The denominator can not be zero");

            let mut value = fmpq { num, den };
            fmpq_canonicalise(&mut value);

            Q { value }
        }
    }
}

impl<Integer: Into<Z>> From<Integer> for Q {
    /// Creates a [`Q`] from a value that implements [`Into<Z>`].
    ///
    /// Parameters:
    /// - `value`: the initial value the [`Q`] should have.
    ///
    /// Returns a [`Q`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::Q;
    /// use qfall_math::integer::Z;
    ///
    /// let a: Q = Q::from(17);
    /// let b: Q = Q::from(Z::from(17));
    /// ```
    fn from(value: Integer) -> Self {
        let value = value.into();
        // this efficient implementation depends on Q::default instantiating 1 as denominator
        let mut out = Q::default();
        unsafe { fmpz_init_set(&mut out.value.num, &value.value) }
        out
    }
}

impl From<f64> for Q {
    /// Create a new rational number of type [`Q`] from a [`f64`].
    /// This function works with the bit representation of the float it received as input.
    /// Floats like `0.1` that are not completely representable,
    /// will not be instantiated as `1/10`.
    ///
    /// Input parameters:
    /// - `value`: the value the rational number will have, provided as a [`f64`]
    ///
    /// Returns a [`Q`].
    ///
    /// # Examples
    /// ```rust
    /// use qfall_math::rational::Q;
    ///
    /// let a: Q = Q::from(0.3);
    /// let a: Q = Q::from(-123.4567);
    /// ```
    fn from(value: f64) -> Self {
        Q::from_f64(value)
    }
}

from_trait!(f32, Q, Q::from_f32);

impl From<&Q> for f64 {
    /// Convert a rational [`Q`] into an [`f64`].
    /// The value is rounded to the closest [`f64`] representation.
    ///
    /// **WARNING:** The return is system dependent if `self` is
    /// is too large or too small to fit in an [`f64`], i.e. the value should be within
    /// [`f64::MIN`] and [`f64::MAX`]. It the entry can't be represented exactly, it will
    /// be rounded towards zero.
    ///
    /// **WARNING:** Please be aware that the deviation of the representation of `self` as a [`f64`]
    /// will scale with the size of `self`, e.g. a value within the size of `2^{64}`
    /// might deviate from the original value by a distance of `1_000`.
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::Q;
    ///
    /// let one_half = Q::from((1, 2));
    /// let float = f64::from(&one_half);
    ///
    /// assert_eq!(0.5, float);
    /// ```
    fn from(value: &Q) -> Self {
        unsafe { fmpq_get_d(&value.value) }
    }
}

impl From<&Q> for Q {
    /// An alias for [`Q::clone`].
    /// It makes the use of generic [`Into<Q>`] types easier.
    fn from(value: &Q) -> Self {
        value.clone()
    }
}

impl FromStr for Q {
    type Err = MathError;

    /// Creates a [`Q`] rational from a [`String`]
    /// In the string should be two decimal numbers separated by `/`.
    /// Optionally, before one or both of them can be a `-`.
    /// The format of that string looks like this `-12/53`.
    ///
    /// If the number is an integer, the string can be in the format of one.
    /// The format of that string looks like this `-12`.
    /// It is automatically transformed to `-12/1`.
    ///
    /// Parameters:
    /// - `s`: the rational value
    ///
    /// Returns a [`Q`] or an error if the provided string was not formatted
    /// correctly, contained a `Null` byte, or the denominator was `0`.
    ///
    /// # Examples
    /// ```
    /// use std::str::FromStr;
    /// use qfall_math::rational::Q;
    ///  
    /// let a: Q = "100/3".parse().unwrap();
    /// let b: Q = Q::from_str("100/3").unwrap();
    /// ```
    ///
    /// ```
    /// use std::str::FromStr;
    /// use qfall_math::rational::Q;
    ///  
    /// let q: Q = Q::from_str("-10/3").unwrap();
    /// let b: Q = Q::from_str("10/-3").unwrap();
    /// ```
    ///
    /// ```
    /// use std::str::FromStr;
    /// use qfall_math::rational::Q;
    ///  
    /// let q: Q = Q::from_str("-10").unwrap();
    /// let b: Q = Q::from_str("10").unwrap();
    /// ```
    ///
    /// # Errors and Failures
    /// - Returns a [`MathError`] of type
    ///   [`StringConversionError`](MathError::StringConversionError)
    ///     - if the provided string contains a `Null` byte, or
    ///     - if the provided string was not formatted correctly.
    /// - Returns a [`MathError`] of type
    ///   [`DivisionByZeroError`](MathError::DivisionByZeroError)
    ///   if the provided string has `0` as the denominator.
    fn from_str(s: &str) -> Result<Self, MathError> {
        if s.contains(char::is_whitespace) {
            return Err(StringConversionError::InvalidStringToQInput(s.to_owned()))?;
        }

        // `fmpq::default()` returns the value `0/0`
        let mut value = fmpq::default();

        let c_string = CString::new(s)?;

        // -1 is returned if the string is an invalid input.
        //
        // Given the documentation `c_string.as_ptr()` is freed once c_string is deallocated
        // 'The pointer will be valid for as long as `self` is'
        // For reading more look at the documentation of `.as_ptr()`.
        //
        // since value is set to `0`, if an error occurs, we do not need to free
        // the allocated space manually
        if -1 == unsafe { fmpq_set_str(&mut value, c_string.as_ptr(), 10) } {
            return Err(StringConversionError::InvalidStringToQInput(s.to_owned()))?;
        };

        // canonical form is expected by other functions
        unsafe { fmpq_canonicalise(&mut value) };

        // if `value.den` is set to `0`, `value.num` is not necessarily `0` as well.
        // hence we do need to free the allocated space of the numerator
        // manually by using `fmpq_clear`
        match unsafe { fmpz_is_zero(&value.den) } {
            0 => Ok(Q { value }),
            _ => {
                unsafe {
                    fmpq_clear(&mut value);
                }
                Err(MathError::DivisionByZeroError(s.to_owned()))
            }
        }
    }
}

#[cfg(test)]
mod tests_from_str {
    use crate::rational::Q;
    use std::str::FromStr;

    /// Ensure that initialization with large numerators and denominators works.
    #[test]
    fn max_int_positive() {
        let mut s_1 = (i64::MAX).to_string();
        s_1.push('/');
        s_1.push_str(&(i64::MAX).to_string());

        let mut s_2 = ("1/").to_string();
        s_2.push_str(&(i64::MAX).to_string());

        assert!(Q::from_str(&(i64::MAX).to_string()).is_ok());
        assert!(Q::from_str(&s_1).is_ok());
        assert!(Q::from_str(&s_2).is_ok());
    }

    /// Ensure that initialization with large numerators and denominators
    /// (larger than i64) works.
    #[test]
    fn large_positive() {
        let mut s_1 = "1".repeat(65);
        s_1.push('/');
        s_1.push_str(&"1".repeat(65));

        let mut s_2 = ("1/").to_string();
        s_2.push_str(&"1".repeat(65));

        assert!(Q::from_str(&"1".repeat(65)).is_ok());
        assert!(Q::from_str(&s_1).is_ok());
        assert!(Q::from_str(&s_2).is_ok());
    }

    /// Ensure that initialization with large negative numerators and
    /// denominators works.
    #[test]
    fn max_int_negative() {
        let mut s_1 = (i64::MIN).to_string();
        s_1.push('/');
        s_1.push_str(&(i64::MIN).to_string());

        let mut s_2 = ("1/").to_string();
        s_2.push_str(&(i64::MIN).to_string());

        assert!(Q::from_str(&(i64::MIN).to_string()).is_ok());
        assert!(Q::from_str(&s_1).is_ok());
        assert!(Q::from_str(&s_2).is_ok());
    }

    /// Ensure that initialization with large negative numerators and
    /// denominators (larger than [`i64`]) works.
    #[test]
    fn large_negative() {
        let mut s_1 = "-".to_string();
        s_1.push_str(&"1".repeat(65));
        s_1.push('/');
        s_1.push_str(&"1".repeat(65));

        let mut s_2 = ("-1/").to_string();
        s_2.push_str(&"1".repeat(65));

        assert!(Q::from_str(&"1".repeat(65)).is_ok());
        assert!(Q::from_str(&s_1).is_ok());
        assert!(Q::from_str(&s_2).is_ok());
    }

    /// Ensure that an initialization with two minus works.
    #[test]
    fn no_error_both_minus() {
        assert!(Q::from_str("-3/-2").is_ok());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_wrong_letters() {
        assert!(Q::from_str("hbrkt35itu3gg").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_wrong_order() {
        assert!(Q::from_str("3/2-").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_two_divisions() {
        assert!(Q::from_str("3/2/4").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_wrong_minus() {
        assert!(Q::from_str("-3-4/2").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_whitespace_mid() {
        assert!(Q::from_str("876/ 543").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_whitespace_start() {
        assert!(Q::from_str(" 876543").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_whitespace_end() {
        assert!(Q::from_str("876543 ").is_err());
    }

    /// Ensure that wrong initialization yields an Error.
    #[test]
    fn error_whitespace_minus() {
        assert!(Q::from_str("- 876543").is_err());
    }

    /// Ensure that values returned by [`Q::from_str()`] are canonical.
    #[test]
    fn canonical_result() {
        let one_1 = Q::from_str("1/1").unwrap();
        let one_2 = Q::from_str("2/2").unwrap();
        let one_3 = Q::from_str("-42/-42").unwrap();

        let zero_1 = Q::from_str("0/1").unwrap();
        let zero_2 = Q::from_str("0/42").unwrap();

        assert_eq!(one_1, one_2);
        assert_eq!(one_1, one_3);
        assert_eq!(zero_1, zero_2);
    }
}

#[cfg(test)]
mod test_from_int_int {
    use crate::integer::Z;
    use crate::integer_mod_q::Zq;
    use crate::rational::Q;

    /// Test that the different combinations of rust integers, [`Z`], and [`Zq`]
    /// in their owned and borrowed form can be used to create a [`Q`].
    #[test]
    fn different_types() {
        let int_8: i8 = 10;
        let int_16: i16 = 10;
        let int_32: i32 = 10;
        let int_64: i64 = 10;
        let uint_8: u8 = 10;
        let uint_16: u16 = 10;
        let uint_32: u32 = 10;
        let uint_64: u64 = 10;
        let z = Z::from(10);
        let zq = Zq::from((10, 20));

        // owned, owned the same type in numerator and denominator
        let _ = Q::from((int_8, int_8));
        let _ = Q::from((int_16, int_16));
        let _ = Q::from((int_32, int_32));
        let _ = Q::from((int_64, int_64));
        let _ = Q::from((uint_8, uint_8));
        let _ = Q::from((uint_16, uint_16));
        let _ = Q::from((uint_32, uint_32));
        let _ = Q::from((uint_64, uint_64));
        let _ = Q::from((z.clone(), z.clone()));
        let _ = Q::from((zq.clone(), zq.clone()));

        // borrowed, borrowed the same type in numerator and denominator
        let _ = Q::from((&int_8, &int_8));
        let _ = Q::from((&int_16, &int_16));
        let _ = Q::from((&int_32, &int_32));
        let _ = Q::from((&int_64, &int_64));
        let _ = Q::from((&uint_8, &uint_8));
        let _ = Q::from((&uint_16, &uint_16));
        let _ = Q::from((&uint_32, &uint_32));
        let _ = Q::from((&uint_64, &uint_64));
        let _ = Q::from((&z, &z));
        let _ = Q::from((&zq, &zq));

        // From now on assume that i/u8, i/u16, i/u32 and i/u64 behave the same.
        // This assumption is reasonable, since their implementation is the same.

        // owned, owned mixed types
        let _ = Q::from((int_8, z.clone()));
        let _ = Q::from((zq.clone(), z.clone()));
        let _ = Q::from((z.clone(), int_8));
        let _ = Q::from((z.clone(), zq.clone()));
        let _ = Q::from((int_8, zq.clone()));
        let _ = Q::from((zq.clone(), int_8));

        // owned, borrowed mixed types
        let _ = Q::from((int_8, &z));
        let _ = Q::from((zq.clone(), &z));
        let _ = Q::from((z.clone(), &int_8));
        let _ = Q::from((z.clone(), &zq));
        let _ = Q::from((int_8, &zq));
        let _ = Q::from((zq.clone(), &int_8));

        // borrowed, owned mixed types
        let _ = Q::from((&int_8, z.clone()));
        let _ = Q::from((&zq, z.clone()));
        let _ = Q::from((&z, int_8));
        let _ = Q::from((&z, zq.clone()));
        let _ = Q::from((&int_8, zq.clone()));
        let _ = Q::from((&zq, int_8));

        // borrowed, borrowed mixed types
        let _ = Q::from((&int_8, &z));
        let _ = Q::from((&zq, &z));
        let _ = Q::from((&z, &int_8));
        let _ = Q::from((&z, &zq));
        let _ = Q::from((&int_8, &zq));
        let _ = Q::from((&zq, &int_8));
    }

    /// Ensure that large parameters work (FLINT uses pointer representation).
    #[test]
    fn working_large() {
        let numerator = u64::MAX;
        let denominator = u64::MAX - 1;
        let numerator_z = Z::from(numerator);
        let denominator_z = Z::from(denominator);

        let q_1 = Q::from((numerator, denominator));
        let q_2 = Q::from((numerator_z, denominator_z));

        assert_eq!(q_1, q_2);
    }

    /// Test with `0` denominator (not valid -> should lead to an error)
    #[test]
    #[should_panic]
    fn divide_by_zero() {
        let _ = Q::from((10, 0));
    }

    /// Test with either negative denominator or numerator
    #[test]
    fn negative_small() {
        let numerator = 10;
        let denominator = -1;

        let q_1 = Q::from((numerator, denominator));
        let q_2 = Q::from((-numerator, -denominator));

        assert_eq!(q_1, q_2);
    }

    /// Ensure that the result is canonical for small parameters.
    #[test]
    fn canonical_small() {
        let numerator = 10;
        let denominator = 1;

        let q_1 = Q::from((numerator, denominator));
        let q_2 = Q::from((-numerator, -denominator));
        let q_3 = Q::from((numerator * 2, denominator * 2));

        let q_4_negative = Q::from((-numerator, denominator));
        let q_5_negative = Q::from((numerator, -denominator));

        assert_eq!(q_1, q_2);
        assert_eq!(q_1, q_3);

        assert_eq!(q_4_negative, q_5_negative);
    }

    /// Ensure that the result is canonical for large parameters.
    #[test]
    fn canonical_large() {
        let numerator = i64::MAX;
        let denominator = i64::MAX - 1;

        let numerator_z = Z::from(numerator);
        let denominator_z = Z::from(denominator);

        let q_1 = Q::from((numerator, denominator));
        let q_2 = Q::from((-numerator, -denominator));
        let q_3 = Q::from((&numerator_z, &denominator_z));
        let q_4 = Q::from((&numerator_z * 2, &denominator_z * 2));
        let q_5_negative = Q::from((-1 * &numerator_z, &denominator_z));
        let q_6_negative = Q::from((&numerator_z, -1 * &denominator_z));

        assert_eq!(q_1, q_2);
        assert_eq!(q_1, q_3);
        assert_eq!(q_1, q_4);
        assert_eq!(q_5_negative, q_6_negative);
    }
}

#[cfg(test)]
mod test_try_from_int_int {
    use crate::integer::Z;
    use crate::rational::Q;

    /// Test the different borrowed parameter types with small numerator and denominator.
    #[test]
    fn test_types_borrowed_small() {
        let numerator = 10;
        let denominator = 15;

        let q_1 = Q::from((&(numerator as u8), &(denominator as i8)));
        let q_2 = Q::from((&(numerator as u16), &(denominator as i16)));
        let q_3 = Q::from((&(numerator as u32), &(denominator)));
        let q_4 = Q::from((&(numerator as u64), &(denominator as i64)));
        let q_5 = Q::from((&Z::from(numerator), &Z::from(denominator)));

        let q_6 = Q::from((&(numerator as i16), &(denominator as u16)));
        let q_7 = Q::from((&(numerator as i16), &(denominator as i16)));

        assert_eq!(q_1, q_2);
        assert_eq!(q_1, q_3);
        assert_eq!(q_1, q_4);
        assert_eq!(q_1, q_5);
        assert_eq!(q_1, q_6);
        assert_eq!(q_1, q_7);
    }

    /// Ensure that large parameters work (FLINT uses pointer representation).
    #[test]
    fn working_large() {
        let numerator = u64::MAX;
        let denominator = u64::MAX - 1;
        let numerator_z = Z::from(numerator);
        let denominator_z = Z::from(denominator);

        let q_1 = Q::from((&numerator, &denominator));
        let q_2 = Q::from((&numerator_z, &denominator_z));

        assert_eq!(q_1, q_2);
    }

    /// Test with `0` denominator (not valid -> should lead to an error)
    #[test]
    #[should_panic]
    fn divide_by_zero() {
        let numerator = 10;
        let denominator = 0;

        let _ = Q::from((&numerator, &denominator));
    }

    /// Test with either negative denominator or numerator
    #[test]
    fn negative_small() {
        let numerator = 10;
        let denominator = -1;

        let q_1 = Q::from((&numerator, &denominator));
        let q_2 = Q::from((&-numerator, &-denominator));

        assert_eq!(q_1, q_2);
    }

    /// Ensure that the result is canonical for small parameters.
    #[test]
    fn canonical_small() {
        let numerator = 10;
        let denominator = 1;

        let q_1 = Q::from((&numerator, &denominator));
        let q_2 = Q::from((&-numerator, &-denominator));
        let q_3 = Q::from((&(numerator * 2), &(denominator * 2)));

        let q_4_negative = Q::from((&-numerator, &denominator));
        let q_5_negative = Q::from((&numerator, &-denominator));

        assert_eq!(q_1, q_2);
        assert_eq!(q_1, q_3);

        assert_eq!(q_4_negative, q_5_negative);
    }

    /// Ensure that the result is canonical for large parameters.
    #[test]
    fn canonical_large() {
        let numerator = i64::MAX;
        let denominator = i64::MAX - 1;

        let numerator_z = Z::from(numerator);
        let denominator_z = Z::from(denominator);

        let q_1 = Q::from((&numerator, &denominator));
        let q_2 = Q::from((&-numerator, &-denominator));
        let q_3 = Q::from((&numerator_z, &denominator_z));
        let q_4 = Q::from((&(&numerator_z * Z::from(2)), &(&denominator_z * Z::from(2))));
        let q_5_negative = Q::from((&(&numerator_z * Z::from(-1)), &denominator_z));
        let q_6_negative = Q::from((&numerator_z, &(&denominator_z * Z::from(-1))));

        assert_eq!(q_1, q_2);
        assert_eq!(q_1, q_3);
        assert_eq!(q_1, q_4);
        assert_eq!(q_5_negative, q_6_negative);
    }
}

#[cfg(test)]
mod test_from_z {
    use super::Q;
    use crate::integer::Z;

    /// Ensure that the [`From`] trait is available and works correctly for
    /// small and large instances of [`Z`].
    #[test]
    fn from_trait() {
        let z_1 = Z::from(u64::MAX);
        let z_2 = Z::from(17);

        assert_eq!(Q::from(u64::MAX), Q::from(z_1));
        assert_eq!(Q::from(17), Q::from(z_2));
    }

    /// Ensure that all types that can be turned into an [`Z`]
    /// can be used to instantiate a [`Q`]
    #[test]
    fn from_into_z() {
        let _ = Q::from(u8::MAX);
        let _ = Q::from(u16::MAX);
        let _ = Q::from(u32::MAX);
        let _ = Q::from(u64::MAX);

        let _ = Q::from(i8::MIN);
        let _ = Q::from(i16::MIN);
        let _ = Q::from(i32::MIN);
        let _ = Q::from(i64::MIN);
    }
}

#[cfg(test)]
mod test_from_float {
    use super::Q;
    use std::{
        f64::consts::{E, LN_2, LN_10},
        str::FromStr,
    };

    /// Test that a large number is correctly converted from float.
    #[test]
    fn large_value() {
        // This is the exact value stored when creating a float with the value 1e+100
        let a: f64 = 10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104.0;

        let q = Q::from(a);

        let cmp = Q::from_str("10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104")
                    .unwrap();
        assert_eq!(q, cmp);
    }

    // Test that a small number is correctly converted from float.
    #[test]
    #[allow(clippy::excessive_precision)]
    fn small_value() {
        // This is the exact value stored when creating a float with the value 0.1
        let a: f64 = 0.1000000000000000055511151231257827021181583404541015625;

        let q = Q::from(a);

        let cmp = Q::from_str("1000000000000000055511151231257827021181583404541015625/10000000000000000000000000000000000000000000000000000000")
            .unwrap();
        assert_eq!(q, cmp);
    }

    /// Enure that the from works correctly for positive values
    #[test]
    fn positive() {
        let numerator = 150001;
        let denominator = 16;

        let value = Q::from(numerator as f64 / denominator as f64);

        let cmp = Q::from((numerator, denominator));
        assert_eq!(cmp, value);
    }

    /// Enure that the from works correctly for positive values
    #[test]
    fn negative() {
        let numerator = 150001;
        let denominator = -8;

        let value = Q::from(numerator as f64 / denominator as f64);

        let cmp = Q::from((numerator, denominator));
        assert_eq!(cmp, value);
    }

    /// Ensure that the [`From`] trait is available for [`f64`] constants
    #[test]
    fn from_trait() {
        let _ = Q::from(E);
        let _ = Q::from(LN_10);
        let _ = Q::from(LN_2);
    }

    /// test availability for [`f32`]
    #[test]
    fn from_f32_available() {
        let f: f32 = 42.17;

        let _ = Q::from(f);
        let _ = Q::from_f32(f);
    }
}

#[cfg(test)]
mod test_into_float {
    use super::*;

    /// Ensure that `1/2` is correctly converted to `0.5`.
    /// Special about `0.5` is that it can be exactly represented as a float.
    #[test]
    fn one_half() {
        let one_half = Q::from((1, 2));
        let float = f64::from(&one_half);

        assert_eq!(0.5, float);
    }

    /// Ensure that a roundtrip [`f64`] -> [`Q`] -> [`f64`]
    /// does not change the value.
    #[test]
    fn round_trip() {
        let start_values = vec![
            0.0,
            0.1,
            f64::MAX,
            f64::MIN,
            f64::MIN_POSITIVE,
            f64::EPSILON,
        ];

        for start in start_values {
            let end = f64::from(&Q::from(start));

            assert_eq!(start, end);
        }
    }
}

#[cfg(test)]
mod test_from_q_ref {
    use crate::rational::Q;

    /// Ensure that [`Q`] can be created from [`&Q`].
    #[test]
    fn availability() {
        let q = Q::from(u64::MAX);

        let _ = Q::from(&q);
    }
}