qfall_math/rational/q/get.rs
1// Copyright © 2023 Sven Moog
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! Get elements of [`Q`] like the numerator and denominator.
10
11use super::Q;
12use crate::integer::Z;
13use flint_sys::fmpz::fmpz_init_set;
14
15impl Q {
16 /// Returns the denominator
17 ///
18 /// # Examples
19 /// ```
20 /// use qfall_math::rational::Q;
21 /// use qfall_math::integer::Z;
22 ///
23 /// let value = Q::from((2, 20));
24 ///
25 /// let den = value.get_denominator();
26 ///
27 /// assert_eq!(den, Z::from(10));
28 /// ```
29 pub fn get_denominator(&self) -> Z {
30 let mut result = Z::default();
31 unsafe { fmpz_init_set(&mut result.value, &self.value.den) };
32 result
33 }
34
35 /// Returns the numerator
36 ///
37 /// # Examples
38 /// ```
39 /// use qfall_math::rational::Q;
40 /// use qfall_math::integer::Z;
41 ///
42 /// let value = Q::from((2, 20));
43 ///
44 /// let num = value.get_numerator();
45 ///
46 /// assert_eq!(num, Z::from(1));
47 /// ```
48 pub fn get_numerator(&self) -> Z {
49 let mut result = Z::default();
50 unsafe { fmpz_init_set(&mut result.value, &self.value.num) };
51 result
52 }
53}
54
55#[cfg(test)]
56mod test_get_denominator {
57 use crate::{integer::Z, rational::Q};
58
59 /// get a small denominator
60 #[test]
61 fn get_small() {
62 let value = Q::from((2, 20));
63 let den = value.get_denominator();
64 assert_eq!(den, Z::from(10));
65 }
66
67 /// get a large denominator (uses FLINT's pointer representation)
68 #[test]
69 fn get_large() {
70 let value = Q::from((1, i64::MAX));
71 let den = value.get_denominator();
72 assert_eq!(den, Z::from(i64::MAX));
73 }
74}
75
76#[cfg(test)]
77mod test_get_numerator {
78 use crate::{integer::Z, rational::Q};
79
80 /// get a small numerator
81 #[test]
82 fn get_small() {
83 let value = Q::from((2, 20));
84 let num = value.get_numerator();
85 assert_eq!(num, Z::from(1));
86 }
87
88 /// get a large numerator (uses FLINT's pointer representation)
89 #[test]
90 fn get_large() {
91 let value = Q::from(i64::MAX);
92 let num = value.get_numerator();
93 assert_eq!(num, Z::from(i64::MAX));
94 }
95}