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
use i256::I256;
use num_traits::{AsPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingSub};
use crate::{
core::integer::FullInt, Circle, CircleConstants, ExponentConstants, FractionConstants, Integer,
Scalar, ScalarConstants,
};
use core::cmp::Ordering;
use core::ops::*;
#[allow(private_bounds)]
impl<
F: Integer
+ FractionConstants
+ FullInt
+ Shl<isize, Output = F>
+ Shr<isize, Output = F>
+ Shl<F, Output = F>
+ Shr<F, Output = F>
+ Shl<E, Output = F>
+ Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingSub
+ WrappingMul,
E: Integer
+ ExponentConstants
+ FullInt
+ Shl<isize, Output = E>
+ Shr<isize, Output = E>
+ Shl<E, Output = E>
+ Shr<E, Output = E>
+ Shl<F, Output = E>
+ Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingSub
+ WrappingMul,
> Circle<F, E>
where
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
I256: From<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<E>,
{
/// Compares two Circle values for exact equality.
///
/// # Description
///
/// This method determines if two Circle values represent exactly the same complex number.
/// Unlike Scalar comparison which provides ordering, Circle comparison only tests for equality since complex numbers do not have a natural total ordering relationship.
///
/// For Circle values to be considered equal, they must have identical representations in both their real and imaginary components, as well as matching exponents when normal.
///
/// # Equality Conditions
///
/// Two Circle values are equal if and only if:
/// - Both are normal `[#]` with identical exponents, real fractions, and imaginary fractions
/// - Both are Zero `[0]` (regardless of the specific Zero representation)
///
/// # Non-Equal Cases
///
/// Returns `false` for:
/// - Different normal values (different exponents or fraction components)
/// - Any escaped states `[↑]`, `[↓]`, `[∞]`, `[℘?]` compared with anything except identical Zero
/// - Mixed normal and non-normal states
/// - Different undefined states `[℘?]`
/// - Exploded or vanished values (even with same escape angle)
/// - Infinity compared with anything except identical Infinity
///
/// # Mathematical Rationale
///
/// Since escaped Circle values lose magnitude information while retaining only orientation/angle data, they cannot be meaningfully compared for equality even when they share the same escape direction. Only normal values with complete magnitude and phase information support exact equality testing.
///
/// # Examples
///
/// ```rust
/// use spirix::{Circle, CircleF5E3};
///
/// // Normal Circle equality
/// let z1 = CircleF5E3::from((3, 4)); // 3 + 4i
/// let z2 = CircleF5E3::from((3, 4)); // 3 + 4i
/// let z3 = CircleF5E3::from((4, 3)); // 4 + 3i
/// assert!(z1 == z2); // Same values
/// assert!(z1 != z3); // Different values
///
/// // Zero equality
/// let zero = CircleF5E3::from((0, 0));
/// assert!(zero == 0);// Both represent zero
///
/// // Vanished isn't Zero
/// let vanished = CircleF5E3::MIN_POS.square();
/// assert!(zero != vanished);// Does not truncate to Zero!
///
/// // Vanished are not comparable
/// let same_vanished = CircleF5E3::MIN_POS.square();
/// assert!(same_vanished != vanished);
///
/// // Mixed normal/non-normal
/// let normal = CircleF5E3::from((1, 1));
/// let exploded = CircleF5E3::from((CircleF5E3::MAX, 0)) * 2;
/// assert!(normal != exploded);
///
/// // Escaped values don't equal each other
/// let exploded1 = CircleF5E3::MAX * 2;
/// let exploded2 = CircleF5E3::MAX * 2;// Same escape value
/// assert!(exploded1 != exploded2); // Escaped values non-equal
///
/// // Undefined values don't equal anything
/// let undefined = CircleF5E3::ZERO / 0;
/// assert!(!undefined.equals(&normal));
/// assert!(!undefined.equals(&undefined)); // Not even themselves
/// ```
pub(crate) fn equals(&self, other: &Circle<F, E>) -> bool {
if self.is_normal() && other.is_normal() {
self.exponent == other.exponent
&& self.real == other.real
&& self.imaginary == other.imaginary
} else {
if self.is_zero() && other.is_zero() {
return true;
}
return false;
}
}
}
#[allow(private_bounds)]
impl<
F: Integer
+ FractionConstants
+ FullInt
+ Shl<isize, Output = F>
+ Shr<isize, Output = F>
+ Shl<F, Output = F>
+ Shr<F, Output = F>
+ Shl<E, Output = F>
+ Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingSub
+ WrappingMul,
E: Integer
+ ExponentConstants
+ FullInt
+ Shl<isize, Output = E>
+ Shr<isize, Output = E>
+ Shl<E, Output = E>
+ Shr<E, Output = E>
+ Shl<F, Output = E>
+ Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingSub
+ WrappingMul,
> Scalar<F, E>
where
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
I256: From<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<E>,
{
/// Compares two Scalar values and returns their relative ordering, if determinable.
///
/// # Description
///
/// This method implements a total ordering for Scalar values that can be compared, while returning `None` for values that have no defined ordering relationship.
/// The comparison follows Spirix's mathematical ordering principles where values are arranged in a continuous spectrum from negative exploded to positive exploded.
///
/// # Ordering Hierarchy
///
/// When both values can be compared, they follow this ordering:
/// ```text
/// [-↑] < [-#] < [-↓] < [0] < [+↓] < [+#] < [+↑]
/// ```
///
/// # Returns
///
/// - `Some(Ordering::Less)` - This Scalar is less than the other
/// - `Some(Ordering::Equal)` - Both Scalars represent the same value
/// - `Some(Ordering::Greater)` - This Scalar is greater than the other
/// - `None` - The values cannot be meaningfully compared
///
/// # Unordered Values
///
/// Returns `None` for comparisons involving:
/// - Any undefined state `[℘?]`
/// - Mathematical infinity `[∞]`
/// - Same-sign escaped values of the same type:
/// - Two positive exploded values `[+↑]` with `[+↑]`
/// - Two negative exploded values `[-↑]` with `[-↑]`
/// - Two positive vanished values `[+↓]` with `[+↓]`
/// - Two negative vanished values `[-↓]` with `[-↓]`
///
/// # Implementation Details
///
/// For normal values, comparison is performed by:
/// 0. Sign comparison (negative < positive)
/// 1. Exponent comparison (larger absolute exponents for same sign)
/// 2. Fraction comparison when exponents are equal
///
/// For escaped values, only cross-sign comparisons are deterministic, as same-sign escaped values have unknown relative magnitudes.
///
/// # Examples
///
/// ```rust
/// use spirix::{Scalar, ScalarF5E3};
/// use core::cmp::Ordering;
///
/// let a = ScalarF5E3::from(42);
/// let b = ScalarF5E3::from(-17);
/// let zero = ScalarF5E3::ZERO;
///
/// // Normal value comparisons
/// assert_eq!(a.compare(&b), Some(Ordering::Greater));
/// assert_eq!(b.compare(&zero), Some(Ordering::Less));
/// assert_eq!(a.compare(&a), Some(Ordering::Equal));
///
/// // Escaped value comparisons
/// let pos_exploded = ScalarF5E3::MAX * 2;
/// let neg_exploded = ScalarF5E3::MIN * 2;
/// assert_eq!(pos_exploded.compare(&neg_exploded), Some(Ordering::Greater));
///
/// // Unordered comparisons
/// let infinity = ScalarF5E3::ONE / 0;
/// let undefined = ScalarF5E3::ZERO / 0;
/// assert_eq!(a.compare(&infinity), None);
/// assert_eq!(a.compare(&undefined), None);
///
/// // Same-sign escaped values are unordered
/// let another_pos_exploded = ScalarF5E3::MAX * 3;
/// assert_eq!(pos_exploded.compare(&another_pos_exploded), None);
/// ```
pub(crate) fn compare(&self, other: &Scalar<F, E>) -> Option<Ordering> {
if self.is_normal() && other.is_normal() {
if self.fraction.is_negative() == other.fraction.is_negative() {
if self.exponent != other.exponent {
return if self.fraction.is_negative() {
Some(other.exponent.cmp(&self.exponent))
} else {
Some(self.exponent.cmp(&other.exponent))
};
}
return Some(self.fraction.cmp(&other.fraction));
} else {
if self.fraction.is_negative() {
return Some(Ordering::Less);
} else {
return Some(Ordering::Greater);
}
}
}
if self.is_undefined() || self.is_infinite() || other.is_undefined() || other.is_infinite()
{
return None;
}
if self.is_zero() && other.is_zero() {
return Some(Ordering::Equal);
}
if self.is_zero() {
if other.fraction.is_negative() {
return Some(Ordering::Greater);
} else {
return Some(Ordering::Less);
}
}
if other.is_zero() {
if self.fraction.is_negative() {
return Some(Ordering::Less);
} else {
return Some(Ordering::Greater);
}
}
if (self.exploded() && other.exploded()) || (self.vanished() && other.vanished()) {
if self.fraction.is_negative() == other.fraction.is_negative() {
return None;
}
if self.fraction.is_negative() {
return Some(Ordering::Less);
}
}
if self.vanished() {
if other.fraction.is_negative() {
return Some(Ordering::Greater);
} else {
return Some(Ordering::Less);
}
}
if other.vanished() {
if self.fraction.is_negative() {
return Some(Ordering::Less);
} else {
return Some(Ordering::Greater);
}
}
if self.exploded() {
if self.is_negative() {
return Some(Ordering::Less);
} else {
return Some(Ordering::Greater);
}
}
if other.is_negative() {
return Some(Ordering::Greater);
} else {
return Some(Ordering::Less);
}
}
}