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