qfall_math/rational/q/default.rs
1// Copyright 2023 Marcel Luca Schmidt, Jan Niklas Siemer
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//! Define default values for [`Q`].
10
11use super::Q;
12use flint3_sys::fmpq;
13
14impl Default for Q {
15 /// Returns an instantiation of [`Q`] with value `0/1`.
16 ///
17 /// # Examples
18 /// ```
19 /// use std::default::Default;
20 /// use qfall_math::rational::Q;
21 ///
22 /// let a: Q = Q::default();
23 /// ```
24 fn default() -> Self {
25 // needs to stay a manual instantiation as try_from uses default inside
26 Q {
27 value: fmpq { num: 0, den: 1 },
28 }
29 }
30}
31
32impl Q {
33 /// Returns an instantiation of [`Q`] with value `1`.
34 ///
35 /// # Examples
36 /// ```
37 /// use qfall_math::rational::Q;
38 ///
39 /// let a: Q = Q::ONE;
40 /// ```
41 pub const ONE: Q = Q {
42 value: fmpq { num: 1, den: 1 },
43 };
44
45 /// Returns an instantiation of [`Q`] with value `0`.
46 ///
47 /// # Examples
48 /// ```
49 /// use qfall_math::rational::Q;
50 ///
51 /// let a: Q = Q::ZERO;
52 /// ```
53 pub const ZERO: Q = Q {
54 value: fmpq { num: 0, den: 1 },
55 };
56
57 /// Returns an instantiation of [`Q`] with value `-1`.
58 ///
59 /// # Examples
60 /// ```
61 /// use qfall_math::rational::Q;
62 ///
63 /// let a: Q = Q::MINUS_ONE;
64 /// ```
65 pub const MINUS_ONE: Q = Q {
66 value: fmpq { num: -1, den: 1 },
67 };
68
69 /// Returns an instantiation of [`Q`] with value `e ≈ 2.718281...`
70 /// with a precision of ~ 10^-36.
71 ///
72 /// # Examples
73 /// ```
74 /// use qfall_math::rational::Q;
75 ///
76 /// let a: Q = Q::E;
77 /// ```
78 pub const E: Q = Q {
79 // generated with continued fraction (40 factors)
80 value: fmpq {
81 num: 2922842896378005707,
82 den: 1075253811351460636,
83 },
84 };
85
86 /// Returns an instantiation of [`Q`] with value `pi ≈ 3.141592...`
87 /// with a precision of ~ 10^-37.
88 ///
89 /// # Examples
90 /// ```
91 /// use qfall_math::rational::Q;
92 ///
93 /// let a: Q = Q::PI;
94 /// ```
95 pub const PI: Q = Q {
96 // generated with continued fraction (33 factors)
97 value: fmpq {
98 num: 2646693125139304345,
99 den: 842468587426513207,
100 },
101 };
102
103 /// Returns an instantiation of [`Q`] with value `2^62 - 1`.
104 ///
105 /// # Examples
106 /// ```
107 /// use qfall_math::rational::Q;
108 ///
109 /// let a: Q = Q::MAX62;
110 /// ```
111 pub const MAX62: Q = Q {
112 value: fmpq {
113 num: i64::pow(2, 62) - 1,
114 den: 1,
115 },
116 };
117
118 /// Returns an instantiation of [`Q`] with value `1 / (2^62 - 1)`.
119 ///
120 /// # Examples
121 /// ```
122 /// use qfall_math::rational::Q;
123 ///
124 /// let a: Q = Q::INV_MAX62;
125 /// ```
126 pub const INV_MAX62: Q = Q {
127 value: fmpq {
128 num: 1,
129 den: i64::pow(2, 62) - 1,
130 },
131 };
132
133 /// Returns an instantiation of [`Q`] with value `2^32 - 1`.
134 ///
135 /// # Examples
136 /// ```
137 /// use qfall_math::rational::Q;
138 ///
139 /// let a: Q = Q::MAX32;
140 /// ```
141 pub const MAX32: Q = Q {
142 value: fmpq {
143 num: i64::pow(2, 32) - 1,
144 den: 1,
145 },
146 };
147
148 /// Returns an instantiation of [`Q`] with value `1 / (2^32 - 1)`.
149 ///
150 /// # Examples
151 /// ```
152 /// use qfall_math::rational::Q;
153 ///
154 /// let a: Q = Q::INV_MAX32;
155 /// ```
156 pub const INV_MAX32: Q = Q {
157 value: fmpq {
158 num: 1,
159 den: i64::pow(2, 32) - 1,
160 },
161 };
162
163 /// Returns an instantiation of [`Q`] with value `2^16 - 1`.
164 ///
165 /// # Examples
166 /// ```
167 /// use qfall_math::rational::Q;
168 ///
169 /// let a: Q = Q::MAX16;
170 /// ```
171 pub const MAX16: Q = Q {
172 value: fmpq {
173 num: i64::pow(2, 16) - 1,
174 den: 1,
175 },
176 };
177
178 /// Returns an instantiation of [`Q`] with value `1 / (2^16 - 1)`.
179 ///
180 /// # Examples
181 /// ```
182 /// use qfall_math::rational::Q;
183 ///
184 /// let a: Q = Q::INV_MAX16;
185 /// ```
186 pub const INV_MAX16: Q = Q {
187 value: fmpq {
188 num: 1,
189 den: i64::pow(2, 16) - 1,
190 },
191 };
192
193 /// Returns an instantiation of [`Q`] with value `2^8 - 1`.
194 ///
195 /// # Examples
196 /// ```
197 /// use qfall_math::rational::Q;
198 ///
199 /// let a: Q = Q::MAX8;
200 /// ```
201 pub const MAX8: Q = Q {
202 value: fmpq {
203 num: i64::pow(2, 8) - 1,
204 den: 1,
205 },
206 };
207
208 /// Returns an instantiation of [`Q`] with value `1 / (2^8 - 1)`.
209 ///
210 /// # Examples
211 /// ```
212 /// use qfall_math::rational::Q;
213 ///
214 /// let a: Q = Q::INV_MAX8;
215 /// ```
216 pub const INV_MAX8: Q = Q {
217 value: fmpq {
218 num: 1,
219 den: i64::pow(2, 8) - 1,
220 },
221 };
222}
223
224#[cfg(test)]
225mod tests_init {
226 use super::Q;
227
228 /// Ensure that [`Default`] initializes [`Q`] with `0`.
229 #[test]
230 fn init_default() {
231 assert_eq!(Q::ZERO, Q::default());
232 }
233
234 /// Ensure that `ZERO` initializes [`Q`] with `0`.
235 #[test]
236 fn init_zero() {
237 assert_eq!(Q::from(0), Q::ZERO);
238 }
239
240 /// Ensure that `ONE` initializes [`Q`] with `1`.
241 #[test]
242 fn init_one() {
243 assert_eq!(Q::from(1), Q::ONE);
244 }
245
246 /// Ensure that `MINUS_ONE` initializes [`Q`] with `-1`.
247 #[test]
248 fn init_minus_one() {
249 assert_eq!(Q::from(-1), Q::MINUS_ONE);
250 }
251
252 /// Ensure that `MAX62` initializes [`Q`] with `2^62 - 1`.
253 #[test]
254 fn init_max62() {
255 assert_eq!(Q::from(i64::pow(2, 62) - 1), Q::MAX62);
256 }
257
258 /// Ensure that `INV_MAX62` initializes [`Q`] with `1 / (2^62 - 1)`.
259 #[test]
260 fn init_inv_max62() {
261 assert_eq!(Q::from((1, i64::pow(2, 62) - 1)), Q::INV_MAX62);
262 }
263
264 /// Ensure that `MAX32` initializes [`Q`] with `2^32 - 1`.
265 #[test]
266 fn init_max32() {
267 assert_eq!(Q::from(i64::pow(2, 32) - 1), Q::MAX32);
268 }
269
270 /// Ensure that `INV_MAX32` initializes [`Q`] with `1 / (2^32 - 1)`.
271 #[test]
272 fn init_inv_max32() {
273 assert_eq!(Q::from((1, i64::pow(2, 32) - 1)), Q::INV_MAX32);
274 }
275
276 /// Ensure that `MAX16` initializes [`Q`] with `2^16 - 1`.
277 #[test]
278 fn init_max16() {
279 assert_eq!(Q::from(i64::pow(2, 16) - 1), Q::MAX16);
280 }
281
282 /// Ensure that `INV_MAX16` initializes [`Q`] with `1 / (2^16 - 1)`.
283 #[test]
284 fn init_inv_max16() {
285 assert_eq!(Q::from((1, i64::pow(2, 16) - 1)), Q::INV_MAX16);
286 }
287
288 /// Ensure that `MAX8` initializes [`Q`] with `2^8 - 1`.
289 #[test]
290 fn init_max8() {
291 assert_eq!(Q::from(i64::pow(2, 8) - 1), Q::MAX8);
292 }
293
294 /// Ensure that `INV_MAX8` initializes [`Q`] with `1 / (2^8 - 1)`.
295 #[test]
296 fn init_inv_max8() {
297 assert_eq!(Q::from((1, i64::pow(2, 8) - 1)), Q::INV_MAX8);
298 }
299}