Skip to main content

malachite_base/num/basic/
traits.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// Implementations of traits for NonZero* types by b4D8.
4//
5// This file is part of Malachite.
6//
7// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
8// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
9// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
10
11use core::num::*;
12
13/// Provides the constant 0.
14#[allow(clippy::declare_interior_mutable_const)]
15pub trait Zero {
16    const ZERO: Self;
17}
18
19/// Provides the constant 1.
20#[allow(clippy::declare_interior_mutable_const)]
21pub trait One {
22    const ONE: Self;
23}
24
25/// Provides the constant 2.
26#[allow(clippy::declare_interior_mutable_const)]
27pub trait Two {
28    const TWO: Self;
29}
30
31/// Provides the constant -1.
32#[allow(clippy::declare_interior_mutable_const)]
33pub trait NegativeOne {
34    const NEGATIVE_ONE: Self;
35}
36
37/// Provides the constant 1/2.
38#[allow(clippy::declare_interior_mutable_const)]
39pub trait OneHalf {
40    const ONE_HALF: Self;
41}
42
43/// Provides the constant -0.
44#[allow(clippy::declare_interior_mutable_const)]
45pub trait NegativeZero {
46    const NEGATIVE_ZERO: Self;
47}
48
49/// Provides the constant (positive) Infinity.
50#[allow(clippy::declare_interior_mutable_const)]
51pub trait Infinity {
52    const INFINITY: Self;
53}
54
55/// Provides the constant -Infinity.
56#[allow(clippy::declare_interior_mutable_const)]
57pub trait NegativeInfinity {
58    const NEGATIVE_INFINITY: Self;
59}
60
61/// Provides the constant NaN.
62#[allow(clippy::declare_interior_mutable_const)]
63pub trait NaN {
64    const NAN: Self;
65}
66
67/// Provides the Prouhet-Thue-Morse constant, whose bits are the Thue-Morse sequence.
68pub trait ProuhetThueMorseConstant {
69    const PROUHET_THUE_MORSE_CONSTANT: Self;
70}
71
72/// Provides the prime constant, whose $n$th bit (starting from $n=1$) is true if and only if $n$ is
73/// prime.
74pub trait PrimeConstant {
75    const PRIME_CONSTANT: Self;
76}
77
78/// Provides $\ln 2$.
79pub trait Ln2 {
80    const LN_2: Self;
81}
82
83/// Provides $\ln 10$.
84pub trait Ln10 {
85    const LN_10: Self;
86}
87
88/// Provides $\log_2 e$.
89pub trait Log2E {
90    const LOG_2_E: Self;
91}
92
93/// Provides $\log_{10} e$.
94pub trait Log10E {
95    const LOG_10_E: Self;
96}
97
98/// Provides $\log_2 10$.
99pub trait Log210 {
100    const LOG_2_10: Self;
101}
102
103/// Provides $\log_{10} 2$.
104pub trait Log102 {
105    const LOG_10_2: Self;
106}
107
108/// Provides $\sqrt{2}$.
109pub trait Sqrt2 {
110    const SQRT_2: Self;
111}
112
113/// Provides $\sqrt{3}$.
114pub trait Sqrt3 {
115    const SQRT_3: Self;
116}
117
118/// Provides $\sqrt{5}$.
119pub trait Sqrt5 {
120    const SQRT_5: Self;
121}
122
123/// Provides $\sqrt{2}/2=\sqrt{1/2}=1/\sqrt{2}$.
124pub trait Sqrt2Over2 {
125    const SQRT_2_OVER_2: Self;
126}
127
128/// Provides $\sqrt{3}/3=\sqrt{1/3}=1/\sqrt{3}$.
129pub trait Sqrt3Over3 {
130    const SQRT_3_OVER_3: Self;
131}
132
133/// Provides $\sqrt{5}/5=\sqrt{1/5}=1/\sqrt{5}$.
134pub trait Sqrt5Over5 {
135    const SQRT_5_OVER_5: Self;
136}
137
138/// Provides $\varphi$, the golden ratio.
139pub trait Phi {
140    const PHI: Self;
141}
142
143/// Provides $\pi$.
144pub trait Pi {
145    const PI: Self;
146}
147
148/// Provides $\tau=2\pi$.
149pub trait Tau {
150    const TAU: Self;
151}
152
153/// Provides $\pi/2$.
154pub trait PiOver2 {
155    const PI_OVER_2: Self;
156}
157
158/// Provides $\pi/3$.
159pub trait PiOver3 {
160    const PI_OVER_3: Self;
161}
162
163/// Provides $\pi/4$.
164pub trait PiOver4 {
165    const PI_OVER_4: Self;
166}
167
168/// Provides $\pi/6$.
169pub trait PiOver6 {
170    const PI_OVER_6: Self;
171}
172
173/// Provides $\pi/8$.
174pub trait PiOver8 {
175    const PI_OVER_8: Self;
176}
177
178/// Provides $1/\pi$.
179pub trait OneOverPi {
180    const ONE_OVER_PI: Self;
181}
182
183/// Provides $\sqrt{\pi}$.
184pub trait SqrtPi {
185    const SQRT_PI: Self;
186}
187
188/// Provides $1/\sqrt{\pi}$.
189pub trait OneOverSqrtPi {
190    const ONE_OVER_SQRT_PI: Self;
191}
192
193/// Provides $1/\sqrt{\tau}=1/\sqrt{2\pi}$.
194pub trait OneOverSqrtTau {
195    const ONE_OVER_SQRT_TAU: Self;
196}
197
198/// Provides $2/\pi$.
199pub trait TwoOverPi {
200    const TWO_OVER_PI: Self;
201}
202
203/// Provides $2/\sqrt{\pi}$.
204pub trait TwoOverSqrtPi {
205    const TWO_OVER_SQRT_PI: Self;
206}
207
208/// Provides Gauss's constant, $G=1/\mathrm{AGM}(1,\sqrt{2})$.
209///
210/// Having three consecutive esses in an identifier is awkward, so let's pretend that we sometimes
211/// use AP Style and write "Gauss' constant".
212pub trait GaussConstant {
213    const GAUSS_CONSTANT: Self;
214}
215
216/// Provides the lemniscate constant $\varpi=\pi G$, where $G$ is Gauss's constant.
217pub trait LemniscateConstant {
218    const LEMNISCATE_CONSTANT: Self;
219}
220
221// Implementation for `NonZero*` types:
222// - `One` and `Two` for both signed and unsigned variants
223// - `NegativeOne` for the signed variant
224macro_rules! impl_non_zero {
225    ($($t:ident),+) => {
226        $(
227            impl One for $t {
228                const ONE: Self = match Self::new(1) {
229                    Some(v) => v,
230                    None => unreachable!() // 1 is a valid nonzero value
231                };
232            }
233
234            impl Two for $t {
235                const TWO: Self = match Self::new(2) {
236                    Some(v) => v,
237                    None => unreachable!() // 2 is a valid nonzero value
238                };
239            }
240        )+
241    };
242    ($($u:ident && $i:ident),+) => {
243        $(
244            impl_non_zero!($u, $i);
245
246            impl NegativeOne for $i {
247                const NEGATIVE_ONE: Self = match Self::new(-1) {
248                    Some(v) => v,
249                    None => unreachable!() // -1 is a valid non zero value
250                };
251            }
252        )+
253    }
254}
255
256impl_non_zero!(
257    NonZeroUsize && NonZeroIsize,
258    NonZeroU128 && NonZeroI128,
259    NonZeroU64 && NonZeroI64,
260    NonZeroU32 && NonZeroI32,
261    NonZeroU16 && NonZeroI16,
262    NonZeroU8 && NonZeroI8
263);