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
use i256::I256;
use num_traits::{AsPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingSub};
use crate::{core::integer::*, Circle, CircleConstants, Integer, Scalar, ScalarConstants};
use core::cmp::Ordering;
use core::ops::*;
#[allow(private_bounds)]
impl<
F: Integer
+ 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
+ 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
///
/// ```ignore
/// use spirix::{Circle, CircleF5E3};
///
/// // Normal Circle equality let z1 = CircleF5E3::from((3_i32, 4_i32)); let z2 = CircleF5E3::from((3_i32, 4_i32)); let z3 = CircleF5E3::from((4_i32, 3_i32)); assert!(z1 == z2); assert!(z1 != z3);
///
/// // Zero equality let zero = CircleF5E3::from((0_i32, 0_i32)); assert!(zero == 0_i32);
///
/// // Undefined values don't equal anything let normal = CircleF5E3::from((1_i32, 1_i32)); let undefined: CircleF5E3 = CircleF5E3::ZERO / 0_i32; assert!(!undefined.equals(&normal)); assert!(!undefined.equals(&undefined));
/// ```
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
+ 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
+ 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
///
/// ```ignore
/// use spirix::{Scalar, ScalarF5E3}; use core::cmp::Ordering;
///
/// let a = ScalarF5E3::from(42_i32); let b = ScalarF5E3::from(-17_i32); 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 = ScalarF5E3::MAX * 2_i32; let neg_exploded: ScalarF5E3 = ScalarF5E3::MIN * 2_i32; assert_eq!(pos_exploded.compare(&neg_exploded), Some(Ordering::Greater));
///
/// // Unordered comparisons let infinity: ScalarF5E3 = ScalarF5E3::ONE / 0_i32; let undefined: ScalarF5E3 = ScalarF5E3::ZERO / 0_i32; assert_eq!(a.compare(&infinity), None); assert_eq!(a.compare(&undefined), None);
/// ```
pub(crate) fn compare(&self, other: &Scalar<F, E>) -> Option<Ordering> {
if self.is_normal() && other.is_normal() {
// Different signs: positive > negative regardless of exponents. Without this, cross-sign + different-exp pairs misdirect thru the cmp.reverse() branch (e.g. +exp=5 vs -exp=10 returns Less).
if self.is_negative() != other.is_negative() {
return Some(if self.is_negative() {
Ordering::Less
} else {
Ordering::Greater
});
}
// Same sign: larger exponent = larger magnitude; for negatives that means smaller value, so reverse. Unsigned compare on AMBIG=0 stored exponents gives the correct cyclic-magnitude order across the +1.0 binade boundary.
if self.exponent != other.exponent {
let cmp = self.exponent.cmp_unsigned(&other.exponent);
return Some(if self.is_negative() {
cmp.reverse()
} else {
cmp
});
}
// Same sign, same exponent: N0 stored fraction ordering (unsigned compare on stored bits) is the value ordering, cross-sign or not, because N0 positive stored bits ≥ 0x80 > any negative stored bits ≤ 0x7F.
return Some(self.fraction.cmp_unsigned(&other.fraction));
}
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() {
return Some(if other.is_positive() {
Ordering::Less
} else {
Ordering::Greater
});
}
if other.is_zero() {
return Some(if self.is_positive() {
Ordering::Greater
} else {
Ordering::Less
});
}
if (self.exploded() && other.exploded()) || (self.vanished() && other.vanished()) {
if self.is_negative() == other.is_negative() {
return None;
}
}
if self.is_positive() != other.is_positive() {
return Some(if self.is_positive() {
Ordering::Greater
} else {
Ordering::Less
});
}
if self.exploded() || other.vanished() {
return Some(if self.is_positive() {
Ordering::Greater
} else {
Ordering::Less
});
}
Some(if self.is_positive() {
Ordering::Less
} else {
Ordering::Greater
})
}
}