Debian Codegraph @

src/num.rs

raw symbols SCIP
1 //! Traits for abstracting over numeric types.
2 //!
3 //! These traits describe various numeric properties and operations. They are
4 //! similar in purpose to the immensely helpful traits in
5 //! [`num-traits`](https://crates.io/crates/num-traits/), but the structure is
6 //! different. The philosophy behind this module is to focus on capabilities,
7 //! rather than categories, and to assume as little as possible. Within reason.
8 //!
9 //! Instead of having large traits with a lot of methods and dependencies, each
10 //! operation (or group of operations), are separated into their own traits.
11 //! This allows number types to have partial compatibility by only implementing
12 //! some of the traits, and new methods can be added as new traits without
13 //! affecting old functionality.
14
15 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#, Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#, Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#, Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#};
16
17 use craterust-analyzer cargo palette 0.7.6 crate/::bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/::HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#;
18
19 #[cfg(all(not(feature = "std"), feature = "libm"))]
20 mod libm;
21 #[cfg(feature = "wide")]
22 mod widerust-analyzer cargo palette 0.7.6 num/wide/;
23
24 /// Numbers that belong to the real number set. It's both a semantic marker and
25 /// provides a constructor for number constants.
26 pub trait Realrust-analyzer cargo palette 0.7.6 num/Real# {
27 /// Create a number from an `f64` value, mainly for converting constants.
28 #[must_use]
29 fn from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(n: f64) -> Self;
30 }
31
32 /// Trait for creating a vectorized value from a scalar value.
33 pub trait FromScalarrust-analyzer cargo palette 0.7.6 num/FromScalar# {
34 /// The scalar type that is stored in each lane of `Self`. Scalar types
35 /// should set this to equal `Self`.
36 type Scalarrust-analyzer cargo palette 0.7.6 num/FromScalar#Scalar#;
37
38 /// Create a new vectorized value where each lane is `scalar`. This
39 /// corresponds to `splat` for SIMD types.
40 #[must_use]
41 fn from_scalarrust-analyzer cargo palette 0.7.6 num/FromScalar#from_scalar().(scalar: Self::Scalarrust-analyzer cargo palette 0.7.6 num/FromScalar#Scalar#) -> Self;
42 }
43
44 /// Conversion from an array of scalars to a vectorized value.
45 pub trait FromScalarArrayrust-analyzer cargo palette 0.7.6 num/FromScalarArray#<const N: usize>: FromScalarrust-analyzer cargo palette 0.7.6 num/FromScalar# {
46 /// Creates a vectorized value from an array of scalars.
47 #[must_use]
48 fn from_arrayrust-analyzer cargo palette 0.7.6 num/FromScalarArray#from_array().(scalars: [Self::Scalarrust-analyzer cargo palette 0.7.6 num/FromScalar#Scalar#; N]) -> Self;
49 }
50
51 /// Conversion from a vectorized value to an array of scalars.
52 pub trait IntoScalarArrayrust-analyzer cargo palette 0.7.6 num/IntoScalarArray#<const N: usize>: FromScalarrust-analyzer cargo palette 0.7.6 num/FromScalar# {
53 /// Creates an array of scalars from a vectorized value.
54 #[must_use]
55 fn into_arrayrust-analyzer cargo palette 0.7.6 num/IntoScalarArray#into_array().(self) -> [Self::Scalarrust-analyzer cargo palette 0.7.6 num/FromScalar#Scalar#; N];
56 }
57
58 /// Methods for the value `0`.
59 pub trait Zerorust-analyzer cargo palette 0.7.6 num/Zero# {
60 /// Create a new `0` value.
61 #[must_use]
62 fn zerorust-analyzer cargo palette 0.7.6 num/Zero#zero().() -> Self;
63 }
64
65 /// Methods for the value `1`.
66 pub trait Onerust-analyzer cargo palette 0.7.6 num/One# {
67 /// Create a new `1` value.
68 #[must_use]
69 fn onerust-analyzer cargo palette 0.7.6 num/One#one().() -> Self;
70 }
71
72 /// A helper trait that collects arithmetic traits under one name.
73 pub trait Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics#
74 where
75 Self: Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = Self>
76 + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = Self>
77 + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = Self>
78 + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = Self>
79 + Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output# = Self>
80 + Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
81 for<'a> Self: Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = Self>
82 + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = Self>
83 + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = Self>
84 + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = Self>,
85 {
86 }
87
88 impl<T> Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# for T
89 where
90 T: Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
91 + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
92 + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
93 + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
94 + Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
95 + Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
96 for<'a> Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]: Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<&'a Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
97 + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<&'a Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
98 + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<&'a Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>
99 + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<&'a Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = Selfrust-analyzer cargo palette 0.7.6 num/impl#[T][Arithmetics]>,
100 {
101 }
102
103 /// Methods for getting the largest or smallest of two values.
104 pub trait MinMaxrust-analyzer cargo palette 0.7.6 num/MinMax#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
105 /// Return the smallest of `self` and `other`.
106 #[must_use]
107 fn minrust-analyzer cargo palette 0.7.6 num/MinMax#min().(self, other: Self) -> Self;
108
109 /// Return the largest of `self` and `other`.
110 #[must_use]
111 fn maxrust-analyzer cargo palette 0.7.6 num/MinMax#max().(self, other: Self) -> Self;
112
113 /// Return a pair of `self` and `other`, where the smallest is the first
114 /// value and the largest is the second.
115 #[must_use]
116 fn min_maxrust-analyzer cargo palette 0.7.6 num/MinMax#min_max().(self, other: Self) -> (Self, Self);
117 }
118
119 /// Trigonometry methods and their inverses.
120 pub trait Trigonometryrust-analyzer cargo palette 0.7.6 num/Trigonometry#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
121 /// Compute the sine of `self` (in radians).
122 #[must_use]
123 fn sinrust-analyzer cargo palette 0.7.6 num/Trigonometry#sin().(self) -> Self;
124
125 /// Compute the cosine of `self` (in radians).
126 #[must_use]
127 fn cosrust-analyzer cargo palette 0.7.6 num/Trigonometry#cos().(self) -> Self;
128
129 /// Simultaneously compute the sine and cosine of `self` (in radians).
130 /// Returns `(sin(self), cos(self))`.
131 #[must_use]
132 fn sin_cosrust-analyzer cargo palette 0.7.6 num/Trigonometry#sin_cos().(self) -> (Self, Self);
133
134 /// Compute the tangent of `self` (in radians).
135 #[must_use]
136 fn tanrust-analyzer cargo palette 0.7.6 num/Trigonometry#tan().(self) -> Self;
137
138 /// Compute the arcsine in radians of `self`.
139 #[must_use]
140 fn asinrust-analyzer cargo palette 0.7.6 num/Trigonometry#asin().(self) -> Self;
141
142 /// Compute the arccosine in radians of `self`.
143 #[must_use]
144 fn acosrust-analyzer cargo palette 0.7.6 num/Trigonometry#acos().(self) -> Self;
145
146 /// Compute the arctangent in radians of `self`.
147 #[must_use]
148 fn atanrust-analyzer cargo palette 0.7.6 num/Trigonometry#atan().(self) -> Self;
149
150 /// Compute the arctangent in radians of `self` (y) and `other` (x).
151 #[must_use]
152 fn atan2rust-analyzer cargo palette 0.7.6 num/Trigonometry#atan2().(self, other: Self) -> Self;
153 }
154
155 /// Method for getting the absolute value of a number.
156 pub trait Absrust-analyzer cargo palette 0.7.6 num/Abs# {
157 /// Returns the absolute value of `self`.
158 #[must_use]
159 fn absrust-analyzer cargo palette 0.7.6 num/Abs#abs().(self) -> Self;
160 }
161
162 /// Method for getting the square root of a number.
163 pub trait Sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt# {
164 /// Returns the square root of `self`.
165 #[must_use]
166 fn sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt#sqrt().(self) -> Self;
167 }
168
169 /// Method for getting the cube root of a number.
170 pub trait Cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt# {
171 /// Returns the cube root of `self`.
172 #[must_use]
173 fn cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt#cbrt().(self) -> Self;
174 }
175
176 /// Method for raising a number by a real number exponent.
177 ///
178 /// The name "powf" is kept for familiarity, even though the exponent doesn't
179 /// have to be a floating point number.
180 pub trait Powfrust-analyzer cargo palette 0.7.6 num/Powf# {
181 /// Return `self` raised to the power of `exp`.
182 #[must_use]
183 fn powfrust-analyzer cargo palette 0.7.6 num/Powf#powf().(self, exp: Self) -> Self;
184 }
185
186 /// Method for raising a number by a signed integer exponent.
187 pub trait Powirust-analyzer cargo palette 0.7.6 num/Powi# {
188 /// Return `self` raised to the power of `exp`.
189 #[must_use]
190 fn powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(self, exp: i32) -> Self;
191 }
192
193 /// Method for raising a number by a n unsigned integer exponent.
194 pub trait Powurust-analyzer cargo palette 0.7.6 num/Powu# {
195 /// Return `self` raised to the power of `exp`.
196 #[must_use]
197 fn powurust-analyzer cargo palette 0.7.6 num/Powu#powu().(self, exp: u32) -> Self;
198 }
199
200 /// Method for calculating `1 / x`.
201 pub trait Reciprust-analyzer cargo palette 0.7.6 num/Recip# {
202 /// Return `1 / self`.
203 #[must_use]
204 fn reciprust-analyzer cargo palette 0.7.6 num/Recip#recip().(self) -> Self;
205 }
206
207 /// Methods for calculating `e ^ x`,
208 pub trait Exprust-analyzer cargo palette 0.7.6 num/Exp# {
209 /// Return `e ^ self`.
210 #[must_use]
211 fn exprust-analyzer cargo palette 0.7.6 num/Exp#exp().(self) -> Self;
212 }
213
214 /// Methods for checking if a number can be used as a divisor.
215 pub trait IsValidDivisorrust-analyzer cargo palette 0.7.6 num/IsValidDivisor#: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# {
216 /// Return `true` if `self` can be used as a divisor in `x / self`.
217 ///
218 /// This checks that division by `self` will result in a finite and defined
219 /// value. Integers check for `self != 0`, while floating point types call
220 /// [`is_normal`][std::primitive::f32::is_normal].
221 #[must_use]
222 fn is_valid_divisorrust-analyzer cargo palette 0.7.6 num/IsValidDivisor#is_valid_divisor().(&self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
223 }
224
225 /// Methods for calculating the lengths of a hypotenuse.
226 pub trait Hypotrust-analyzer cargo palette 0.7.6 num/Hypot# {
227 /// Returns the length of the hypotenuse formed by `self` and `other`, i.e.
228 /// `sqrt(self * self + other * other)`.
229 #[must_use]
230 fn hypotrust-analyzer cargo palette 0.7.6 num/Hypot#hypot().(self, other: Self) -> Self;
231 }
232
233 /// Methods for rounding numbers to integers.
234 pub trait Roundrust-analyzer cargo palette 0.7.6 num/Round# {
235 /// Return the nearest integer to `self`. Round half-way cases away from 0.0.
236 #[must_use]
237 fn roundrust-analyzer cargo palette 0.7.6 num/Round#round().(self) -> Self;
238
239 /// Return the largest integer less than or equal to `self`.
240 #[must_use]
241 fn floorrust-analyzer cargo palette 0.7.6 num/Round#floor().(self) -> Self;
242
243 /// Return the smallest integer greater than or equal to `self`.
244 #[must_use]
245 fn ceilrust-analyzer cargo palette 0.7.6 num/Round#ceil().(self) -> Self;
246 }
247
248 /// Trait for clamping a value.
249 pub trait Clamprust-analyzer cargo palette 0.7.6 num/Clamp# {
250 /// Clamp self to be within the range `[min, max]`.
251 #[must_use]
252 fn clamprust-analyzer cargo palette 0.7.6 num/Clamp#clamp().(self, min: Self, max: Self) -> Self;
253
254 /// Clamp self to be within the range `[min, ∞)`.
255 #[must_use]
256 fn clamp_minrust-analyzer cargo palette 0.7.6 num/Clamp#clamp_min().(self, min: Self) -> Self;
257
258 /// Clamp self to be within the range `(-∞, max]`.
259 #[must_use]
260 fn clamp_maxrust-analyzer cargo palette 0.7.6 num/Clamp#clamp_max().(self, max: Self) -> Self;
261 }
262
263 /// Assigning trait for clamping a value.
264 pub trait ClampAssignrust-analyzer cargo palette 0.7.6 num/ClampAssign# {
265 /// Clamp self to be within the range `[min, max]`.
266 fn clamp_assignrust-analyzer cargo palette 0.7.6 num/ClampAssign#clamp_assign().(&mut self, min: Self, max: Self);
267
268 /// Clamp self to be within the range `[min, ∞)`.
269 fn clamp_min_assignrust-analyzer cargo palette 0.7.6 num/ClampAssign#clamp_min_assign().(&mut self, min: Self);
270
271 /// Clamp self to be within the range `(-∞, max]`.
272 fn clamp_max_assignrust-analyzer cargo palette 0.7.6 num/ClampAssign#clamp_max_assign().(&mut self, max: Self);
273 }
274
275 /// Combined multiplication and addition operation.
276 pub trait MulAddrust-analyzer cargo palette 0.7.6 num/MulAdd# {
277 /// Multiplies self with `m` and add `a`, as in `(self * m) + a`.
278 #[must_use]
279 fn mul_addrust-analyzer cargo palette 0.7.6 num/MulAdd#mul_add().(self, m: Self, a: Self) -> Self;
280 }
281
282 /// Combined multiplication and subtraction operation.
283 pub trait MulSubrust-analyzer cargo palette 0.7.6 num/MulSub# {
284 /// Multiplies self with `m` and subtract `s`, as in `(self * m) - s`.
285 #[must_use]
286 fn mul_subrust-analyzer cargo palette 0.7.6 num/MulSub#mul_sub().(self, m: Self, s: Self) -> Self;
287 }
288
289 /// Saturating addition operation.
290 pub trait SaturatingAddrust-analyzer cargo palette 0.7.6 num/SaturatingAdd#<Rhs = Self> {
291 /// The resulting type.
292 type Outputrust-analyzer cargo palette 0.7.6 num/SaturatingAdd#Output#;
293
294 /// Returns the sum of `self` and `other`, but saturates instead of overflowing.
295 #[must_use]
296 fn saturating_addrust-analyzer cargo palette 0.7.6 num/SaturatingAdd#saturating_add().(self, other: Rhs) -> Self::Outputrust-analyzer cargo palette 0.7.6 num/SaturatingAdd#Output#;
297 }
298
299 /// Saturating subtraction operation.
300 pub trait SaturatingSubrust-analyzer cargo palette 0.7.6 num/SaturatingSub#<Rhs = Self> {
301 /// The resulting type.
302 type Outputrust-analyzer cargo palette 0.7.6 num/SaturatingSub#Output#;
303
304 /// Returns the difference of `self` and `other`, but saturates instead of overflowing.
305 #[must_use]
306 fn saturating_subrust-analyzer cargo palette 0.7.6 num/SaturatingSub#saturating_sub().(self, other: Rhs) -> Self::Outputrust-analyzer cargo palette 0.7.6 num/SaturatingSub#Output#;
307 }
308
309 /// Trait for getting a number that represents the sign of `self`.
310 pub trait Signumrust-analyzer cargo palette 0.7.6 num/Signum# {
311 /// Returns a number that represents the sign of `self`. For floating point:
312 ///
313 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
314 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
315 /// * NaN if the number is NaN
316 fn signumrust-analyzer cargo palette 0.7.6 num/Signum#signum().(self) -> Self;
317 }
318
319 /// Trait for getting the natural logarithm of `self`.
320 pub trait Lnrust-analyzer cargo palette 0.7.6 num/Ln# {
321 /// Returns the natural logarithm of `self`.
322 fn lnrust-analyzer cargo palette 0.7.6 num/Ln#ln().(self) -> Self;
323 }
324
325 macro_rules! impl_uintrust-analyzer cargo palette 0.7.6 num/impl_uint! {
326 ($($ty: ident),+) => {
327 $(
328 impl FromScalar for $ty {
329 type Scalar = Self;
330
331 #[inline]
332 fn from_scalar(scalar: Self) -> Self {
333 scalar
334 }
335 }
336
337 impl FromScalarArray<1> for $ty {
338 #[inline]
339 fn from_array(scalars: [Self; 1]) -> Self {
340 let [scalar] = scalars;
341 scalar
342 }
343 }
344
345 impl IntoScalarArray<1> for $ty {
346 #[inline]
347 fn into_array(self) -> [Self; 1] {
348 [self]
349 }
350 }
351
352 impl Zero for $ty {
353 #[inline]
354 fn zero() -> Self {
355 0
356 }
357 }
358
359 impl One for $ty {
360 #[inline]
361 fn one() -> Self {
362 1
363 }
364 }
365
366 impl MinMax for $ty {
367 #[inline]
368 fn min(self, other: Self) -> Self {
369 core::cmp::Ord::min(self, other)
370 }
371
372 #[inline]
373 fn max(self, other: Self) -> Self {
374 core::cmp::Ord::max(self, other)
375 }
376
377 #[inline]
378 fn min_max(self, other: Self) -> (Self, Self) {
379 if self > other {
380 (other, self)
381 } else {
382 (self, other)
383 }
384 }
385 }
386
387 impl Powu for $ty {
388 #[inline]
389 fn powu(self, exp: u32) -> Self {
390 pow(self, exp)
391 }
392 }
393
394 impl IsValidDivisor for $ty {
395 #[inline]
396 fn is_valid_divisor(&self) -> bool {
397 *self != 0
398 }
399 }
400
401 impl Clamp for $ty {
402 #[inline]
403 fn clamp(self, min: Self, max: Self) -> Self {
404 core::cmp::Ord::clamp(self, min, max)
405 }
406
407 #[inline]
408 fn clamp_min(self, min: Self) -> Self {
409 core::cmp::Ord::max(self, min)
410 }
411
412 #[inline]
413 fn clamp_max(self, max: Self) -> Self {
414 core::cmp::Ord::min(self, max)
415 }
416 }
417
418 impl ClampAssign for $ty {
419 #[inline]
420 fn clamp_assign(&mut self, min: Self, max: Self) {
421 *self = core::cmp::Ord::clamp(*self, min, max);
422 }
423
424 #[inline]
425 fn clamp_min_assign(&mut self, min: Self) {
426 *self = core::cmp::Ord::max(*self, min);
427 }
428
429 #[inline]
430 fn clamp_max_assign(&mut self, max: Self) {
431 *self = core::cmp::Ord::min(*self, max);
432 }
433 }
434
435 impl MulAdd for $ty {
436 #[inline]
437 fn mul_add(self, m: Self, a: Self) -> Self {
438 (self * m) + a
439 }
440 }
441
442 impl MulSub for $ty {
443 #[inline]
444 fn mul_sub(self, m: Self, s: Self) -> Self {
445 (self * m) - s
446 }
447 }
448
449 impl SaturatingAdd for $ty {
450 type Output = $ty;
451 #[inline]
452 fn saturating_add(self, other: Self) -> Self{
453 <$ty>::saturating_add(self, other)
454 }
455 }
456
457 impl SaturatingSub for $ty {
458 type Output = $ty;
459 #[inline]
460 fn saturating_sub(self, other: Self) -> Self{
461 <$ty>::saturating_sub(self, other)
462 }
463 }
464 )+
465 };
466 }
467
468 macro_rules! impl_floatrust-analyzer cargo palette 0.7.6 num/impl_float! {
469 ($($ty: ident),+) => {
470 $(
471 impl Real for $ty {
472 #[inline]
473 fn from_f64(n: f64) -> $ty {
474 n as $ty
475 }
476 }
477
478 impl FromScalar for $ty {
479 type Scalar = Self;
480
481 #[inline]
482 fn from_scalar(scalar: Self) -> Self {
483 scalar
484 }
485 }
486
487 impl FromScalarArray<1> for $ty {
488 #[inline]
489 fn from_array(scalars: [Self; 1]) -> Self {
490 let [scalar] = scalars;
491 scalar
492 }
493 }
494
495 impl IntoScalarArray<1> for $ty {
496 #[inline]
497 fn into_array(self) -> [Self; 1] {
498 [self]
499 }
500 }
501
502 impl Zero for $ty {
503 #[inline]
504 fn zero() -> Self {
505 0.0
506 }
507 }
508
509 impl One for $ty {
510 #[inline]
511 fn one() -> Self {
512 1.0
513 }
514 }
515
516 impl MinMax for $ty {
517 #[inline]
518 fn max(self, other: Self) -> Self {
519 $ty::max(self, other)
520 }
521
522 #[inline]
523 fn min(self, other: Self) -> Self {
524 $ty::min(self, other)
525 }
526
527 #[inline]
528 fn min_max(self, other: Self) -> (Self, Self) {
529 if self > other {
530 (other, self)
531 } else {
532 (self, other)
533 }
534 }
535 }
536
537 impl Powu for $ty {
538 #[inline]
539 fn powu(self, exp: u32) -> Self {
540 pow(self, exp)
541 }
542 }
543
544 impl IsValidDivisor for $ty {
545 #[inline]
546 fn is_valid_divisor(&self) -> bool {
547 $ty::is_normal(*self)
548 }
549 }
550
551 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
552 impl Trigonometry for $ty {
553 #[inline]
554 fn sin(self) -> Self {
555 $ty::sin(self)
556 }
557
558 #[inline]
559 fn cos(self) -> Self {
560 $ty::cos(self)
561 }
562
563 #[inline]
564 fn sin_cos(self) -> (Self, Self) {
565 $ty::sin_cos(self)
566 }
567
568 #[inline]
569 fn tan(self) -> Self {
570 $ty::tan(self)
571 }
572
573 #[inline]
574 fn asin(self) -> Self {
575 $ty::asin(self)
576 }
577
578 #[inline]
579 fn acos(self) -> Self {
580 $ty::acos(self)
581 }
582
583 #[inline]
584 fn atan(self) -> Self {
585 $ty::atan(self)
586 }
587
588 #[inline]
589 fn atan2(self, other: Self) -> Self {
590 $ty::atan2(self, other)
591 }
592 }
593
594 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
595 impl Abs for $ty {
596 #[inline]
597 fn abs(self) -> Self {
598 $ty::abs(self)
599 }
600 }
601
602 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
603 impl Sqrt for $ty {
604 #[inline]
605 fn sqrt(self) -> Self {
606 $ty::sqrt(self)
607 }
608 }
609
610 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
611 impl Cbrt for $ty {
612 #[inline]
613 fn cbrt(self) -> Self {
614 $ty::cbrt(self)
615 }
616 }
617
618 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
619 impl Powf for $ty {
620 #[inline]
621 fn powf(self, exp: Self) -> Self {
622 $ty::powf(self, exp)
623 }
624 }
625
626 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
627 impl Powi for $ty {
628 #[inline]
629 fn powi(self, exp: i32) -> Self {
630 $ty::powi(self, exp)
631 }
632 }
633
634 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
635 impl Recip for $ty {
636 #[inline]
637 fn recip(self) -> Self {
638 $ty::recip(self)
639 }
640 }
641
642 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
643 impl Exp for $ty {
644 #[inline]
645 fn exp(self) -> Self {
646 $ty::exp(self)
647 }
648 }
649
650 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
651 impl Hypot for $ty {
652 #[inline]
653 fn hypot(self, other: Self) -> Self {
654 $ty::hypot(self, other)
655 }
656 }
657
658 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
659 impl Round for $ty {
660 #[inline]
661 fn round(self) -> Self {
662 $ty::round(self)
663 }
664
665 #[inline]
666 fn floor(self) -> Self {
667 $ty::floor(self)
668 }
669
670 #[inline]
671 fn ceil(self) -> Self {
672 $ty::ceil(self)
673 }
674 }
675
676 impl Clamp for $ty {
677 #[inline]
678 fn clamp(self, min: Self, max: Self) -> Self {
679 $ty::clamp(self, min, max)
680 }
681
682 #[inline]
683 fn clamp_min(self, min: Self) -> Self {
684 $ty::max(self, min)
685 }
686
687 #[inline]
688 fn clamp_max(self, max: Self) -> Self {
689 $ty::min(self, max)
690 }
691 }
692
693 impl ClampAssign for $ty {
694 #[inline]
695 fn clamp_assign(&mut self, min: Self, max: Self) {
696 *self = $ty::clamp(*self, min, max);
697 }
698
699 #[inline]
700 fn clamp_min_assign(&mut self, min: Self) {
701 *self = $ty::max(*self, min);
702 }
703
704 #[inline]
705 fn clamp_max_assign(&mut self, max: Self) {
706 *self = $ty::min(*self, max);
707 }
708 }
709
710 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
711 impl MulAdd for $ty {
712 #[inline]
713 fn mul_add(self, m: Self, a: Self) -> Self {
714 $ty::mul_add(self, m, a)
715 }
716 }
717
718 impl MulSub for $ty {
719 #[inline]
720 fn mul_sub(self, m: Self, s: Self) -> Self {
721 (self * m) - s
722 }
723 }
724
725 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
726 impl Signum for $ty {
727 #[inline]
728 fn signum(self) -> Self {
729 $ty::signum(self)
730 }
731 }
732
733 #[cfg(any(feature = "std", all(test, not(feature = "libm"))))]
734 impl Ln for $ty {
735 #[inline]
736 fn ln(self) -> Self {
737 $ty::ln(self)
738 }
739 }
740 )+
741 };
742 }
743
744 impl_uintrust-analyzer cargo palette 0.7.6 num/impl_uint!!(u8, u16, u32, u64, u128);
745 impl_floatrust-analyzer cargo palette 0.7.6 num/impl_float!!(f32, f64);
746
747 /// "borrowed" from num_traits
748 ///
749 /// Raises a value to the power of exp, using exponentiation by squaring.
750 ///
751 /// Note that `0⁰` (`pow(0, 0)`) returns `1`. Mathematically this is undefined.
752 //
753 // # Example
754 //
755 // ```rust
756 // use num_traits::pow;
757 //
758 // assert_eq!(pow(2i8, 4), 16);
759 // assert_eq!(pow(6u8, 3), 216);
760 // assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you
761 // ```
762 #[inline]
763 fn powrust-analyzer cargo palette 0.7.6 num/pow().<T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Onerust-analyzer cargo palette 0.7.6 num/One# + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<T, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = T>>(mut base: T, mut exp: u32) -> T {
764 if exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 {
765 return T::onerust-analyzer cargo palette 0.7.6 num/One#one().();
766 }
767
768 while exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 {
769 base = base.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().base;
770 exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().1;
771 }
772 if exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().1 {
773 return base;
774 }
775
776 let mut acc = base.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
777 while exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().1 {
778 exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().1;
779 base = base.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().base;
780 if exp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().1 {
781 acc = acc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().base.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
782 }
783 }
784 acc
785 }
786
787 /// Trait for lanewise comparison of two values.
788 ///
789 /// This is similar to `PartialEq` and `PartialOrd`, except that it returns a
790 /// Boolean mask instead of `bool` or [`Ordering`][core::cmp::Ordering].
791 pub trait PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp#: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# {
792 /// Compares `self < other`.
793 #[must_use]
794 fn ltrust-analyzer cargo palette 0.7.6 num/PartialCmp#lt().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
795
796 /// Compares `self <= other`.
797 #[must_use]
798 fn lt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#lt_eq().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
799
800 /// Compares `self == other`.
801 #[must_use]
802 fn eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#eq().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
803
804 /// Compares `self != other`.
805 #[must_use]
806 fn neqrust-analyzer cargo palette 0.7.6 num/PartialCmp#neq().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
807
808 /// Compares `self >= other`.
809 #[must_use]
810 fn gt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt_eq().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
811
812 /// Compares `self > other`.
813 #[must_use]
814 fn gtrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
815 }
816
817 macro_rules! impl_partial_cmprust-analyzer cargo palette 0.7.6 num/impl_partial_cmp! {
818 ($($ty:ident),+) => {
819 $(
820 impl PartialCmp for $ty {
821 #[inline]
822 fn lt(&self, other: &Self) -> Self::Mask {
823 self < other
824 }
825
826 #[inline]
827 fn lt_eq(&self, other: &Self) -> Self::Mask {
828 self <= other
829 }
830
831 #[inline]
832 fn eq(&self, other: &Self) -> Self::Mask {
833 self == other
834 }
835
836 #[inline]
837 fn neq(&self, other: &Self) -> Self::Mask {
838 self != other
839 }
840
841 #[inline]
842 fn gt_eq(&self, other: &Self) -> Self::Mask {
843 self >= other
844 }
845
846 #[inline]
847 fn gt(&self, other: &Self) -> Self::Mask {
848 self > other
849 }
850 }
851 )+
852 };
853 }
854
855 impl_partial_cmprust-analyzer cargo palette 0.7.6 num/impl_partial_cmp!!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64);