fixed/traits.rs
1// Copyright © 2018–2025 Trevor Spiteri
2
3// This library is free software: you can redistribute it and/or
4// modify it under the terms of either
5//
6// * the Apache License, Version 2.0 or
7// * the MIT License
8//
9// at your option.
10//
11// You should have recieved copies of the Apache License and the MIT
12// License along with the library. If not, see
13// <https://www.apache.org/licenses/LICENSE-2.0> and
14// <https://opensource.org/licenses/MIT>.
15
16/*!
17Traits for conversions and for generic use of fixed-point numbers.
18*/
19
20#![allow(deprecated)]
21
22use crate::helpers::{Private, Sealed, Widest};
23pub use crate::traits_bits::{
24 FixedBits, FixedBitsCast, FixedBitsOptionalArbitrary, FixedBitsOptionalBorsh,
25 FixedBitsOptionalNum, FixedBitsOptionalSerde,
26};
27use crate::types::extra::{LeEqU8, LeEqU16, LeEqU32, LeEqU64, LeEqU128, Unsigned};
28use crate::{
29 F128, F128Bits, FixedI8, FixedI16, FixedI32, FixedI64, FixedI128, FixedU8, FixedU16, FixedU32,
30 FixedU64, FixedU128, ParseFixedError,
31};
32#[cfg(feature = "arbitrary")]
33use arbitrary::Arbitrary;
34#[cfg(feature = "borsh")]
35use borsh::{BorshDeserialize, BorshSerialize};
36use bytemuck::{Contiguous, Pod, TransparentWrapper};
37use core::fmt::{Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex};
38use core::hash::Hash;
39use core::iter::{Product, Sum};
40use core::num::{NonZero, TryFromIntError};
41use core::ops::{
42 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
43 Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
44};
45use core::str::FromStr;
46use half::{bf16 as half_bf16, f16 as half_f16};
47#[cfg(feature = "num-traits")]
48use num_traits::{
49 bounds::Bounded,
50 cast::{FromPrimitive, ToPrimitive},
51 float::FloatConst,
52 identities::Zero,
53 ops::checked::{
54 CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr,
55 CheckedSub,
56 },
57 ops::inv::Inv,
58 ops::overflowing::{OverflowingAdd, OverflowingMul, OverflowingSub},
59 ops::saturating::{SaturatingAdd, SaturatingMul, SaturatingSub},
60 ops::wrapping::{WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub},
61};
62#[cfg(feature = "serde")]
63use serde::{de::Deserialize, ser::Serialize};
64
65#[cfg(not(feature = "arbitrary"))]
66/// This trait is used to provide supertraits to the [`Fixed`] trait depending
67/// on the crate’s [optional features], and should not be used directly.
68///
69/// If the `arbitrary` feature is enabled, [`Arbitrary`] is a supertrait of
70/// [`Fixed`].
71///
72/// [optional features]: crate#optional-features
73pub trait FixedOptionalArbitrary: Sealed {}
74
75#[cfg(feature = "arbitrary")]
76/// This trait is used to provide supertraits to the [`Fixed`] trait depending
77/// on the crate’s [optional features], and should not be used directly.
78///
79/// If the `arbitrary` feature is enabled, [`Arbitrary`] is a supertrait of
80/// [`Fixed`].
81///
82/// [optional features]: crate#optional-features
83pub trait FixedOptionalArbitrary: Sealed
84where
85 Self: for<'a> Arbitrary<'a>,
86{
87}
88
89#[cfg(not(feature = "borsh"))]
90/// This trait is used to provide supertraits to the [`Fixed`] trait depending
91/// on the crate’s [optional features], and should not be used directly.
92///
93/// If the `borsh` experimental feature is enabled, [`BorshSerialize`] and
94/// [`BorshDeserialize`] are supertraits of [`Fixed`].
95///
96/// [optional features]: crate#optional-features
97pub trait FixedOptionalBorsh: Sealed {}
98
99#[cfg(feature = "borsh")]
100/// This trait is used to provide supertraits to the [`Fixed`] trait depending
101/// on the crate’s [optional features], and should not be used directly.
102///
103/// If the `borsh` experimental feature is enabled, [`BorshSerialize`] and
104/// [`BorshDeserialize`] are supertraits of [`Fixed`].
105///
106/// [optional features]: crate#optional-features
107pub trait FixedOptionalBorsh: Sealed
108where
109 Self: BorshSerialize + BorshDeserialize,
110{
111}
112
113#[cfg(not(feature = "num-traits"))]
114/// This trait is used to provide supertraits to the [`Fixed`] trait depending
115/// on the crate’s [optional features], and should not be used directly.
116///
117/// If the `num-traits` experimental feature is enabled, the following are
118/// supertraits of [`Fixed`]:
119///
120/// * [`Zero`]
121/// * [`Bounded`]
122/// * [`Inv`]
123/// * [`CheckedAdd`], [`CheckedSub`], [`CheckedNeg`],
124/// [`CheckedMul`], [`CheckedDiv`], [`CheckedRem`],
125/// [`CheckedShl`], [`CheckedShr`]
126/// * [`SaturatingAdd`], [`SaturatingSub`], [`SaturatingMul`]
127/// * [`WrappingAdd`], [`WrappingSub`], [`WrappingNeg`],
128/// [`WrappingMul`], [`WrappingShl`], [`WrappingShr`]
129/// * [`OverflowingAdd`], [`OverflowingSub`], [`OverflowingMul`]
130/// * [`ToPrimitive`], [`FromPrimitive`]
131/// * [`FloatConst`]
132///
133/// The following are *not* supertraits of [`Fixed`], even though they
134/// are implemented for fixed-point numbers where applicable:
135///
136/// * [`ConstZero`] because of conflicts with
137/// <code>[Fixed]::[ZERO][Fixed::ZERO]</code>
138/// * [`One`] and [`ConstOne`] because not all fixed-point numbers can
139/// represent the value 1
140/// * [`Num`] because it has [`One`] as a supertrait
141/// * [`MulAdd`], [`MulAddAssign`] because
142/// <code>[MulAdd][`MulAdd`]::[mul\_add][`mul_add`]</code>
143/// conflicts with
144/// <code>[Fixed]::[mul\_add][Fixed::mul_add]</code>
145/// * [`ToBytes`], [`FromBytes`] because of conflicts with
146/// <code>[Fixed]::[Bytes][Fixed::Bytes]</code> and [`Fixed`] methods
147///
148/// Similarly, [`Signed`] and [`Unsigned`] are *not* supertraits of
149/// [`FixedSigned`] and [`FixedUnsigned`] because they have [`Num`] as
150/// a supertrait.
151///
152/// [`ConstOne`]: num_traits::identities::ConstOne
153/// [`ConstZero`]: num_traits::identities::ConstZero
154/// [`FromBytes`]: num_traits::ops::bytes::FromBytes
155/// [`MulAddAssign`]: num_traits::ops::mul_add::MulAddAssign
156/// [`MulAdd`]: num_traits::ops::mul_add::MulAdd
157/// [`Num`]: num_traits::Num
158/// [`One`]: num_traits::identities::One
159/// [`Signed`]: num_traits::sign::Signed
160/// [`ToBytes`]: num_traits::ops::bytes::ToBytes
161/// [`Unsigned`]: num_traits::sign::Unsigned
162/// [`mul_add`]: num_traits::ops::mul_add::MulAdd::mul_add
163/// [optional features]: crate#optional-features
164pub trait FixedOptionalNum: Sealed {}
165
166#[cfg(feature = "num-traits")]
167/// This trait is used to provide supertraits to the [`Fixed`] trait depending
168/// on the crate’s [optional features], and should not be used directly.
169///
170/// If the `num-traits` experimental feature is enabled, the following are
171/// supertraits of [`Fixed`]:
172///
173/// * [`Zero`]
174/// * [`Bounded`]
175/// * [`Inv`]
176/// * [`CheckedAdd`], [`CheckedSub`], [`CheckedNeg`],
177/// [`CheckedMul`], [`CheckedDiv`], [`CheckedRem`],
178/// [`CheckedShl`], [`CheckedShr`]
179/// * [`SaturatingAdd`], [`SaturatingSub`], [`SaturatingMul`]
180/// * [`WrappingAdd`], [`WrappingSub`], [`WrappingNeg`],
181/// [`WrappingMul`], [`WrappingShl`], [`WrappingShr`]
182/// * [`OverflowingAdd`], [`OverflowingSub`], [`OverflowingMul`]
183/// * [`ToPrimitive`], [`FromPrimitive`]
184/// * [`FloatConst`]
185///
186/// The following are *not* supertraits of [`Fixed`], even though they
187/// are implemented for fixed-point numbers where applicable:
188///
189/// * [`ConstZero`] because of conflicts with
190/// <code>[Fixed]::[ZERO][Fixed::ZERO]</code>
191/// * [`One`] and [`ConstOne`] because not all fixed-point numbers can
192/// represent the value 1
193/// * [`Num`] because it has [`One`] as a supertrait
194/// * [`MulAdd`], [`MulAddAssign`] because
195/// <code>[MulAdd][`MulAdd`]::[mul\_add][`mul_add`]</code>
196/// conflicts with
197/// <code>[Fixed]::[mul\_add][Fixed::mul_add]</code>
198/// * [`ToBytes`], [`FromBytes`] because of conflicts with
199/// <code>[Fixed]::[Bytes][Fixed::Bytes]</code> and [`Fixed`] methods
200///
201/// Similarly, [`Signed`] and [`Unsigned`] are *not* supertraits of
202/// [`FixedSigned`] and [`FixedUnsigned`] because they have [`Num`] as
203/// a supertrait.
204///
205/// [`ConstOne`]: num_traits::identities::ConstOne
206/// [`ConstZero`]: num_traits::identities::ConstZero
207/// [`FromBytes`]: num_traits::ops::bytes::FromBytes
208/// [`MulAddAssign`]: num_traits::ops::mul_add::MulAddAssign
209/// [`MulAdd`]: num_traits::ops::mul_add::MulAdd
210/// [`Num`]: num_traits::Num
211/// [`One`]: num_traits::identities::One
212/// [`Signed`]: num_traits::sign::Signed
213/// [`ToBytes`]: num_traits::ops::bytes::ToBytes
214/// [`Unsigned`]: num_traits::sign::Unsigned
215/// [`mul_add`]: num_traits::ops::mul_add::MulAdd::mul_add
216/// [optional features]: crate#optional-features
217pub trait FixedOptionalNum: Sealed
218where
219 Self: Zero + Bounded + Inv,
220 Self: CheckedAdd + CheckedSub + CheckedNeg + CheckedMul,
221 Self: CheckedDiv + CheckedRem + CheckedShl + CheckedShr,
222 Self: SaturatingAdd + SaturatingSub + SaturatingMul,
223 Self: WrappingAdd + WrappingSub + WrappingNeg + WrappingMul,
224 Self: WrappingShl + WrappingShr,
225 Self: OverflowingAdd + OverflowingSub + OverflowingMul,
226 Self: ToPrimitive + FromPrimitive + FloatConst,
227{
228}
229
230#[cfg(not(feature = "serde"))]
231/// This trait is used to provide supertraits to the [`Fixed`] trait depending
232/// on the crate’s [optional features], and should not be used directly.
233///
234/// If the `serde` feature is enabled and the `serde-str` feature is disabled,
235/// [`Serialize`] and [`Deserialize`] are supertraits of [`Fixed`].
236///
237/// [optional features]: crate#optional-features
238pub trait FixedOptionalSerde: Sealed {}
239
240#[cfg(feature = "serde")]
241/// This trait is used to provide supertraits to the [`Fixed`] trait depending
242/// on the crate’s [optional features], and should not be used directly.
243///
244/// If the `serde` feature is enabled and the `serde-str` feature is disabled,
245/// [`Serialize`] and [`Deserialize`] are supertraits of [`Fixed`].
246///
247/// [optional features]: crate#optional-features
248pub trait FixedOptionalSerde: Sealed
249where
250 Self: Serialize + for<'de> Deserialize<'de>,
251{
252}
253
254#[cfg(not(feature = "nightly-float"))]
255/// This trait is used to provide supertraits to the [`Fixed`] trait depending
256/// on the crate’s [optional features], and should not be used directly.
257///
258/// If the `nightly-float` feature is enabled, [`PartialOrd<f16>`][PartialOrd]
259/// and [`PartialOrd<f128>`][PartialOrd] are supertraits of [`Fixed`].
260///
261/// [optional features]: crate#optional-features
262pub trait FixedOptionalNightlyFloat: Sealed {}
263
264#[cfg(feature = "nightly-float")]
265/// This trait is used to provide supertraits to the [`Fixed`] trait depending
266/// on the crate’s [optional features], and should not be used directly.
267///
268/// If the `nightly-float` feature is enabled, [`PartialOrd<f16>`][PartialOrd]
269/// and [`PartialOrd<f128>`][PartialOrd] are supertraits of [`Fixed`].
270///
271/// [optional features]: crate#optional-features
272pub trait FixedOptionalNightlyFloat: Sealed
273where
274 Self: PartialOrd<f16> + PartialOrd<f128>,
275{
276}
277
278/// This trait is used to provide supertraits to the [`Fixed`] trait depending
279/// on the crate’s [optional features], and should not be used directly.
280///
281/// [optional features]: crate#optional-features
282pub trait FixedOptionalFeatures: Sealed
283where
284 Self: FixedOptionalArbitrary,
285 Self: FixedOptionalBorsh,
286 Self: FixedOptionalNum,
287 Self: FixedOptionalSerde,
288 Self: FixedOptionalNightlyFloat,
289{
290}
291
292/// This trait provides methods common to all fixed-point numbers.
293///
294/// It can be helpful when writing generic code that makes use of
295/// fixed-point numbers. For methods only available on signed
296/// fixed-point numbers, use the [`FixedSigned`] trait instead, and
297/// for methods only available on unsigned fixed-point numbers, use
298/// [`FixedUnsigned`].
299///
300/// This trait is sealed and cannot be implemented for more types; it
301/// is implemented for [`FixedI8`], [`FixedI16`], [`FixedI32`],
302/// [`FixedI64`], [`FixedI128`], [`FixedU8`], [`FixedU16`],
303/// [`FixedU32`], [`FixedU64`], and [`FixedU128`].
304///
305/// # Examples
306///
307/// ```rust
308/// use fixed::traits::Fixed;
309/// use fixed::types::{I8F8, I16F16};
310///
311/// fn checked_add_twice<F: Fixed>(lhs: F, rhs: F) -> Option<F> {
312/// lhs.checked_add(rhs)?.checked_add(rhs)
313/// }
314///
315/// let val1 = checked_add_twice(I8F8::from_num(5), Fixed::from_num(1.75));
316/// assert_eq!(val1, Some(Fixed::from_num(8.5)));
317/// // can use with different fixed-point type
318/// let val2 = checked_add_twice(I16F16::from_num(5), Fixed::from_num(1.75));
319/// assert_eq!(val2, Some(Fixed::from_num(8.5)));
320/// ```
321///
322/// The following example fails to compile, since the compiler cannot
323/// infer that 500 in the `checked_mul_int` call is of type `F::Bits`.
324///
325/// ```rust,compile_fail
326/// use fixed::traits::Fixed;
327///
328/// fn checked_add_times_500<F: Fixed>(lhs: F, rhs: F) -> Option<F> {
329/// rhs.checked_mul_int(500)?.checked_add(lhs)
330/// }
331/// ```
332///
333/// One way to fix this is to add a trait bound indicating that any
334/// [`u16`] (which can represent 500) can be converted into `F::Bits`.
335///
336/// ```rust
337/// use fixed::traits::Fixed;
338/// use fixed::types::U12F4;
339///
340/// fn checked_add_times_500<F: Fixed>(lhs: F, rhs: F) -> Option<F>
341/// where
342/// u16: Into<F::Bits>,
343/// {
344/// rhs.checked_mul_int(500.into())?.checked_add(lhs)
345/// }
346///
347/// let val = checked_add_times_500(U12F4::from_num(0.25), Fixed::from_num(1.5));
348/// assert_eq!(val, Some(Fixed::from_num(750.25)));
349/// ```
350///
351/// While this works in most cases, [`u16`] cannot be converted to
352/// [`i16`], even if the value 500 does fit in [`i16`], so that the
353/// following example would fail to compile.
354///
355/// ```rust,compile_fail
356/// use fixed::traits::Fixed;
357/// use fixed::types::I12F4;
358///
359/// fn checked_add_times_500<F: Fixed>(lhs: F, rhs: F) -> Option<F>
360/// where
361/// u16: Into<F::Bits>,
362/// {
363/// rhs.checked_mul_int(500.into())?.checked_add(lhs)
364/// }
365///
366/// // I12F4::Bits is i16, and u16 does not implement Into<i16>
367/// let val = checked_add_times_500(I12F4::from_num(0.25), Fixed::from_num(1.5));
368/// # let _ = val;
369/// ```
370///
371/// We can use [`TryFrom`] to fix this, as we know that
372/// `F::Bits::try_from(500_u16)` will work for both [`u16`] and
373/// [`i16`]. (The function will always return [`None`] when `F::Bits`
374/// is [`u8`] or [`i8`].)
375///
376/// ```rust
377/// use fixed::traits::Fixed;
378/// use fixed::types::I12F4;
379///
380/// fn checked_add_times_500<F: Fixed>(lhs: F, rhs: F) -> Option<F>
381/// where
382/// u16: TryInto<F::Bits>,
383/// {
384/// rhs.checked_mul_int(500.try_into().ok()?)?.checked_add(lhs)
385/// }
386///
387/// let val = checked_add_times_500(I12F4::from_num(0.25), Fixed::from_num(1.5));
388/// assert_eq!(val, Some(Fixed::from_num(750.25)));
389/// ```
390///
391/// [`TryFrom`]: core::convert::TryFrom
392pub trait Fixed
393where
394 Self: Default + Hash + Ord,
395 Self: Contiguous + Pod + TransparentWrapper<<Self as Fixed>::Bits>,
396 Self: Debug + Display + LowerExp + UpperExp,
397 Self: Binary + Octal + LowerHex + UpperHex,
398 Self: FromStr<Err = ParseFixedError>,
399 Self: FromFixed + ToFixed,
400 Self: Add<Output = Self> + AddAssign,
401 Self: Sub<Output = Self> + SubAssign,
402 Self: Mul<Output = Self> + MulAssign,
403 Self: Div<Output = Self> + DivAssign,
404 Self: Rem<Output = Self> + RemAssign,
405 Self: Mul<<Self as Fixed>::Bits, Output = Self> + MulAssign<<Self as Fixed>::Bits>,
406 Self: Div<<Self as Fixed>::Bits, Output = Self> + DivAssign<<Self as Fixed>::Bits>,
407 Self: Rem<<Self as Fixed>::Bits, Output = Self> + RemAssign<<Self as Fixed>::Bits>,
408 Self: Rem<<Self as Fixed>::NonZeroBits, Output = Self>,
409 Self: RemAssign<<Self as Fixed>::NonZeroBits>,
410 Self: Not<Output = Self>,
411 Self: BitAnd<Output = Self> + BitAndAssign,
412 Self: BitOr<Output = Self> + BitOrAssign,
413 Self: BitXor<Output = Self> + BitXorAssign,
414 Self: Shl<u32, Output = Self> + ShlAssign<u32>,
415 Self: Shr<u32, Output = Self> + ShrAssign<u32>,
416 Self: Sum + Product,
417 Self: PartialOrd<i8> + PartialOrd<i16> + PartialOrd<i32>,
418 Self: PartialOrd<i64> + PartialOrd<i128> + PartialOrd<isize>,
419 Self: PartialOrd<u8> + PartialOrd<u16> + PartialOrd<u32>,
420 Self: PartialOrd<u64> + PartialOrd<u128> + PartialOrd<usize>,
421 Self: PartialOrd<half_f16> + PartialOrd<half_bf16>,
422 Self: PartialOrd<f32> + PartialOrd<f64>,
423 Self: PartialOrd<F128> + PartialOrd<F128Bits>,
424 Self: FixedOptionalFeatures,
425 Self: Sealed,
426{
427 /// The primitive integer underlying type.
428 ///
429 /// # Examples
430 ///
431 /// ```rust
432 /// use fixed::traits::Fixed;
433 /// use fixed::types::I16F16;
434 /// // 32-bit DELTA is 0x0000_0001_i32
435 /// const DELTA_BITS: <I16F16 as Fixed>::Bits = I16F16::DELTA.to_bits();
436 /// assert_eq!(DELTA_BITS, 1i32);
437 /// ```
438 type Bits: FixedBits + From<Self::NonZeroBits>;
439
440 /// The non-zero wrapped version of [`Bits`].
441 ///
442 /// # Examples
443 ///
444 /// ```rust
445 /// use fixed::traits::Fixed;
446 /// use fixed::types::I16F16;
447 /// let val = I16F16::from_num(31);
448 /// let non_zero_5 = <I16F16 as Fixed>::NonZeroBits::new(5).unwrap();
449 /// assert_eq!(val % non_zero_5, val % 5);
450 /// ```
451 ///
452 /// [`Bits`]: Fixed::Bits
453 type NonZeroBits: TryFrom<Self::Bits, Error = TryFromIntError>;
454
455 /// A byte array with the same size as the type.
456 ///
457 /// # Examples
458 ///
459 /// ```rust
460 /// use fixed::traits::Fixed;
461 /// use fixed::types::I16F16;
462 /// // 32-bit DELTA is 0x0000_0001_i32
463 /// const DELTA_LE_BYTES: <I16F16 as Fixed>::Bytes = I16F16::DELTA.to_le_bytes();
464 /// assert_eq!(DELTA_LE_BYTES, 1i32.to_le_bytes());
465 /// ```
466 type Bytes;
467
468 /// The number of fractional bits as a compile-time [`Unsigned`] as provided
469 /// by the [*typenum* crate].
470 ///
471 /// <code>\<F as [Fixed]>::Frac::[U32]</code> is equivalent to
472 /// <code>\<F as [Fixed]>::[FRAC\_NBITS]</code>.
473 ///
474 /// `Frac` can be used as the generic parameter of fixed-point number types.
475 ///
476 /// # Examples
477 ///
478 /// ```rust
479 /// use fixed::traits::Fixed;
480 /// use fixed::types::extra::U16;
481 /// use fixed::{FixedI32, FixedI64};
482 /// type Fix1 = FixedI32::<U16>;
483 /// assert_eq!(Fix1::FRAC_NBITS, 16);
484 /// assert_eq!(Fix1::INT_NBITS, 32 - 16);
485 /// type Fix2 = FixedI64::<<Fix1 as Fixed>::Frac>;
486 /// assert_eq!(Fix2::FRAC_NBITS, 16);
487 /// assert_eq!(Fix2::INT_NBITS, 64 - 16);
488 /// ```
489 ///
490 /// [*typenum* crate]: https://crates.io/crates/typenum
491 /// [U32]: crate::types::extra::Unsigned::U32
492 /// [FRAC\_NBITS]: Fixed::FRAC_NBITS
493 type Frac: Unsigned;
494
495 /// A signed fixed-point number type with the same number of integer and
496 /// fractional bits as `Self`.
497 ///
498 /// If `Self` is signed, then `Self::Signed` is the same as `Self`.
499 ///
500 /// # Examples
501 ///
502 /// ```rust
503 /// use fixed::traits::Fixed;
504 /// use fixed::types::{I16F16, U16F16};
505 /// // I16F16::Signed is I16F16
506 /// assert_eq!(<I16F16 as Fixed>::Signed::FRAC_NBITS, I16F16::FRAC_NBITS);
507 /// assert_eq!(<I16F16 as Fixed>::Signed::INT_NBITS, I16F16::INT_NBITS);
508 /// assert_eq!(<I16F16 as Fixed>::Signed::IS_SIGNED, I16F16::IS_SIGNED);
509 /// // U16F16::Signed is I16F16
510 /// assert_eq!(<U16F16 as Fixed>::Signed::FRAC_NBITS, I16F16::FRAC_NBITS);
511 /// assert_eq!(<U16F16 as Fixed>::Signed::INT_NBITS, I16F16::INT_NBITS);
512 /// assert_eq!(<U16F16 as Fixed>::Signed::IS_SIGNED, I16F16::IS_SIGNED);
513 /// ```
514 ///
515 /// [I16F16]: crate::types::I16F16
516 /// [U16F16]: crate::types::U16F16
517 /// [types]: crate::types
518 type Signed: FixedSigned;
519
520 /// An unsigned fixed-point number type with the same number of integer and
521 /// fractional bits as `Self`.
522 ///
523 /// If `Self` is unsigned, then `Self::Unsigned` is the same as `Self`.
524 ///
525 /// # Examples
526 ///
527 /// ```rust
528 /// use fixed::traits::Fixed;
529 /// use fixed::types::{I16F16, U16F16};
530 /// // I16F16::Unsigned is U16F16
531 /// assert_eq!(<I16F16 as Fixed>::Unsigned::FRAC_NBITS, U16F16::FRAC_NBITS);
532 /// assert_eq!(<I16F16 as Fixed>::Unsigned::INT_NBITS, U16F16::INT_NBITS);
533 /// assert_eq!(<I16F16 as Fixed>::Unsigned::IS_SIGNED, U16F16::IS_SIGNED);
534 /// // U16F16::Unsigned is U16F16
535 /// assert_eq!(<U16F16 as Fixed>::Unsigned::FRAC_NBITS, U16F16::FRAC_NBITS);
536 /// assert_eq!(<U16F16 as Fixed>::Unsigned::INT_NBITS, U16F16::INT_NBITS);
537 /// assert_eq!(<U16F16 as Fixed>::Unsigned::IS_SIGNED, U16F16::IS_SIGNED);
538 /// ```
539 ///
540 /// [I16F16]: crate::types::I16F16
541 /// [U16F16]: crate::types::U16F16
542 /// [types]: crate::types
543 type Unsigned: FixedUnsigned;
544
545 /// Returns the bit pattern of `self` reinterpreted as a signed fixed-point
546 /// number of the same size.
547 ///
548 /// For signed fixed-point numbers, the returned value is equal to `self`.
549 ///
550 /// See also <code>FixedU32::[cast\_signed][FixedU32::cast_signed]</code>.
551 ///
552 /// # Examples
553 ///
554 /// ```rust
555 /// use fixed::traits::Fixed;
556 /// use fixed::types::{I16F16, U16F16};
557 ///
558 /// let i = -I16F16::DELTA;
559 /// let u = U16F16::MAX;
560 /// assert_eq!(i.cast_signed(), i);
561 /// assert_eq!(u.cast_signed(), i);
562 /// ```
563 #[must_use]
564 #[inline]
565 fn cast_signed(self) -> Self::Signed {
566 bytemuck::cast(self)
567 }
568
569 /// Returns the bit pattern of `self` reinterpreted as an unsigned
570 /// fixed-point number of the same size.
571 ///
572 /// For unsigned fixed-point numbers, the returned value is equal to `self`.
573 ///
574 /// See also
575 /// <code>FixedI32::[cast\_unsigned][FixedI32::cast_unsigned]</code>.
576 ///
577 /// # Examples
578 ///
579 /// ```rust
580 /// use fixed::traits::Fixed;
581 /// use fixed::types::{I16F16, U16F16};
582 ///
583 /// let i = -I16F16::DELTA;
584 /// let u = U16F16::MAX;
585 /// assert_eq!(i.cast_unsigned(), u);
586 /// assert_eq!(u.cast_unsigned(), u);
587 /// ```
588 #[must_use]
589 #[inline]
590 fn cast_unsigned(self) -> Self::Unsigned {
591 bytemuck::cast(self)
592 }
593
594 /// Returns a reference to `self` as [`FixedSigned`] if the type is signed,
595 /// or [`None`] if it is unsigned.
596 ///
597 /// # Examples
598 ///
599 /// ```rust
600 /// use fixed::traits::Fixed;
601 /// use fixed::types::{I16F16, U16F16};
602 ///
603 /// let i = I16F16::from_num(-3.5);
604 /// match i.get_signed() {
605 /// Some(signed) => assert_eq!(signed.signum(), -1),
606 /// None => unreachable!(),
607 /// }
608 ///
609 /// let u = U16F16::from_num(3.5);
610 /// assert!(u.get_signed().is_none());
611 /// ```
612 #[inline]
613 fn get_signed(&self) -> Option<&Self::Signed> {
614 if Self::IS_SIGNED {
615 Some(bytemuck::cast_ref(self))
616 } else {
617 None
618 }
619 }
620
621 /// Returns a reference to `self` as [`FixedUnsigned`] if the type is
622 /// unsigned, or [`None`] if it is signed.
623 ///
624 /// # Examples
625 ///
626 /// ```rust
627 /// use fixed::traits::Fixed;
628 /// use fixed::types::{I16F16, U16F16};
629 ///
630 /// let u = U16F16::from_num(3.5);
631 /// match u.get_unsigned() {
632 /// Some(unsigned) => assert_eq!(unsigned.next_power_of_two(), 4),
633 /// None => unreachable!(),
634 /// }
635 ///
636 /// let i = I16F16::from_num(3.5);
637 /// assert!(i.get_unsigned().is_none());
638 /// ```
639 #[inline]
640 fn get_unsigned(&self) -> Option<&Self::Unsigned> {
641 if Self::IS_SIGNED {
642 None
643 } else {
644 Some(bytemuck::cast_ref(self))
645 }
646 }
647
648 /// Returns a mutable reference to `self` as [`FixedSigned`] if the type is
649 /// signed, or [`None`] if it is unsigned.
650 ///
651 /// # Examples
652 ///
653 /// ```rust
654 /// use fixed::traits::Fixed;
655 /// use fixed::types::{I16F16, U16F16};
656 ///
657 /// let mut i = I16F16::from_num(-3.5);
658 /// match i.get_signed_mut() {
659 /// Some(signed) => *signed = signed.signum(),
660 /// None => unreachable!(),
661 /// }
662 /// assert_eq!(i, -1);
663 ///
664 /// let mut u = U16F16::from_num(3.5);
665 /// assert!(u.get_signed_mut().is_none());
666 /// ```
667 #[inline]
668 fn get_signed_mut(&mut self) -> Option<&mut Self::Signed> {
669 if Self::IS_SIGNED {
670 Some(bytemuck::cast_mut(self))
671 } else {
672 None
673 }
674 }
675
676 /// Returns a mutable reference to `self` as [`FixedUnsigned`] if the type
677 /// is unsigned, or [`None`] if it is signed.
678 ///
679 /// # Examples
680 ///
681 /// ```rust
682 /// use fixed::traits::Fixed;
683 /// use fixed::types::{I16F16, U16F16};
684 ///
685 /// let mut u = U16F16::from_num(3.5);
686 /// match u.get_unsigned_mut() {
687 /// Some(unsigned) => *unsigned = unsigned.next_power_of_two(),
688 /// None => unreachable!(),
689 /// }
690 /// assert_eq!(u, 4);
691 ///
692 /// let mut i = I16F16::from_num(3.5);
693 /// assert!(i.get_unsigned_mut().is_none());
694 /// ```
695 #[inline]
696 fn get_unsigned_mut(&mut self) -> Option<&mut Self::Unsigned> {
697 if Self::IS_SIGNED {
698 None
699 } else {
700 Some(bytemuck::cast_mut(self))
701 }
702 }
703
704 /// Zero.
705 ///
706 /// See also <code>FixedI32::[ZERO][FixedI32::ZERO]</code> and
707 /// <code>FixedU32::[ZERO][FixedU32::ZERO]</code>.
708 const ZERO: Self;
709
710 /// One if the fixed-point number can represent it, otherwise [`None`].
711 const TRY_ONE: Option<Self>;
712
713 /// The difference between any two successive representable numbers, <i>Δ</i>.
714 ///
715 /// See also <code>FixedI32::[DELTA][FixedI32::DELTA]</code> and
716 /// <code>FixedU32::[DELTA][FixedU32::DELTA]</code>.
717 const DELTA: Self;
718
719 /// The smallest value that can be represented.
720 ///
721 /// See also <code>FixedI32::[MIN][FixedI32::MIN]</code> and
722 /// <code>FixedU32::[MIN][FixedU32::MIN]</code>.
723 const MIN: Self;
724
725 /// The largest value that can be represented.
726 ///
727 /// See also <code>FixedI32::[MAX][FixedI32::MAX]</code> and
728 /// <code>FixedU32::[MAX][FixedU32::MAX]</code>.
729 const MAX: Self;
730
731 /// [`true`] if the type is signed.
732 ///
733 /// See also <code>FixedI32::[IS\_SIGNED][FixedI32::IS_SIGNED]</code> and
734 /// <code>FixedU32::[IS\_SIGNED][FixedU32::IS_SIGNED]</code>.
735 const IS_SIGNED: bool;
736
737 /// The number of integer bits.
738 ///
739 /// See also <code>FixedI32::[INT\_NBITS][FixedI32::INT_NBITS]</code> and
740 /// <code>FixedU32::[INT\_NBITS][FixedU32::INT_NBITS]</code>.
741 const INT_NBITS: u32;
742
743 /// The number of fractional bits.
744 ///
745 /// See also <code>FixedI32::[FRAC\_NBITS][FixedI32::FRAC_NBITS]</code> and
746 /// <code>FixedU32::[FRAC\_NBITS][FixedU32::FRAC_NBITS]</code>.
747 const FRAC_NBITS: u32;
748
749 /// Creates a fixed-point number that has a bitwise representation
750 /// identical to the given integer.
751 ///
752 /// See also <code>FixedI32::[from\_bits][FixedI32::from_bits]</code> and
753 /// <code>FixedU32::[from\_bits][FixedU32::from_bits]</code>.
754 fn from_bits(bits: Self::Bits) -> Self;
755
756 /// Creates an integer that has a bitwise representation identical
757 /// to the given fixed-point number.
758 ///
759 /// See also <code>FixedI32::[to\_bits][FixedI32::to_bits]</code> and
760 /// <code>FixedU32::[to\_bits][FixedU32::to_bits]</code>.
761 fn to_bits(self) -> Self::Bits;
762
763 /// Converts a fixed-point number from big endian to the target’s endianness.
764 ///
765 /// See also <code>FixedI32::[from\_be][FixedI32::from_be]</code> and
766 /// <code>FixedU32::[from\_be][FixedU32::from_be]</code>.
767 fn from_be(fixed: Self) -> Self;
768
769 /// Converts a fixed-point number from little endian to the target’s endianness.
770 ///
771 /// See also <code>FixedI32::[from\_le][FixedI32::from_le]</code> and
772 /// <code>FixedU32::[from\_le][FixedU32::from_le]</code>.
773 fn from_le(fixed: Self) -> Self;
774
775 /// Converts this fixed-point number to big endian from the target’s endianness.
776 ///
777 /// See also <code>FixedI32::[to\_be][FixedI32::to_be]</code> and
778 /// <code>FixedU32::[to\_be][FixedU32::to_be]</code>.
779 #[must_use]
780 fn to_be(self) -> Self;
781
782 /// Converts this fixed-point number to little endian from the target’s endianness.
783 ///
784 /// See also <code>FixedI32::[to\_le][FixedI32::to_le]</code> and
785 /// <code>FixedU32::[to\_le][FixedU32::to_le]</code>.
786 #[must_use]
787 fn to_le(self) -> Self;
788
789 ///Reverses the byte order of the fixed-point number.
790 ///
791 /// See also <code>FixedI32::[swap\_bytes][FixedI32::swap_bytes]</code> and
792 /// <code>FixedU32::[swap\_bytes][FixedU32::swap_bytes]</code>.
793 #[must_use]
794 fn swap_bytes(self) -> Self;
795
796 /// Creates a fixed-point number from its representation as a byte
797 /// array in big endian.
798 ///
799 /// See also
800 /// <code>FixedI32::[from\_be\_bytes][FixedI32::from_be_bytes]</code> and
801 /// <code>FixedU32::[from\_be\_bytes][FixedU32::from_be_bytes]</code>.
802 fn from_be_bytes(bytes: Self::Bytes) -> Self;
803
804 /// Creates a fixed-point number from its representation as a byte
805 /// array in little endian.
806 ///
807 /// See also
808 /// <code>FixedI32::[from\_le\_bytes][FixedI32::from_le_bytes]</code> and
809 /// <code>FixedU32::[from\_le\_bytes][FixedU32::from_le_bytes]</code>.
810 fn from_le_bytes(bytes: Self::Bytes) -> Self;
811
812 /// Creates a fixed-point number from its representation as a byte
813 /// array in native endian.
814 ///
815 /// See also
816 /// <code>FixedI32::[from\_ne\_bytes][FixedI32::from_ne_bytes]</code> and
817 /// <code>FixedU32::[from\_ne\_bytes][FixedU32::from_ne_bytes]</code>.
818 fn from_ne_bytes(bytes: Self::Bytes) -> Self;
819
820 /// Returns the memory representation of this fixed-point number
821 /// as a byte array in big-endian byte order.
822 ///
823 /// See also <code>FixedI32::[to\_be\_bytes][FixedI32::to_be_bytes]</code>
824 /// and <code>FixedU32::[to\_be\_bytes][FixedU32::to_be_bytes]</code>.
825 fn to_be_bytes(self) -> Self::Bytes;
826
827 /// Returns the memory representation of this fixed-point number
828 /// as a byte array in little-endian byte order.
829 ///
830 /// See also <code>FixedI32::[to\_le\_bytes][FixedI32::to_le_bytes]</code>
831 /// and <code>FixedU32::[to\_le\_bytes][FixedU32::to_le_bytes]</code>.
832 fn to_le_bytes(self) -> Self::Bytes;
833
834 /// Returns the memory representation of this fixed-point number
835 /// as a byte array in native byte order.
836 ///
837 /// See also <code>FixedI32::[to\_ne\_bytes][FixedI32::to_ne_bytes]</code>
838 /// and <code>FixedU32::[to\_ne\_bytes][FixedU32::to_ne_bytes]</code>.
839 fn to_ne_bytes(self) -> Self::Bytes;
840
841 /// Creates a fixed-point number from another number.
842 ///
843 /// Returns the same value as
844 /// <code>src.[to\_fixed][ToFixed::to_fixed]\()</code>.
845 ///
846 /// See also <code>FixedI32::[from\_num][FixedI32::from_num]</code> and
847 /// <code>FixedU32::[from\_num][FixedU32::from_num]</code>.
848 fn from_num<Src: ToFixed>(src: Src) -> Self;
849
850 /// Converts a fixed-point number to another number.
851 ///
852 /// Returns the same value as
853 /// <code>Dst::[from\_fixed][FromFixed::from_fixed]\(self)</code>.
854 ///
855 /// See also <code>FixedI32::[to\_num][FixedI32::to_num]</code> and
856 /// <code>FixedU32::[to\_num][FixedU32::to_num]</code>.
857 fn to_num<Dst: FromFixed>(self) -> Dst;
858
859 /// Creates a fixed-point number from another number if it fits,
860 /// otherwise returns [`None`].
861 ///
862 /// Returns the same value as
863 /// <code>src.[checked\_to\_fixed][ToFixed::checked_to_fixed]\()</code>.
864 ///
865 /// See also
866 /// <code>FixedI32::[checked\_from\_num][FixedI32::checked_from_num]</code>
867 /// and
868 /// <code>FixedU32::[checked\_from\_num][FixedU32::checked_from_num]</code>.
869 fn checked_from_num<Src: ToFixed>(src: Src) -> Option<Self>;
870
871 /// Converts a fixed-point number to another number if it fits,
872 /// otherwise returns [`None`].
873 ///
874 /// Returns the same value as
875 /// <code>Dst::[checked\_from\_fixed][FromFixed::checked_from_fixed]\(self)</code>.
876 ///
877 /// See also
878 /// <code>FixedI32::[checked\_to\_num][FixedI32::checked_to_num]</code> and
879 /// <code>FixedU32::[checked\_to\_num][FixedU32::checked_to_num]</code>.
880 fn checked_to_num<Dst: FromFixed>(self) -> Option<Dst>;
881
882 /// Creates a fixed-point number from another number, saturating the
883 /// value if it does not fit.
884 ///
885 /// Returns the same value as
886 /// <code>src.[saturating\_to\_fixed][ToFixed::saturating_to_fixed]\()</code>.
887 ///
888 /// See also
889 /// <code>FixedI32::[saturating\_from\_num][FixedI32::saturating_from_num]</code>
890 /// and
891 /// <code>FixedU32::[saturating\_from\_num][FixedU32::saturating_from_num]</code>.
892 fn saturating_from_num<Src: ToFixed>(src: Src) -> Self;
893
894 /// Converts a fixed-point number to another number, saturating the
895 /// value if it does not fit.
896 ///
897 /// Returns the same value as
898 /// <code>Dst::[saturating\_from\_fixed][FromFixed::saturating_from_fixed]\(self)</code>.
899 ///
900 /// See also
901 /// <code>FixedI32::[saturating\_to\_num][FixedI32::saturating_to_num]</code>
902 /// and
903 /// <code>FixedU32::[saturating\_to\_num][FixedU32::saturating_to_num]</code>.
904 fn saturating_to_num<Dst: FromFixed>(self) -> Dst;
905
906 /// Creates a fixed-point number from another number, wrapping the
907 /// value on overflow.
908 ///
909 /// Returns the same value as
910 /// <code>src.[wrapping\_to\_fixed][ToFixed::wrapping_to_fixed]\()</code>.
911 ///
912 /// See also
913 /// <code>FixedI32::[wrapping\_from\_num][FixedI32::wrapping_from_num]</code>
914 /// and
915 /// <code>FixedU32::[wrapping\_from\_num][FixedU32::wrapping_from_num]</code>.
916 fn wrapping_from_num<Src: ToFixed>(src: Src) -> Self;
917
918 /// Converts a fixed-point number to another number, wrapping the
919 /// value on overflow.
920 ///
921 /// Returns the same value as
922 /// <code>Dst::[wrapping\_from\_fixed][FromFixed::wrapping_from_fixed]\(self)</code>.
923 ///
924 /// See also
925 /// <code>FixedI32::[wrapping\_to\_num][FixedI32::wrapping_to_num]</code>
926 /// and
927 /// <code>FixedU32::[wrapping\_to\_num][FixedU32::wrapping_to_num]</code>.
928 fn wrapping_to_num<Dst: FromFixed>(self) -> Dst;
929
930 /// Creates a fixed-point number from another number, panicking on overflow.
931 ///
932 /// Returns the same value as
933 /// <code>src.[unwrapped\_to\_fixed][ToFixed::unwrapped_to_fixed]\()</code>.
934 ///
935 /// See also
936 /// <code>FixedI32::[unwrapped\_from\_num][FixedI32::unwrapped_from_num]</code>
937 /// and
938 /// <code>FixedU32::[unwrapped\_from\_num][FixedU32::unwrapped_from_num]</code>.
939 ///
940 /// # Panics
941 ///
942 /// Panics if the value does not fit.
943 #[track_caller]
944 fn unwrapped_from_num<Src: ToFixed>(src: Src) -> Self;
945
946 /// Converts a fixed-point number to another number, panicking on overflow.
947 ///
948 /// Returns the same value as
949 /// <code>Dst::[unwrapped\_from\_fixed][FromFixed::unwrapped_from_fixed]\(self)</code>.
950 ///
951 /// See also
952 /// <code>FixedI32::[unwrapped\_to\_num][FixedI32::unwrapped_to_num]</code>
953 /// and
954 /// <code>FixedU32::[unwrapped\_to\_num][FixedU32::unwrapped_to_num]</code>.
955 ///
956 /// # Panics
957 ///
958 /// Panics if the value does not fit.
959 #[track_caller]
960 fn unwrapped_to_num<Dst: FromFixed>(self) -> Dst;
961
962 /// Creates a fixed-point number from another number.
963 ///
964 /// Returns the same value as
965 /// <code>src.[overflowing\_to\_fixed][ToFixed::overflowing_to_fixed]\()</code>.
966 ///
967 /// See also
968 /// <code>FixedI32::[overflowing\_from\_num][FixedI32::overflowing_from_num]</code>
969 /// and
970 /// <code>FixedU32::[overflowing\_from\_num][FixedU32::overflowing_from_num]</code>.
971 fn overflowing_from_num<Src: ToFixed>(src: Src) -> (Self, bool);
972
973 /// Converts a fixed-point number to another number.
974 ///
975 /// Returns the same value as
976 /// <code>Dst::[overflowing\_from\_fixed][FromFixed::overflowing_from_fixed]\(self)</code>.
977 ///
978 /// See also
979 /// <code>FixedI32::[overflowing\_to\_num][FixedI32::overflowing_to_num]</code>
980 /// and
981 /// <code>FixedU32::[overflowing\_to\_num][FixedU32::overflowing_to_num]</code>.
982 fn overflowing_to_num<Dst: FromFixed>(self) -> (Dst, bool);
983
984 /// Parses a string slice containing binary digits to return a fixed-point number.
985 ///
986 /// Rounding is to the nearest, with ties rounded to even.
987 ///
988 /// See also
989 /// <code>FixedI32::[from\_str\_binary][FixedI32::from_str_binary]</code>
990 /// and
991 /// <code>FixedU32::[from\_str\_binary][FixedU32::from_str_binary]</code>.
992 fn from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
993
994 /// Parses a string slice containing octal digits to return a fixed-point number.
995 ///
996 /// Rounding is to the nearest, with ties rounded to even.
997 ///
998 /// See also
999 /// <code>FixedI32::[from\_str\_octal][FixedI32::from_str_octal]</code> and
1000 /// <code>FixedU32::[from\_str\_octal][FixedU32::from_str_octal]</code>.
1001 fn from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1002
1003 /// Parses a string slice containing hexadecimal digits to return a fixed-point number.
1004 ///
1005 /// Rounding is to the nearest, with ties rounded to even.
1006 ///
1007 /// See also <code>FixedI32::[from\_str\_hex][FixedI32::from_str_hex]</code>
1008 /// and <code>FixedU32::[from\_str\_hex][FixedU32::from_str_hex]</code>.
1009 fn from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1010
1011 /// Parses an ASCII-byte slice containing binary digits to return a fixed-point number.
1012 ///
1013 /// Rounding is to the nearest, with ties rounded to even.
1014 ///
1015 /// See also
1016 /// <code>FixedI32::[from\_ascii\_binary][FixedI32::from_ascii_binary]</code>
1017 /// and
1018 /// <code>FixedU32::[from\_ascii\_binary][FixedU32::from_ascii_binary]</code>.
1019 fn from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1020
1021 /// Parses an ASCII-byte slice containing binary digits to return a fixed-point number.
1022 ///
1023 /// Rounding is to the nearest, with ties rounded to even.
1024 ///
1025 /// See also
1026 /// <code>FixedI32::[from\_ascii\_binary][FixedI32::from_ascii_binary]</code>
1027 /// and
1028 /// <code>FixedU32::[from\_ascii\_binary][FixedU32::from_ascii_binary]</code>.
1029 fn from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1030
1031 /// Parses an ASCII-byte slice containing octal digits to return a fixed-point number.
1032 ///
1033 /// Rounding is to the nearest, with ties rounded to even.
1034 ///
1035 /// See also
1036 /// <code>FixedI32::[from\_ascii\_octal][FixedI32::from_ascii_octal]</code> and
1037 /// <code>FixedU32::[from\_ascii\_octal][FixedU32::from_ascii_octal]</code>.
1038 fn from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1039
1040 /// Parses an ASCII-byte slice containing hexadecimal digits to return a fixed-point number.
1041 ///
1042 /// Rounding is to the nearest, with ties rounded to even.
1043 ///
1044 /// See also <code>FixedI32::[from\_ascii\_hex][FixedI32::from_ascii_hex]</code>
1045 /// and <code>FixedU32::[from\_ascii\_hex][FixedU32::from_ascii_hex]</code>.
1046 fn from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1047
1048 /// Parses a string slice containing decimal digits to return a
1049 /// fixed-point number, saturating on overflow.
1050 ///
1051 /// Rounding is to the nearest, with ties rounded to even.
1052 ///
1053 /// See also
1054 /// <code>FixedI32::[saturating\_from\_str][FixedI32::saturating_from_str]</code>
1055 /// and
1056 /// <code>FixedU32::[saturating\_from\_str][FixedU32::saturating_from_str]</code>.
1057 fn saturating_from_str(src: &str) -> Result<Self, ParseFixedError>;
1058
1059 /// Parses a string slice containing binary digits to return a
1060 /// fixed-point number, saturating on overflow.
1061 ///
1062 /// Rounding is to the nearest, with ties rounded to even.
1063 ///
1064 /// See also
1065 /// <code>FixedI32::[saturating\_from\_str\_binary][FixedI32::saturating_from_str_binary]</code>
1066 /// and
1067 /// <code>FixedU32::[saturating\_from\_str\_binary][FixedU32::saturating_from_str_binary]</code>.
1068 fn saturating_from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
1069
1070 /// Parses a string slice containing octal digits to return a
1071 /// fixed-point number, saturating on overflow.
1072 ///
1073 /// Rounding is to the nearest, with ties rounded to even.
1074 ///
1075 /// See also
1076 /// <code>FixedI32::[saturating\_from\_str\_octal][FixedI32::saturating_from_str_octal]</code>
1077 /// and
1078 /// <code>FixedU32::[saturating\_from\_str\_octal][FixedU32::saturating_from_str_octal]</code>.
1079 fn saturating_from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1080
1081 /// Parses a string slice containing hexadecimal digits to return a
1082 /// fixed-point number, saturating on overflow.
1083 ///
1084 /// Rounding is to the nearest, with ties rounded to even.
1085 ///
1086 /// See also
1087 /// <code>FixedI32::[saturating\_from\_str\_hex][FixedI32::saturating_from_str_hex]</code>
1088 /// and
1089 /// <code>FixedU32::[saturating\_from\_str\_hex][FixedU32::saturating_from_str_hex]</code>.
1090 fn saturating_from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1091
1092 /// Parses an ASCII-byte slice containing decimal digits to return a
1093 /// fixed-point number, saturating on overflow.
1094 ///
1095 /// Rounding is to the nearest, with ties rounded to even.
1096 ///
1097 /// See also
1098 /// <code>FixedI32::[saturating\_from\_ascii][FixedI32::saturating_from_ascii]</code>
1099 /// and
1100 /// <code>FixedU32::[saturating\_from\_ascii][FixedU32::saturating_from_ascii]</code>.
1101 fn saturating_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1102
1103 /// Parses an ASCII-byte slice containing binary digits to return a
1104 /// fixed-point number, saturating on overflow.
1105 ///
1106 /// Rounding is to the nearest, with ties rounded to even.
1107 ///
1108 /// See also
1109 /// <code>FixedI32::[saturating\_from\_ascii\_binary][FixedI32::saturating_from_ascii_binary]</code>
1110 /// and
1111 /// <code>FixedU32::[saturating\_from\_ascii\_binary][FixedU32::saturating_from_ascii_binary]</code>.
1112 fn saturating_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1113
1114 /// Parses an ASCII-byte slice containing octal digits to return a
1115 /// fixed-point number, saturating on overflow.
1116 ///
1117 /// Rounding is to the nearest, with ties rounded to even.
1118 ///
1119 /// See also
1120 /// <code>FixedI32::[saturating\_from\_ascii\_octal][FixedI32::saturating_from_ascii_octal]</code>
1121 /// and
1122 /// <code>FixedU32::[saturating\_from\_ascii\_octal][FixedU32::saturating_from_ascii_octal]</code>.
1123 fn saturating_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1124
1125 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1126 /// fixed-point number, saturating on overflow.
1127 ///
1128 /// Rounding is to the nearest, with ties rounded to even.
1129 ///
1130 /// See also
1131 /// <code>FixedI32::[saturating\_from\_ascii\_hex][FixedI32::saturating_from_ascii_hex]</code>
1132 /// and
1133 /// <code>FixedU32::[saturating\_from\_ascii\_hex][FixedU32::saturating_from_ascii_hex]</code>.
1134 fn saturating_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1135
1136 /// Parses a string slice containing decimal digits to return a
1137 /// fixed-point number, wrapping on overflow.
1138 ///
1139 /// Rounding is to the nearest, with ties rounded to even.
1140 ///
1141 /// See also
1142 /// <code>FixedI32::[wrapping\_from\_str][FixedI32::wrapping_from_str]</code>
1143 /// and
1144 /// <code>FixedU32::[wrapping\_from\_str][FixedU32::wrapping_from_str]</code>.
1145 fn wrapping_from_str(src: &str) -> Result<Self, ParseFixedError>;
1146
1147 /// Parses a string slice containing binary digits to return a
1148 /// fixed-point number, wrapping on overflow.
1149 ///
1150 /// Rounding is to the nearest, with ties rounded to even.
1151 ///
1152 /// See also
1153 /// <code>FixedI32::[wrapping\_from\_str\_binary][FixedI32::wrapping_from_str_binary]</code>
1154 /// and
1155 /// <code>FixedU32::[wrapping\_from\_str\_binary][FixedU32::wrapping_from_str_binary]</code>.
1156 fn wrapping_from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
1157
1158 /// Parses a string slice containing octal digits to return a
1159 /// fixed-point number, wrapping on overflow.
1160 ///
1161 /// Rounding is to the nearest, with ties rounded to even.
1162 ///
1163 /// See also
1164 /// <code>FixedI32::[wrapping\_from\_str\_octal][FixedI32::wrapping_from_str_octal]</code>
1165 /// and
1166 /// <code>FixedU32::[wrapping\_from\_str\_octal][FixedU32::wrapping_from_str_octal]</code>.
1167 fn wrapping_from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1168
1169 /// Parses a string slice containing hexadecimal digits to return a
1170 /// fixed-point number, wrapping on overflow.
1171 ///
1172 /// Rounding is to the nearest, with ties rounded to even.
1173 ///
1174 /// See also
1175 /// <code>FixedI32::[wrapping\_from\_str\_hex][FixedI32::wrapping_from_str_hex]</code>
1176 /// and
1177 /// <code>FixedU32::[wrapping\_from\_str\_hex][FixedU32::wrapping_from_str_hex]</code>.
1178 fn wrapping_from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1179
1180 /// Parses an ASCII-byte slice containing decimal digits to return a
1181 /// fixed-point number, wrapping on overflow.
1182 ///
1183 /// Rounding is to the nearest, with ties rounded to even.
1184 ///
1185 /// See also
1186 /// <code>FixedI32::[wrapping\_from\_ascii][FixedI32::wrapping_from_ascii]</code>
1187 /// and
1188 /// <code>FixedU32::[wrapping\_from\_ascii][FixedU32::wrapping_from_ascii]</code>.
1189 fn wrapping_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1190
1191 /// Parses an ASCII-byte slice containing binary digits to return a
1192 /// fixed-point number, wrapping on overflow.
1193 ///
1194 /// Rounding is to the nearest, with ties rounded to even.
1195 ///
1196 /// See also
1197 /// <code>FixedI32::[wrapping\_from\_ascii\_binary][FixedI32::wrapping_from_ascii_binary]</code>
1198 /// and
1199 /// <code>FixedU32::[wrapping\_from\_ascii\_binary][FixedU32::wrapping_from_ascii_binary]</code>.
1200 fn wrapping_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1201
1202 /// Parses an ASCII-byte slice containing octal digits to return a
1203 /// fixed-point number, wrapping on overflow.
1204 ///
1205 /// Rounding is to the nearest, with ties rounded to even.
1206 ///
1207 /// See also
1208 /// <code>FixedI32::[wrapping\_from\_ascii\_octal][FixedI32::wrapping_from_ascii_octal]</code>
1209 /// and
1210 /// <code>FixedU32::[wrapping\_from\_ascii\_octal][FixedU32::wrapping_from_ascii_octal]</code>.
1211 fn wrapping_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1212
1213 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1214 /// fixed-point number, wrapping on overflow.
1215 ///
1216 /// Rounding is to the nearest, with ties rounded to even.
1217 ///
1218 /// See also
1219 /// <code>FixedI32::[wrapping\_from\_ascii\_hex][FixedI32::wrapping_from_ascii_hex]</code>
1220 /// and
1221 /// <code>FixedU32::[wrapping\_from\_ascii\_hex][FixedU32::wrapping_from_ascii_hex]</code>.
1222 fn wrapping_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1223
1224 /// Parses a string slice containing decimal digits to return a
1225 /// fixed-point number, panicking on overflow.
1226 ///
1227 /// Rounding is to the nearest, with ties rounded to even.
1228 ///
1229 /// See also
1230 /// <code>FixedI32::[unwrapped\_from\_str][FixedI32::unwrapped_from_str]</code>
1231 /// and
1232 /// <code>FixedU32::[unwrapped\_from\_str][FixedU32::unwrapped_from_str]</code>.
1233 ///
1234 /// # Panics
1235 ///
1236 /// Panics if the value does not fit or if there is a parsing error.
1237 #[track_caller]
1238 fn unwrapped_from_str(src: &str) -> Self;
1239
1240 /// Parses a string slice containing binary digits to return a
1241 /// fixed-point number, panicking on overflow.
1242 ///
1243 /// Rounding is to the nearest, with ties rounded to even.
1244 ///
1245 /// See also
1246 /// <code>FixedI32::[unwrapped\_from\_str\_binary][FixedI32::unwrapped_from_str_binary]</code>
1247 /// and
1248 /// <code>FixedU32::[unwrapped\_from\_str\_binary][FixedU32::unwrapped_from_str_binary]</code>.
1249 ///
1250 /// # Panics
1251 ///
1252 /// Panics if the value does not fit or if there is a parsing error.
1253 #[track_caller]
1254 fn unwrapped_from_str_binary(src: &str) -> Self;
1255
1256 /// Parses a string slice containing octal digits to return a
1257 /// fixed-point number, panicking on overflow.
1258 ///
1259 /// Rounding is to the nearest, with ties rounded to even.
1260 ///
1261 /// See also
1262 /// <code>FixedI32::[unwrapped\_from\_str\_octal][FixedI32::unwrapped_from_str_octal]</code>
1263 /// and
1264 /// <code>FixedU32::[unwrapped\_from\_str\_octal][FixedU32::unwrapped_from_str_octal]</code>.
1265 ///
1266 /// # Panics
1267 ///
1268 /// Panics if the value does not fit or if there is a parsing error.
1269 #[track_caller]
1270 fn unwrapped_from_str_octal(src: &str) -> Self;
1271
1272 /// Parses a string slice containing hexadecimal digits to return a
1273 /// fixed-point number, panicking on overflow.
1274 ///
1275 /// Rounding is to the nearest, with ties rounded to even.
1276 ///
1277 /// See also
1278 /// <code>FixedI32::[unwrapped\_from\_str\_hex][FixedI32::unwrapped_from_str_hex]</code>
1279 /// and
1280 /// <code>FixedU32::[unwrapped\_from\_str\_hex][FixedU32::unwrapped_from_str_hex]</code>.
1281 ///
1282 /// # Panics
1283 ///
1284 /// Panics if the value does not fit or if there is a parsing error.
1285 #[track_caller]
1286 fn unwrapped_from_str_hex(src: &str) -> Self;
1287
1288 /// Parses an ASCII-byte slice containing decimal digits to return a
1289 /// fixed-point number, panicking on overflow.
1290 ///
1291 /// Rounding is to the nearest, with ties rounded to even.
1292 ///
1293 /// See also
1294 /// <code>FixedI32::[unwrapped\_from\_ascii][FixedI32::unwrapped_from_ascii]</code>
1295 /// and
1296 /// <code>FixedU32::[unwrapped\_from\_ascii][FixedU32::unwrapped_from_ascii]</code>.
1297 ///
1298 /// # Panics
1299 ///
1300 /// Panics if the value does not fit or if there is a parsing error.
1301 #[track_caller]
1302 fn unwrapped_from_ascii(src: &[u8]) -> Self;
1303
1304 /// Parses an ASCII-byte slice containing binary digits to return a
1305 /// fixed-point number, panicking on overflow.
1306 ///
1307 /// Rounding is to the nearest, with ties rounded to even.
1308 ///
1309 /// See also
1310 /// <code>FixedI32::[unwrapped\_from\_ascii\_binary][FixedI32::unwrapped_from_ascii_binary]</code>
1311 /// and
1312 /// <code>FixedU32::[unwrapped\_from\_ascii\_binary][FixedU32::unwrapped_from_ascii_binary]</code>.
1313 ///
1314 /// # Panics
1315 ///
1316 /// Panics if the value does not fit or if there is a parsing error.
1317 #[track_caller]
1318 fn unwrapped_from_ascii_binary(src: &[u8]) -> Self;
1319
1320 /// Parses an ASCII-byte slice containing octal digits to return a
1321 /// fixed-point number, panicking on overflow.
1322 ///
1323 /// Rounding is to the nearest, with ties rounded to even.
1324 ///
1325 /// See also
1326 /// <code>FixedI32::[unwrapped\_from\_ascii\_octal][FixedI32::unwrapped_from_ascii_octal]</code>
1327 /// and
1328 /// <code>FixedU32::[unwrapped\_from\_ascii\_octal][FixedU32::unwrapped_from_ascii_octal]</code>.
1329 ///
1330 /// # Panics
1331 ///
1332 /// Panics if the value does not fit or if there is a parsing error.
1333 #[track_caller]
1334 fn unwrapped_from_ascii_octal(src: &[u8]) -> Self;
1335
1336 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1337 /// fixed-point number, panicking on overflow.
1338 ///
1339 /// Rounding is to the nearest, with ties rounded to even.
1340 ///
1341 /// See also
1342 /// <code>FixedI32::[unwrapped\_from\_ascii\_hex][FixedI32::unwrapped_from_ascii_hex]</code>
1343 /// and
1344 /// <code>FixedU32::[unwrapped\_from\_ascii\_hex][FixedU32::unwrapped_from_ascii_hex]</code>.
1345 ///
1346 /// # Panics
1347 ///
1348 /// Panics if the value does not fit or if there is a parsing error.
1349 #[track_caller]
1350 fn unwrapped_from_ascii_hex(src: &[u8]) -> Self;
1351
1352 /// Parses a string slice containing decimal digits to return a
1353 /// fixed-point number.
1354 ///
1355 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1356 /// indicating whether an overflow has occurred. On overflow, the
1357 /// wrapped value is returned.
1358 ///
1359 /// Rounding is to the nearest, with ties rounded to even.
1360 ///
1361 /// See also
1362 /// <code>FixedI32::[overflowing\_from\_str][FixedI32::overflowing_from_str]</code>
1363 /// and
1364 /// <code>FixedU32::[overflowing\_from\_str][FixedU32::overflowing_from_str]</code>.
1365 fn overflowing_from_str(src: &str) -> Result<(Self, bool), ParseFixedError>;
1366
1367 /// Parses a string slice containing binary digits to return a
1368 /// fixed-point number.
1369 ///
1370 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1371 /// indicating whether an overflow has occurred. On overflow, the
1372 /// wrapped value is returned.
1373 ///
1374 /// Rounding is to the nearest, with ties rounded to even.
1375 ///
1376 /// See also
1377 /// <code>FixedI32::[overflowing\_from\_str\_binary][FixedI32::overflowing_from_str_binary]</code>
1378 /// and
1379 /// <code>FixedU32::[overflowing\_from\_str\_binary][FixedU32::overflowing_from_str_binary]</code>.
1380 fn overflowing_from_str_binary(src: &str) -> Result<(Self, bool), ParseFixedError>;
1381
1382 /// Parses a string slice containing octal digits to return a
1383 /// fixed-point number.
1384 ///
1385 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1386 /// indicating whether an overflow has occurred. On overflow, the
1387 /// wrapped value is returned.
1388 ///
1389 /// Rounding is to the nearest, with ties rounded to even.
1390 ///
1391 /// See also
1392 /// <code>FixedI32::[overflowing\_from\_str\_octal][FixedI32::overflowing_from_str_octal]</code>
1393 /// and
1394 /// <code>FixedU32::[overflowing\_from\_str\_octal][FixedU32::overflowing_from_str_octal]</code>.
1395 fn overflowing_from_str_octal(src: &str) -> Result<(Self, bool), ParseFixedError>;
1396
1397 /// Parses a string slice containing hexadecimal digits to return a
1398 /// fixed-point number.
1399 ///
1400 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1401 /// indicating whether an overflow has occurred. On overflow, the
1402 /// wrapped value is returned.
1403 ///
1404 /// Rounding is to the nearest, with ties rounded to even.
1405 ///
1406 /// See also
1407 /// <code>FixedI32::[overflowing\_from\_str\_hex][FixedI32::overflowing_from_str_hex]</code>
1408 /// and
1409 /// <code>FixedU32::[overflowing\_from\_str\_hex][FixedU32::overflowing_from_str_hex]</code>.
1410 fn overflowing_from_str_hex(src: &str) -> Result<(Self, bool), ParseFixedError>;
1411
1412 /// Parses an ASCII-byte slice containing decimal digits to return a
1413 /// fixed-point number.
1414 ///
1415 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1416 /// indicating whether an overflow has occurred. On overflow, the
1417 /// wrapped value is returned.
1418 ///
1419 /// Rounding is to the nearest, with ties rounded to even.
1420 ///
1421 /// See also
1422 /// <code>FixedI32::[overflowing\_from\_ascii][FixedI32::overflowing_from_ascii]</code>
1423 /// and
1424 /// <code>FixedU32::[overflowing\_from\_ascii][FixedU32::overflowing_from_ascii]</code>.
1425 fn overflowing_from_ascii(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1426
1427 /// Parses an ASCII-byte slice containing binary digits to return a
1428 /// fixed-point number.
1429 ///
1430 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1431 /// indicating whether an overflow has occurred. On overflow, the
1432 /// wrapped value is returned.
1433 ///
1434 /// Rounding is to the nearest, with ties rounded to even.
1435 ///
1436 /// See also
1437 /// <code>FixedI32::[overflowing\_from\_ascii\_binary][FixedI32::overflowing_from_ascii_binary]</code>
1438 /// and
1439 /// <code>FixedU32::[overflowing\_from\_ascii\_binary][FixedU32::overflowing_from_ascii_binary]</code>.
1440 fn overflowing_from_ascii_binary(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1441
1442 /// Parses an ASCII-byte slice containing octal digits to return a
1443 /// fixed-point number.
1444 ///
1445 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1446 /// indicating whether an overflow has occurred. On overflow, the
1447 /// wrapped value is returned.
1448 ///
1449 /// Rounding is to the nearest, with ties rounded to even.
1450 ///
1451 /// See also
1452 /// <code>FixedI32::[overflowing\_from\_ascii\_octal][FixedI32::overflowing_from_ascii_octal]</code>
1453 /// and
1454 /// <code>FixedU32::[overflowing\_from\_ascii\_octal][FixedU32::overflowing_from_ascii_octal]</code>.
1455 fn overflowing_from_ascii_octal(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1456
1457 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1458 /// fixed-point number.
1459 ///
1460 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1461 /// indicating whether an overflow has occurred. On overflow, the
1462 /// wrapped value is returned.
1463 ///
1464 /// Rounding is to the nearest, with ties rounded to even.
1465 ///
1466 /// See also
1467 /// <code>FixedI32::[overflowing\_from\_ascii\_hex][FixedI32::overflowing_from_ascii_hex]</code>
1468 /// and
1469 /// <code>FixedU32::[overflowing\_from\_ascii\_hex][FixedU32::overflowing_from_ascii_hex]</code>.
1470 fn overflowing_from_ascii_hex(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1471
1472 /// Returns the integer part.
1473 ///
1474 /// See also <code>FixedI32::[int][FixedI32::int]</code> and
1475 /// <code>FixedU32::[int][FixedU32::int]</code>.
1476 #[must_use]
1477 fn int(self) -> Self;
1478
1479 /// Returns the fractional part.
1480 ///
1481 /// See also <code>FixedI32::[frac][FixedI32::frac]</code> and
1482 /// <code>FixedU32::[frac][FixedU32::frac]</code>.
1483 #[must_use]
1484 fn frac(self) -> Self;
1485
1486 /// Rounds to the next integer towards 0.
1487 ///
1488 /// See also
1489 /// <code>FixedI32::[round\_to\_zero][FixedI32::round_to_zero]</code> and
1490 /// <code>FixedU32::[round\_to\_zero][FixedU32::round_to_zero]</code>.
1491 #[must_use]
1492 fn round_to_zero(self) -> Self;
1493
1494 /// Rounds to the next integer towards +∞.
1495 ///
1496 /// See also <code>FixedI32::[ceil][FixedI32::ceil]</code> and
1497 /// <code>FixedU32::[ceil][FixedU32::ceil]</code>.
1498 #[must_use]
1499 fn ceil(self) -> Self;
1500
1501 /// Rounds to the next integer towards −∞.
1502 ///
1503 /// See also <code>FixedI32::[floor][FixedI32::floor]</code> and
1504 /// <code>FixedU32::[floor][FixedU32::floor]</code>.
1505 #[must_use]
1506 fn floor(self) -> Self;
1507
1508 /// Rounds to the nearest integer, with ties rounded away from zero.
1509 ///
1510 /// See also <code>FixedI32::[round][FixedI32::round]</code> and
1511 /// <code>FixedU32::[round][FixedU32::round]</code>.
1512 #[must_use]
1513 fn round(self) -> Self;
1514
1515 /// Rounds to the nearest integer, with ties rounded to even.
1516 ///
1517 /// See also
1518 /// <code>FixedI32::[round\_ties\_even][FixedI32::round_ties_even]</code>
1519 /// and
1520 /// <code>FixedU32::[round\_ties\_even][FixedU32::round_ties_even]</code>.
1521 #[must_use]
1522 fn round_ties_even(self) -> Self;
1523
1524 /// Checked ceil. Rounds to the next integer towards +∞, returning
1525 /// [`None`] on overflow.
1526 ///
1527 /// See also <code>FixedI32::[checked\_ceil][FixedI32::checked_ceil]</code>
1528 /// and <code>FixedU32::[checked\_ceil][FixedU32::checked_ceil]</code>.
1529 fn checked_ceil(self) -> Option<Self>;
1530
1531 /// Checked floor. Rounds to the next integer towards −∞, returning
1532 /// [`None`] on overflow.
1533 ///
1534 /// See also
1535 /// <code>FixedI32::[checked\_floor][FixedI32::checked_floor]</code> and
1536 /// <code>FixedU32::[checked\_floor][FixedU32::checked_floor]</code>.
1537 fn checked_floor(self) -> Option<Self>;
1538
1539 /// Checked round. Rounds to the nearest integer, with ties
1540 /// rounded away from zero, returning [`None`] on overflow.
1541 ///
1542 /// See also
1543 /// <code>FixedI32::[checked\_round][FixedI32::checked_round]</code> and
1544 /// <code>FixedU32::[checked\_round][FixedU32::checked_round]</code>.
1545 fn checked_round(self) -> Option<Self>;
1546
1547 /// Checked round. Rounds to the nearest integer, with ties rounded to even,
1548 /// returning [`None`] on overflow.
1549 ///
1550 /// See also
1551 /// <code>FixedI32::[checked\_round\_ties\_even][FixedI32::checked_round_ties_even]</code>
1552 /// and
1553 /// <code>FixedU32::[checked\_round\_ties\_even][FixedU32::checked_round_ties_even]</code>.
1554 fn checked_round_ties_even(self) -> Option<Self>;
1555
1556 /// Saturating ceil. Rounds to the next integer towards +∞,
1557 /// saturating on overflow.
1558 ///
1559 /// See also
1560 /// <code>FixedI32::[saturating\_ceil][FixedI32::saturating_ceil]</code> and
1561 /// <code>FixedU32::[saturating\_ceil][FixedU32::saturating_ceil]</code>.
1562 #[must_use]
1563 fn saturating_ceil(self) -> Self;
1564
1565 /// Saturating floor. Rounds to the next integer towards −∞,
1566 /// saturating on overflow.
1567 ///
1568 /// See also
1569 /// <code>FixedI32::[saturating\_floor][FixedI32::saturating_floor]</code>
1570 /// and
1571 /// <code>FixedU32::[saturating\_floor][FixedU32::saturating_floor]</code>.
1572 #[must_use]
1573 fn saturating_floor(self) -> Self;
1574
1575 /// Saturating round. Rounds to the nearest integer, with ties
1576 /// rounded away from zero, and saturating on overflow.
1577 ///
1578 /// See also
1579 /// <code>FixedI32::[saturating\_round][FixedI32::saturating_round]</code>
1580 /// and
1581 /// <code>FixedU32::[saturating\_round][FixedU32::saturating_round]</code>.
1582 #[must_use]
1583 fn saturating_round(self) -> Self;
1584
1585 /// Saturating round. Rounds to the nearest integer, with ties rounded
1586 /// to_even, and saturating on overflow.
1587 ///
1588 /// See also
1589 /// <code>FixedI32::[saturating\_round\_ties\_even][FixedI32::saturating_round_ties_even]</code>
1590 /// and
1591 /// <code>FixedU32::[saturating\_round\_ties\_even][FixedU32::saturating_round_ties_even]</code>.
1592 #[must_use]
1593 fn saturating_round_ties_even(self) -> Self;
1594
1595 /// Wrapping ceil. Rounds to the next integer towards +∞, wrapping
1596 /// on overflow.
1597 ///
1598 /// See also
1599 /// <code>FixedI32::[wrapping\_ceil][FixedI32::wrapping_ceil]</code> and
1600 /// <code>FixedU32::[wrapping\_ceil][FixedU32::wrapping_ceil]</code>.
1601 #[must_use]
1602 fn wrapping_ceil(self) -> Self;
1603
1604 /// Wrapping floor. Rounds to the next integer towards −∞,
1605 /// wrapping on overflow.
1606 ///
1607 /// See also
1608 /// <code>FixedI32::[wrapping\_floor][FixedI32::wrapping_floor]</code> and
1609 /// <code>FixedU32::[wrapping\_floor][FixedU32::wrapping_floor]</code>.
1610 #[must_use]
1611 fn wrapping_floor(self) -> Self;
1612
1613 /// Wrapping round. Rounds to the next integer to the nearest,
1614 /// with ties rounded away from zero, and wrapping on overflow.
1615 ///
1616 /// See also
1617 /// <code>FixedI32::[wrapping\_round][FixedI32::wrapping_round]</code> and
1618 /// <code>FixedU32::[wrapping\_round][FixedU32::wrapping_round]</code>.
1619 #[must_use]
1620 fn wrapping_round(self) -> Self;
1621
1622 /// Wrapping round. Rounds to the next integer to the nearest, with ties
1623 /// rounded to even, and wrapping on overflow.
1624 ///
1625 /// See also
1626 /// <code>FixedI32::[wrapping\_round\_ties\_even][FixedI32::wrapping_round_ties_even]</code>
1627 /// and
1628 /// <code>FixedU32::[wrapping\_round\_ties\_even][FixedU32::wrapping_round_ties_even]</code>.
1629 #[must_use]
1630 fn wrapping_round_ties_even(self) -> Self;
1631
1632 /// Unwrapped ceil. Rounds to the next integer towards +∞,
1633 /// panicking on overflow.
1634 ///
1635 /// See also
1636 /// <code>FixedI32::[unwrapped\_ceil][FixedI32::unwrapped_ceil]</code> and
1637 /// <code>FixedU32::[unwrapped\_ceil][FixedU32::unwrapped_ceil]</code>.
1638 ///
1639 /// # Panics
1640 ///
1641 /// Panics if the result does not fit.
1642 #[track_caller]
1643 #[must_use]
1644 fn unwrapped_ceil(self) -> Self;
1645
1646 /// Unwrapped floor. Rounds to the next integer towards −∞,
1647 /// panicking on overflow.
1648 ///
1649 /// See also
1650 /// <code>FixedI32::[unwrapped\_floor][FixedI32::unwrapped_floor]</code> and
1651 /// <code>FixedU32::[unwrapped\_floor][FixedU32::unwrapped_floor]</code>.
1652 ///
1653 /// # Panics
1654 ///
1655 /// Panics if the result does not fit.
1656 #[track_caller]
1657 #[must_use]
1658 fn unwrapped_floor(self) -> Self;
1659
1660 /// Unwrapped round. Rounds to the next integer to the nearest,
1661 /// with ties rounded away from zero, and panicking on overflow.
1662 ///
1663 /// See also
1664 /// <code>FixedI32::[unwrapped\_round][FixedI32::unwrapped_round]</code> and
1665 /// <code>FixedU32::[unwrapped\_round][FixedU32::unwrapped_round]</code>.
1666 ///
1667 /// # Panics
1668 ///
1669 /// Panics if the result does not fit.
1670 #[track_caller]
1671 #[must_use]
1672 fn unwrapped_round(self) -> Self;
1673
1674 /// Unwrapped round. Rounds to the next integer to the nearest, with ties
1675 /// rounded to even, and panicking on overflow.
1676 ///
1677 /// See also
1678 /// <code>FixedI32::[unwrapped\_round\_ties\_even][FixedI32::unwrapped_round_ties_even]</code>
1679 /// and
1680 /// <code>FixedU32::[unwrapped\_round\_ties\_even][FixedU32::unwrapped_round_ties_even]</code>.
1681 ///
1682 /// # Panics
1683 ///
1684 /// Panics if the result does not fit.
1685 #[track_caller]
1686 #[must_use]
1687 fn unwrapped_round_ties_even(self) -> Self;
1688
1689 /// Overflowing ceil. Rounds to the next integer towards +∞.
1690 ///
1691 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1692 /// indicating whether an overflow has occurred. On overflow, the
1693 /// wrapped value is returned.
1694 ///
1695 /// See also
1696 /// <code>FixedI32::[overflowing\_ceil][FixedI32::overflowing_ceil]</code>
1697 /// and
1698 /// <code>FixedU32::[overflowing\_ceil][FixedU32::overflowing_ceil]</code>.
1699 fn overflowing_ceil(self) -> (Self, bool);
1700
1701 /// Overflowing floor. Rounds to the next integer towards −∞.
1702 ///
1703 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1704 /// indicating whether an overflow has occurred. On overflow, the
1705 /// wrapped value is returned.
1706 ///
1707 /// See also
1708 /// <code>FixedI32::[overflowing\_floor][FixedI32::overflowing_floor]</code>
1709 /// and
1710 /// <code>FixedU32::[overflowing\_floor][FixedU32::overflowing_floor]</code>.
1711 fn overflowing_floor(self) -> (Self, bool);
1712
1713 /// Overflowing round. Rounds to the next integer to the nearest,
1714 /// with ties rounded away from zero.
1715 ///
1716 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1717 /// indicating whether an overflow has occurred. On overflow, the
1718 /// wrapped value is returned.
1719 ///
1720 /// See also
1721 /// <code>FixedI32::[overflowing\_round][FixedI32::overflowing_round]</code>
1722 /// and
1723 /// <code>FixedU32::[overflowing\_round][FixedU32::overflowing_round]</code>.
1724 fn overflowing_round(self) -> (Self, bool);
1725
1726 /// Overflowing round. Rounds to the next integer to the nearest, with ties
1727 /// rounded to even.
1728 ///
1729 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1730 /// indicating whether an overflow has occurred. On overflow, the
1731 /// wrapped value is returned.
1732 ///
1733 /// See also
1734 /// <code>FixedI32::[overflowing\_round\_ties\_even][FixedI32::overflowing_round_ties_even]</code>
1735 /// and
1736 /// <code>FixedU32::[overflowing\_round\_ties\_even][FixedU32::overflowing_round_ties_even]</code>.
1737 fn overflowing_round_ties_even(self) -> (Self, bool);
1738
1739 /// Returns the number of ones in the binary representation.
1740 ///
1741 /// See also <code>FixedI32::[count\_ones][FixedI32::count_ones]</code> and
1742 /// <code>FixedU32::[count\_ones][FixedU32::count_ones]</code>.
1743 #[doc(alias("popcount", "popcnt"))]
1744 fn count_ones(self) -> u32;
1745
1746 /// Returns the number of zeros in the binary representation.
1747 ///
1748 /// See also <code>FixedI32::[count\_zeros][FixedI32::count_zeros]</code>
1749 /// and <code>FixedU32::[count\_zeros][FixedU32::count_zeros]</code>.
1750 fn count_zeros(self) -> u32;
1751
1752 /// Returns the number of leading ones in the binary representation.
1753 ///
1754 /// See also <code>FixedI32::[leading\_ones][FixedI32::leading_ones]</code>
1755 /// and <code>FixedU32::[leading\_ones][FixedU32::leading_ones]</code>.
1756 fn leading_ones(self) -> u32;
1757
1758 /// Returns the number of leading zeros in the binary representation.
1759 ///
1760 /// See also
1761 /// <code>FixedI32::[leading\_zeros][FixedI32::leading_zeros]</code> and
1762 /// <code>FixedU32::[leading\_zeros][FixedU32::leading_zeros]</code>.
1763 fn leading_zeros(self) -> u32;
1764
1765 /// Returns the number of trailing ones in the binary representation.
1766 ///
1767 /// See also
1768 /// <code>FixedI32::[trailing\_ones][FixedI32::trailing_ones]</code> and
1769 /// <code>FixedU32::[trailing\_ones][FixedU32::trailing_ones]</code>.
1770 fn trailing_ones(self) -> u32;
1771
1772 /// Returns the number of trailing zeros in the binary representation.
1773 ///
1774 /// See also
1775 /// <code>FixedI32::[trailing\_zeros][FixedI32::trailing_zeros]</code> and
1776 /// <code>FixedU32::[trailing\_zeros][FixedU32::trailing_zeros]</code>.
1777 fn trailing_zeros(self) -> u32;
1778
1779 /// Integer base-2 logarithm, rounded down.
1780 ///
1781 /// See also <code>FixedI32::[int\_log2][FixedI32::int_log2]</code> and
1782 /// <code>FixedU32::[int\_log2][FixedU32::int_log2]</code>.
1783 ///
1784 /// # Panics
1785 ///
1786 /// Panics if the fixed-point number is ≤ 0.
1787 #[doc(alias("ilog2"))]
1788 fn int_log2(self) -> i32;
1789
1790 /// Integer base-10 logarithm, rounded down.
1791 ///
1792 /// See also <code>FixedI32::[int\_log10][FixedI32::int_log10]</code> and
1793 /// <code>FixedU32::[int\_log10][FixedU32::int_log10]</code>.
1794 ///
1795 /// # Panics
1796 ///
1797 /// Panics if the fixed-point number is ≤ 0.
1798 #[doc(alias("ilog10"))]
1799 fn int_log10(self) -> i32;
1800
1801 /// Integer logarithm to the specified base, rounded down.
1802 ///
1803 /// See also <code>FixedI32::[int\_log][FixedI32::int_log]</code> and
1804 /// <code>FixedU32::[int\_log][FixedU32::int_log]</code>.
1805 ///
1806 /// # Panics
1807 ///
1808 /// Panics if the fixed-point number is ≤ 0 or if the base is < 2.
1809 #[doc(alias("ilog"))]
1810 fn int_log(self, base: u32) -> i32;
1811
1812 /// Checked integer base-2 logarithm, rounded down. Returns the
1813 /// logarithm or [`None`] if the fixed-point number is ≤ 0.
1814 ///
1815 /// See also
1816 /// <code>FixedI32::[checked\_int\_log2][FixedI32::checked_int_log2]</code>
1817 /// and
1818 /// <code>FixedU32::[checked\_int\_log2][FixedU32::checked_int_log2]</code>.
1819 #[doc(alias("checked_ilog2"))]
1820 fn checked_int_log2(self) -> Option<i32>;
1821
1822 /// Checked integer base-10 logarithm, rounded down. Returns the
1823 /// logarithm or [`None`] if the fixed-point number is ≤ 0.
1824 ///
1825 /// See also
1826 /// <code>FixedI32::[checked\_int\_log10][FixedI32::checked_int_log10]</code>
1827 /// and
1828 /// <code>FixedU32::[checked\_int\_log10][FixedU32::checked_int_log10]</code>.
1829 #[doc(alias("checked_ilog10"))]
1830 fn checked_int_log10(self) -> Option<i32>;
1831
1832 /// Checked integer logarithm to the specified base, rounded down. Returns
1833 /// the logarithm, or [`None`] if the fixed-point number is ≤ 0 or if
1834 /// the base is < 2.
1835 ///
1836 /// See also
1837 /// <code>FixedI32::[checked\_int\_log][FixedI32::checked_int_log]</code>
1838 /// and
1839 /// <code>FixedU32::[checked\_int\_log][FixedU32::checked_int_log]</code>.
1840 #[doc(alias("checked_ilog"))]
1841 fn checked_int_log(self, base: u32) -> Option<i32>;
1842
1843 /// Reverses the order of the bits of the fixed-point number.
1844 ///
1845 /// See also <code>FixedI32::[reverse\_bits][FixedI32::reverse_bits]</code>
1846 /// and <code>FixedU32::[reverse\_bits][FixedU32::reverse_bits]</code>.
1847 #[must_use = "this returns the result of the operation, without modifying the original"]
1848 fn reverse_bits(self) -> Self;
1849
1850 /// Shifts to the left by `n` bits, wrapping the truncated bits to the right end.
1851 ///
1852 /// See also <code>FixedI32::[rotate\_left][FixedI32::rotate_left]</code>
1853 /// and <code>FixedU32::[rotate\_left][FixedU32::rotate_left]</code>.
1854 #[must_use = "this returns the result of the operation, without modifying the original"]
1855 fn rotate_left(self, n: u32) -> Self;
1856
1857 /// Shifts to the right by `n` bits, wrapping the truncated bits to the left end.
1858 ///
1859 /// See also <code>FixedI32::[rotate\_right][FixedI32::rotate_right]</code>
1860 /// and <code>FixedU32::[rotate\_right][FixedU32::rotate_right]</code>.
1861 #[must_use = "this returns the result of the operation, without modifying the original"]
1862 fn rotate_right(self, n: u32) -> Self;
1863
1864 /// Returns [`true`] if the number is zero.
1865 ///
1866 /// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
1867 /// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
1868 fn is_zero(self) -> bool;
1869
1870 /// Returns the distance from `self` to `other`.
1871 ///
1872 /// See also <code>FixedI32::[dist][FixedI32::dist]</code> and
1873 /// <code>FixedU32::[dist][FixedU32::dist]</code>.
1874 #[must_use = "this returns the result of the operation, without modifying the original"]
1875 fn dist(self, other: Self) -> Self;
1876
1877 /// Returns the absolute value of the difference between `self` and `other`
1878 /// using an unsigned type without any wrapping or panicking.
1879 ///
1880 /// See also <code>FixedI32::[abs\_diff][FixedI32::abs_diff]</code> and
1881 /// <code>FixedU32::[abs\_diff][FixedU32::abs_diff]</code>.
1882 #[must_use = "this returns the result of the operation, without modifying the original"]
1883 fn abs_diff(self, other: Self) -> Self::Unsigned;
1884
1885 /// Returns the mean of `self` and `other`.
1886 ///
1887 /// See also <code>FixedI32::[mean][FixedI32::mean]</code> and
1888 /// <code>FixedU32::[mean][FixedU32::mean]</code>.
1889 #[must_use = "this returns the result of the operation, without modifying the original"]
1890 fn mean(self, other: Self) -> Self;
1891
1892 /// Compute the hypotenuse of a right triange.
1893 ///
1894 /// See also <code>FixedI32::[hypot][FixedI32::hypot]</code> and
1895 /// <code>FixedU32::[hypot][FixedU32::hypot]</code>.
1896 #[must_use = "this returns the result of the operation, without modifying the original"]
1897 fn hypot(self, other: Self) -> Self;
1898
1899 /// Returns the reciprocal.
1900 ///
1901 /// See also <code>FixedI32::[recip][FixedI32::recip]</code> and
1902 /// <code>FixedU32::[recip][FixedU32::recip]</code>.
1903 ///
1904 /// # Panics
1905 ///
1906 /// Panics if `self` is zero.
1907 #[must_use]
1908 fn recip(self) -> Self;
1909
1910 /// Returns the next multiple of `other`.
1911 ///
1912 /// See also
1913 /// <code>FixedI32::[next\_multiple\_of][FixedI32::next_multiple_of]</code>
1914 /// and
1915 /// <code>FixedU32::[next\_multiple\_of][FixedU32::next_multiple_of]</code>.
1916 ///
1917 /// # Panics
1918 ///
1919 /// Panics if `other` is zero.
1920 #[must_use]
1921 fn next_multiple_of(self, other: Self) -> Self;
1922
1923 /// Multiply and add. Returns `self` × `mul` + `add`.
1924 ///
1925 /// Note that the inherent [`mul_add`] method is more flexible
1926 /// than this method and allows the `mul` parameter to have a
1927 /// fixed-point type like `self` but with a different number of
1928 /// fractional bits.
1929 ///
1930 /// [`mul_add`]: FixedI32::mul_add
1931 ///
1932 /// See also <code>FixedI32::[mul\_add][FixedI32::mul_add]</code> and
1933 /// <code>FixedU32::[mul\_add][FixedU32::mul_add]</code>.
1934 #[must_use = "this returns the result of the operation, without modifying the original"]
1935 fn mul_add(self, mul: Self, add: Self) -> Self;
1936
1937 /// Adds `self` to the product `a` × `b`.
1938 ///
1939 /// Note that the inherent [`add_prod`] method is more flexible than this
1940 /// method and allows the `a` and `b` parameters to have a fixed-point type
1941 /// like `self` but with a different number of fractional bits.
1942 ///
1943 /// [`add_prod`]: FixedI32::add_prod
1944 ///
1945 /// See also <code>FixedI32::[add\_prod][FixedI32::add_prod]</code> and
1946 /// <code>FixedU32::[add\_prod][FixedU32::add_prod]</code>.
1947 #[must_use]
1948 fn add_prod(self, a: Self, b: Self) -> Self;
1949
1950 /// Multiply and accumulate. Adds (`a` × `b`) to `self`.
1951 ///
1952 /// Note that the inherent [`mul_acc`] method is more flexible than this
1953 /// method and allows the `a` and `b` parameters to have a fixed-point type
1954 /// like `self` but with a different number of fractional bits.
1955 ///
1956 /// [`mul_acc`]: FixedI32::mul_acc
1957 ///
1958 /// See also <code>FixedI32::[mul\_acc][FixedI32::mul_acc]</code> and
1959 /// <code>FixedU32::[mul\_acc][FixedU32::mul_acc]</code>.
1960 fn mul_acc(&mut self, a: Self, b: Self);
1961
1962 /// Euclidean division by an integer.
1963 ///
1964 /// See also <code>FixedI32::[div\_euclid][FixedI32::div_euclid]</code> and
1965 /// <code>FixedU32::[div\_euclid][FixedU32::div_euclid]</code>.
1966 ///
1967 /// # Panics
1968 ///
1969 /// Panics if the divisor is zero or if the division results in overflow.
1970 #[must_use = "this returns the result of the operation, without modifying the original"]
1971 fn div_euclid(self, rhs: Self) -> Self;
1972
1973 /// Remainder for Euclidean division.
1974 ///
1975 /// See also <code>FixedI32::[rem\_euclid][FixedI32::rem_euclid]</code> and
1976 /// <code>FixedU32::[rem\_euclid][FixedU32::rem_euclid]</code>.
1977 ///
1978 /// # Panics
1979 ///
1980 /// Panics if the divisor is zero.
1981 #[must_use = "this returns the result of the operation, without modifying the original"]
1982 fn rem_euclid(self, rhs: Self) -> Self;
1983
1984 /// Euclidean division by an integer.
1985 ///
1986 /// See also
1987 /// <code>FixedI32::[div\_euclid\_int][FixedI32::div_euclid_int]</code> and
1988 /// <code>FixedU32::[div\_euclid\_int][FixedU32::div_euclid_int]</code>.
1989 ///
1990 /// # Panics
1991 ///
1992 /// Panics if the divisor is zero or if the division results in overflow.
1993 #[must_use = "this returns the result of the operation, without modifying the original"]
1994 fn div_euclid_int(self, rhs: Self::Bits) -> Self;
1995
1996 /// Remainder for Euclidean division by an integer.
1997 ///
1998 /// See also
1999 /// <code>FixedI32::[rem\_euclid\_int][FixedI32::rem_euclid_int]</code> and
2000 /// <code>FixedU32::[rem\_euclid\_int][FixedU32::rem_euclid_int]</code>.
2001 ///
2002 /// # Panics
2003 ///
2004 /// Panics if the divisor is zero or if the division results in overflow.
2005 #[must_use = "this returns the result of the operation, without modifying the original"]
2006 fn rem_euclid_int(self, rhs: Self::Bits) -> Self;
2007
2008 /// Unbounded shift left. Computes `self << rhs`, without bounding the value
2009 /// of `rhs`.
2010 ///
2011 /// See also
2012 /// <code>FixedI32::[unbounded\_shl][FixedI32::unbounded_shl]</code> and
2013 /// <code>FixedU32::[unbounded\_shl][FixedU32::unbounded_shl]</code>.
2014 #[must_use = "this returns the result of the operation, without modifying the original"]
2015 fn unbounded_shl(self, rhs: u32) -> Self;
2016
2017 /// Unbounded shift right. Computes `self >> rhs`, without bounding the
2018 /// value of `rhs`.
2019 ///
2020 /// See also
2021 /// <code>FixedI32::[unbounded\_shr][FixedI32::unbounded_shr]</code> and
2022 /// <code>FixedU32::[unbounded\_shr][FixedU32::unbounded_shr]</code>.
2023 #[must_use = "this returns the result of the operation, without modifying the original"]
2024 fn unbounded_shr(self, rhs: u32) -> Self;
2025
2026 /// Returns the square root.
2027 ///
2028 /// See also <code>FixedI32::[sqrt][FixedI32::sqrt]</code> and
2029 /// <code>FixedU32::[sqrt][FixedU32::sqrt]</code>.
2030 ///
2031 /// # Panics
2032 ///
2033 /// Panics if the number is negative.
2034 fn sqrt(self) -> Self;
2035
2036 /// Linear interpolation between `start` and `end`.
2037 ///
2038 /// See also <code>FixedI32::[lerp][FixedI32::lerp]</code> and
2039 /// <code>FixedU32::[lerp][FixedU32::lerp]</code>.
2040 #[must_use]
2041 fn lerp(self, start: Self, end: Self) -> Self;
2042
2043 /// Inverse linear interpolation between `start` and `end`.
2044 ///
2045 /// See also <code>FixedI32::[inv\_lerp][FixedI32::inv_lerp]</code> and
2046 /// <code>FixedU32::[inv\_lerp][FixedU32::inv_lerp]</code>.
2047 #[must_use]
2048 fn inv_lerp(self, start: Self, end: Self) -> Self;
2049
2050 /// Checked negation. Returns the negated value, or [`None`] on overflow.
2051 ///
2052 /// See also <code>FixedI32::[checked\_neg][FixedI32::checked_neg]</code>
2053 /// and <code>FixedU32::[checked\_neg][FixedU32::checked_neg]</code>.
2054 fn checked_neg(self) -> Option<Self>;
2055
2056 /// Checked addition. Returns the sum, or [`None`] on overflow.
2057 ///
2058 /// See also <code>FixedI32::[checked\_add][FixedI32::checked_add]</code>
2059 /// and <code>FixedU32::[checked\_add][FixedU32::checked_add]</code>.
2060 #[must_use = "this returns the result of the operation, without modifying the original"]
2061 fn checked_add(self, rhs: Self) -> Option<Self>;
2062
2063 /// Checked subtraction. Returns the difference, or [`None`] on overflow.
2064 ///
2065 /// See also <code>FixedI32::[checked\_sub][FixedI32::checked_sub]</code>
2066 /// and <code>FixedU32::[checked\_sub][FixedU32::checked_sub]</code>.
2067 #[must_use = "this returns the result of the operation, without modifying the original"]
2068 fn checked_sub(self, rhs: Self) -> Option<Self>;
2069
2070 /// Checked multiplication. Returns the product, or [`None`] on overflow.
2071 ///
2072 /// See also <code>FixedI32::[checked\_mul][FixedI32::checked_mul]</code>
2073 /// and <code>FixedU32::[checked\_mul][FixedU32::checked_mul]</code>.
2074 #[must_use = "this returns the result of the operation, without modifying the original"]
2075 fn checked_mul(self, rhs: Self) -> Option<Self>;
2076
2077 /// Checked division. Returns the quotient, or [`None`] if the
2078 /// divisor is zero or on overflow.
2079 ///
2080 /// See also <code>FixedI32::[checked\_div][FixedI32::checked_div]</code>
2081 /// and <code>FixedU32::[checked\_div][FixedU32::checked_div]</code>.
2082 #[must_use = "this returns the result of the operation, without modifying the original"]
2083 fn checked_div(self, rhs: Self) -> Option<Self>;
2084
2085 /// Checked remainder. Returns the remainder, or [`None`] if the
2086 /// divisor is zero.
2087 ///
2088 /// See also <code>FixedI32::[checked\_rem][FixedI32::checked_rem]</code>
2089 /// and <code>FixedU32::[checked\_rem][FixedU32::checked_rem]</code>.
2090 #[must_use = "this returns the result of the operation, without modifying the original"]
2091 fn checked_rem(self, rhs: Self) -> Option<Self>;
2092
2093 /// Checked reciprocal. Returns the reciprocal, or [`None`] if
2094 /// `self` is zero or on overflow.
2095 ///
2096 /// See also
2097 /// <code>FixedI32::[checked\_recip][FixedI32::checked_recip]</code> and
2098 /// <code>FixedU32::[checked\_recip][FixedU32::checked_recip]</code>.
2099 fn checked_recip(self) -> Option<Self>;
2100
2101 /// Checked next multiple of `other`. Returns the next multiple, or [`None`]
2102 /// if `other` is zero or on overflow.
2103 ///
2104 /// See also
2105 /// <code>FixedI32::[checked\_next\_multiple\_of][FixedI32::checked_next_multiple_of]</code>
2106 /// and
2107 /// <code>FixedU32::[checked\_next\_multiple\_of][FixedU32::checked_next_multiple_of]</code>.
2108 #[must_use]
2109 fn checked_next_multiple_of(self, other: Self) -> Option<Self>;
2110
2111 /// Checked multiply and add. Returns `self` × `mul` + `add`, or [`None`] on overflow.
2112 ///
2113 /// See also
2114 /// <code>FixedI32::[checked\_mul\_add][FixedI32::checked_mul_add]</code>
2115 /// and
2116 /// <code>FixedU32::[checked\_mul\_add][FixedU32::checked_mul_add]</code>.
2117 #[must_use = "this returns the result of the operation, without modifying the original"]
2118 fn checked_mul_add(self, mul: Self, add: Self) -> Option<Self>;
2119
2120 /// Adds `self` to the product `a` × `b`, returning [`None`] on overflow.
2121 ///
2122 /// See also
2123 /// <code>FixedI32::[checked\_add\_prod][FixedI32::checked_add_prod]</code>
2124 /// and
2125 /// <code>FixedU32::[checked\_add\_prod][FixedU32::checked_add_prod]</code>.
2126 #[must_use = "this `Option` may be a `None` variant indicating overflow, which should be handled"]
2127 fn checked_add_prod(self, a: Self, b: Self) -> Option<Self>;
2128
2129 /// Checked multiply and accumulate. Adds (`a` × `b`) to `self`, or returns
2130 /// [`None`] on overflow.
2131 ///
2132 /// See also
2133 /// <code>FixedI32::[checked\_mul\_acc][FixedI32::checked_mul_acc]</code>
2134 /// and
2135 /// <code>FixedU32::[checked\_mul\_acc][FixedU32::checked_mul_acc]</code>.
2136 #[must_use = "this `Option` may be a `None` variant indicating overflow, which should be handled"]
2137 fn checked_mul_acc(&mut self, a: Self, b: Self) -> Option<()>;
2138
2139 /// Checked remainder for Euclidean division. Returns the
2140 /// remainder, or [`None`] if the divisor is zero or the division
2141 /// results in overflow.
2142 ///
2143 /// See also
2144 /// <code>FixedI32::[checked\_div\_euclid][FixedI32::checked_div_euclid]</code>
2145 /// and
2146 /// <code>FixedU32::[checked\_div\_euclid][FixedU32::checked_div_euclid]</code>.
2147 #[must_use = "this returns the result of the operation, without modifying the original"]
2148 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
2149
2150 /// Checked remainder for Euclidean division. Returns the
2151 /// remainder, or [`None`] if the divisor is zero.
2152 ///
2153 /// See also
2154 /// <code>FixedI32::[checked\_rem\_euclid][FixedI32::checked_rem_euclid]</code>
2155 /// and
2156 /// <code>FixedU32::[checked\_rem\_euclid][FixedU32::checked_rem_euclid]</code>.
2157 #[must_use = "this returns the result of the operation, without modifying the original"]
2158 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
2159
2160 /// Checked multiplication by an integer. Returns the product, or
2161 /// [`None`] on overflow.
2162 ///
2163 /// See also
2164 /// <code>FixedI32::[checked\_mul\_int][FixedI32::checked_mul_int]</code>
2165 /// and
2166 /// <code>FixedU32::[checked\_mul\_int][FixedU32::checked_mul_int]</code>.
2167 #[must_use = "this returns the result of the operation, without modifying the original"]
2168 fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self>;
2169
2170 /// Checked division by an integer. Returns the quotient, or
2171 /// [`None`] if the divisor is zero or if the division results in
2172 /// overflow.
2173 ///
2174 /// See also
2175 /// <code>FixedI32::[checked\_div\_int][FixedI32::checked_div_int]</code>
2176 /// and
2177 /// <code>FixedU32::[checked\_div\_int][FixedU32::checked_div_int]</code>.
2178 #[must_use = "this returns the result of the operation, without modifying the original"]
2179 fn checked_div_int(self, rhs: Self::Bits) -> Option<Self>;
2180
2181 /// Checked fixed-point remainder for division by an integer.
2182 /// Returns the remainder, or [`None`] if the divisor is zero or
2183 /// if the division results in overflow.
2184 ///
2185 /// See also
2186 /// <code>FixedI32::[checked\_rem\_int][FixedI32::checked_rem_int]</code>
2187 /// and
2188 /// <code>FixedU32::[checked\_rem\_int][FixedU32::checked_rem_int]</code>.
2189 #[must_use = "this returns the result of the operation, without modifying the original"]
2190 fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self>;
2191
2192 /// Checked Euclidean division by an integer. Returns the
2193 /// quotient, or [`None`] if the divisor is zero or if the
2194 /// division results in overflow.
2195 ///
2196 /// See also
2197 /// <code>FixedI32::[checked\_div\_euclid\_int][FixedI32::checked_div_euclid_int]</code>
2198 /// and
2199 /// <code>FixedU32::[checked\_div\_euclid\_int][FixedU32::checked_div_euclid_int]</code>.
2200 #[must_use = "this returns the result of the operation, without modifying the original"]
2201 fn checked_div_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
2202
2203 /// Checked remainder for Euclidean division by an integer.
2204 /// Returns the remainder, or [`None`] if the divisor is zero or
2205 /// if the remainder results in overflow.
2206 ///
2207 /// See also
2208 /// <code>FixedI32::[checked\_rem\_euclid\_int][FixedI32::checked_rem_euclid_int]</code>
2209 /// and
2210 /// <code>FixedU32::[checked\_rem\_euclid\_int][FixedU32::checked_rem_euclid_int]</code>.
2211 #[must_use = "this returns the result of the operation, without modifying the original"]
2212 fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
2213
2214 /// Checked shift left. Returns the shifted number, or [`None`] if
2215 /// `rhs` ≥ the number of bits.
2216 ///
2217 /// See also <code>FixedI32::[checked\_shl][FixedI32::checked_shl]</code>
2218 /// and <code>FixedU32::[checked\_shl][FixedU32::checked_shl]</code>.
2219 #[must_use = "this returns the result of the operation, without modifying the original"]
2220 fn checked_shl(self, rhs: u32) -> Option<Self>;
2221
2222 /// Checked shift right. Returns the shifted number, or [`None`]
2223 /// if `rhs` ≥ the number of bits.
2224 ///
2225 /// See also <code>FixedI32::[checked\_shr][FixedI32::checked_shr]</code>
2226 /// and <code>FixedU32::[checked\_shr][FixedU32::checked_shr]</code>.
2227 #[must_use = "this returns the result of the operation, without modifying the original"]
2228 fn checked_shr(self, rhs: u32) -> Option<Self>;
2229
2230 /// Checked distance. Returns the distance from `self` to `other`, or
2231 /// [`None`] on overflow.
2232 ///
2233 /// See also <code>FixedI32::[checked\_dist][FixedI32::checked_dist]</code>
2234 /// and <code>FixedU32::[checked\_dist][FixedU32::checked_dist]</code>.
2235 #[must_use = "this returns the result of the operation, without modifying the original"]
2236 fn checked_dist(self, other: Self) -> Option<Self>;
2237
2238 /// Compute the hypotenuse of a right triange, returning [`None`] on
2239 /// overflow.
2240 ///
2241 /// See also
2242 /// <code>FixedI32::[checked\_hypot][FixedI32::checked_hypot]</code> and
2243 /// <code>FixedU32::[checked\_hypot][FixedU32::checked_hypot]</code>.
2244 #[must_use = "this returns the result of the operation, without modifying the original"]
2245 fn checked_hypot(self, other: Self) -> Option<Self>;
2246
2247 /// Checked square root. Returns [`None`] for negative numbers or on overflow.
2248 ///
2249 /// See also <code>FixedI32::[checked\_sqrt][FixedI32::checked_sqrt]</code>
2250 /// and <code>FixedU32::[checked\_sqrt][FixedU32::checked_sqrt]</code>.
2251 fn checked_sqrt(self) -> Option<Self>;
2252
2253 /// Checked linear interpolation between `start` and `end`. Returns [`None`]
2254 /// on overflow.
2255 ///
2256 /// See also <code>FixedI32::[checked\_lerp][FixedI32::checked_lerp]</code>
2257 /// and <code>FixedU32::[checked\_lerp][FixedU32::checked_lerp]</code>.
2258 fn checked_lerp(self, start: Self, end: Self) -> Option<Self>;
2259
2260 /// Checked inverse linear interpolation between `start` and `end`. Returns
2261 /// [`None`] when `start` = `end` or on overflow.
2262 ///
2263 /// See also
2264 /// <code>FixedI32::[checked\_inv\_lerp][FixedI32::checked_inv_lerp]</code>
2265 /// and
2266 /// <code>FixedU32::[checked\_inv\_lerp][FixedU32::checked_inv_lerp]</code>.
2267 fn checked_inv_lerp(self, start: Self, end: Self) -> Option<Self>;
2268
2269 /// Saturated negation. Returns the negated value, saturating on overflow.
2270 ///
2271 /// See also
2272 /// <code>FixedI32::[saturating\_neg][FixedI32::saturating_neg]</code> and
2273 /// <code>FixedU32::[saturating\_neg][FixedU32::saturating_neg]</code>.
2274 #[must_use]
2275 fn saturating_neg(self) -> Self;
2276
2277 /// Saturating addition. Returns the sum, saturating on overflow.
2278 ///
2279 /// See also
2280 /// <code>FixedI32::[saturating\_add][FixedI32::saturating_add]</code> and
2281 /// <code>FixedU32::[saturating\_add][FixedU32::saturating_add]</code>.
2282 #[must_use = "this returns the result of the operation, without modifying the original"]
2283 fn saturating_add(self, rhs: Self) -> Self;
2284
2285 /// Saturating subtraction. Returns the difference, saturating on overflow.
2286 ///
2287 /// See also
2288 /// <code>FixedI32::[saturating\_sub][FixedI32::saturating_sub]</code> and
2289 /// <code>FixedU32::[saturating\_sub][FixedU32::saturating_sub]</code>.
2290 #[must_use = "this returns the result of the operation, without modifying the original"]
2291 fn saturating_sub(self, rhs: Self) -> Self;
2292
2293 /// Saturating multiplication. Returns the product, saturating on overflow.
2294 ///
2295 /// See also
2296 /// <code>FixedI32::[saturating\_mul][FixedI32::saturating_mul]</code> and
2297 /// <code>FixedU32::[saturating\_mul][FixedU32::saturating_mul]</code>.
2298 #[must_use = "this returns the result of the operation, without modifying the original"]
2299 fn saturating_mul(self, rhs: Self) -> Self;
2300
2301 /// Saturating division. Returns the quotient, saturating on overflow.
2302 ///
2303 /// See also
2304 /// <code>FixedI32::[saturating\_div][FixedI32::saturating_div]</code> and
2305 /// <code>FixedU32::[saturating\_div][FixedU32::saturating_div]</code>.
2306 ///
2307 /// # Panics
2308 ///
2309 /// Panics if the divisor is zero.
2310 #[must_use = "this returns the result of the operation, without modifying the original"]
2311 fn saturating_div(self, rhs: Self) -> Self;
2312
2313 /// Saturating reciprocal.
2314 ///
2315 /// See also
2316 /// <code>FixedI32::[saturating\_recip][FixedI32::saturating_recip]</code>
2317 /// and
2318 /// <code>FixedU32::[saturating\_recip][FixedU32::saturating_recip]</code>.
2319 ///
2320 /// # Panics
2321 ///
2322 /// Panics if `self` is zero.
2323 #[must_use]
2324 fn saturating_recip(self) -> Self;
2325
2326 /// Saturating next multiple of `other`.
2327 ///
2328 /// See also
2329 /// <code>FixedI32::[saturating\_next\_multiple\_of][FixedI32::saturating_next_multiple_of]</code>
2330 /// and
2331 /// <code>FixedU32::[saturating\_next\_multiple\_of][FixedU32::saturating_next_multiple_of]</code>.
2332 ///
2333 /// # Panics
2334 ///
2335 /// Panics if `other` is zero.
2336 #[must_use]
2337 fn saturating_next_multiple_of(self, other: Self) -> Self;
2338
2339 /// Saturating multiply and add. Returns `self` × `mul` + `add`, saturating on overflow.
2340 ///
2341 /// See also
2342 /// <code>FixedI32::[saturating\_mul\_add][FixedI32::saturating_mul_add]</code>
2343 /// and
2344 /// <code>FixedU32::[saturating\_mul\_add][FixedU32::saturating_mul_add]</code>.
2345 #[must_use = "this returns the result of the operation, without modifying the original"]
2346 fn saturating_mul_add(self, mul: Self, add: Self) -> Self;
2347
2348 /// Adds `self` to the product `a` × `b`, saturating on overflow.
2349 ///
2350 /// See also
2351 /// <code>FixedI32::[saturating\_add\_prod][FixedI32::saturating_add_prod]</code>
2352 /// and
2353 /// <code>FixedU32::[saturating\_add\_prod][FixedU32::saturating_add_prod]</code>.
2354 #[must_use]
2355 fn saturating_add_prod(self, a: Self, b: Self) -> Self;
2356
2357 /// Saturating multiply and add. Adds (`a` × `b`) to `self`, saturating on overflow.
2358 ///
2359 /// See also
2360 /// <code>FixedI32::[saturating\_mul\_acc][FixedI32::saturating_mul_acc]</code>
2361 /// and
2362 /// <code>FixedU32::[saturating\_mul\_acc][FixedU32::saturating_mul_acc]</code>.
2363 fn saturating_mul_acc(&mut self, a: Self, b: Self);
2364
2365 /// Saturating Euclidean division. Returns the quotient, saturating on overflow.
2366 ///
2367 /// See also
2368 /// <code>FixedI32::[saturating\_div\_euclid][FixedI32::saturating_div_euclid]</code>
2369 /// and
2370 /// <code>FixedU32::[saturating\_div\_euclid][FixedU32::saturating_div_euclid]</code>.
2371 ///
2372 /// # Panics
2373 ///
2374 /// Panics if the divisor is zero.
2375 #[must_use = "this returns the result of the operation, without modifying the original"]
2376 fn saturating_div_euclid(self, rhs: Self) -> Self;
2377
2378 /// Saturating multiplication by an integer. Returns the product, saturating on overflow.
2379 ///
2380 /// See also
2381 /// <code>FixedI32::[saturating\_mul\_int][FixedI32::saturating_mul_int]</code>
2382 /// and
2383 /// <code>FixedU32::[saturating\_mul\_int][FixedU32::saturating_mul_int]</code>.
2384 #[must_use = "this returns the result of the operation, without modifying the original"]
2385 fn saturating_mul_int(self, rhs: Self::Bits) -> Self;
2386
2387 /// Saturating division by an integer. Returns the quotient, saturating on overflow.
2388 ///
2389 /// Overflow can only occur when dividing the minimum value by −1.
2390 ///
2391 /// See also
2392 /// <code>FixedI32::[saturating\_div\_int][FixedI32::saturating_div_int]</code>
2393 /// and
2394 /// <code>FixedU32::[saturating\_div\_int][FixedU32::saturating_div_int]</code>.
2395 ///
2396 /// # Panics
2397 ///
2398 /// Panics if the divisor is zero.
2399 #[must_use = "this returns the result of the operation, without modifying the original"]
2400 fn saturating_div_int(self, rhs: Self::Bits) -> Self;
2401
2402 /// Saturating Euclidean division by an integer. Returns the
2403 /// quotient, saturating on overflow.
2404 ///
2405 /// See also
2406 /// <code>FixedI32::[saturating\_div\_euclid\_int][FixedI32::saturating_div_euclid_int]</code>
2407 /// and
2408 /// <code>FixedU32::[saturating\_div\_euclid\_int][FixedU32::saturating_div_euclid_int]</code>.
2409 ///
2410 /// # Panics
2411 ///
2412 /// Panics if the divisor is zero.
2413 #[must_use = "this returns the result of the operation, without modifying the original"]
2414 fn saturating_div_euclid_int(self, rhs: Self::Bits) -> Self;
2415
2416 /// Saturating remainder for Euclidean division by an integer.
2417 /// Returns the remainder, saturating on overflow.
2418 ///
2419 /// See also
2420 /// <code>FixedI32::[saturating\_rem\_euclid\_int][FixedI32::saturating_rem_euclid_int]</code>
2421 /// and
2422 /// <code>FixedU32::[saturating\_rem\_euclid\_int][FixedU32::saturating_rem_euclid_int]</code>.
2423 ///
2424 /// # Panics
2425 ///
2426 /// Panics if the divisor is zero.
2427 #[must_use = "this returns the result of the operation, without modifying the original"]
2428 fn saturating_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2429
2430 /// Saturating distance. Returns the distance from `self` to `other`,
2431 /// saturating on overflow.
2432 ///
2433 /// See also
2434 /// <code>FixedI32::[saturating\_dist][FixedI32::saturating_dist]</code> and
2435 /// <code>FixedU32::[saturating\_dist][FixedU32::saturating_dist]</code>.
2436 #[must_use = "this returns the result of the operation, without modifying the original"]
2437 fn saturating_dist(self, other: Self) -> Self;
2438
2439 /// Compute the hypotenuse of a right triange, saturating on overflow.
2440 ///
2441 /// See also
2442 /// <code>FixedI32::[saturating\_hypot][FixedI32::saturating_hypot]</code>
2443 /// and
2444 /// <code>FixedU32::[saturating\_hypot][FixedU32::saturating_hypot]</code>.
2445 #[must_use = "this returns the result of the operation, without modifying the original"]
2446 fn saturating_hypot(self, other: Self) -> Self;
2447
2448 /// Returns the square root, saturating on overflow.
2449 ///
2450 /// See also
2451 /// <code>FixedI32::[saturating\_sqrt][FixedI32::saturating_sqrt]</code> and
2452 /// <code>FixedU32::[saturating\_sqrt][FixedU32::saturating_sqrt]</code>.
2453 ///
2454 /// # Panics
2455 ///
2456 /// Panics if the number is negative.
2457 fn saturating_sqrt(self) -> Self;
2458
2459 /// Linear interpolation between `start` and `end`, saturating on overflow.
2460 ///
2461 /// See also
2462 /// <code>FixedI32::[saturating\_lerp][FixedI32::saturating_lerp]</code> and
2463 /// <code>FixedU32::[saturating\_lerp][FixedU32::saturating_lerp]</code>.
2464 #[must_use]
2465 fn saturating_lerp(self, start: Self, end: Self) -> Self;
2466
2467 /// Inverse linear interpolation between `start` and `end`, saturating on overflow.
2468 ///
2469 /// See also
2470 /// <code>FixedI32::[saturating\_inv\_lerp][FixedI32::saturating_inv_lerp]</code>
2471 /// and
2472 /// <code>FixedU32::[saturating\_inv\_lerp][FixedU32::saturating_inv_lerp]</code>.
2473 #[must_use]
2474 fn saturating_inv_lerp(self, start: Self, end: Self) -> Self;
2475
2476 /// Wrapping negation. Returns the negated value, wrapping on overflow.
2477 ///
2478 /// See also <code>FixedI32::[wrapping\_neg][FixedI32::wrapping_neg]</code>
2479 /// and <code>FixedU32::[wrapping\_neg][FixedU32::wrapping_neg]</code>.
2480 #[must_use]
2481 fn wrapping_neg(self) -> Self;
2482
2483 /// Wrapping addition. Returns the sum, wrapping on overflow.
2484 ///
2485 /// See also <code>FixedI32::[wrapping\_add][FixedI32::wrapping_add]</code>
2486 /// and <code>FixedU32::[wrapping\_add][FixedU32::wrapping_add]</code>.
2487 #[must_use = "this returns the result of the operation, without modifying the original"]
2488 fn wrapping_add(self, rhs: Self) -> Self;
2489
2490 /// Wrapping subtraction. Returns the difference, wrapping on overflow.
2491 ///
2492 /// See also <code>FixedI32::[wrapping\_sub][FixedI32::wrapping_sub]</code>
2493 /// and <code>FixedU32::[wrapping\_sub][FixedU32::wrapping_sub]</code>.
2494 #[must_use = "this returns the result of the operation, without modifying the original"]
2495 fn wrapping_sub(self, rhs: Self) -> Self;
2496
2497 /// Wrapping multiplication. Returns the product, wrapping on overflow.
2498 ///
2499 /// See also <code>FixedI32::[wrapping\_mul][FixedI32::wrapping_mul]</code>
2500 /// and <code>FixedU32::[wrapping\_mul][FixedU32::wrapping_mul]</code>.
2501 #[must_use = "this returns the result of the operation, without modifying the original"]
2502 fn wrapping_mul(self, rhs: Self) -> Self;
2503
2504 /// Wrapping division. Returns the quotient, wrapping on overflow.
2505 ///
2506 /// See also <code>FixedI32::[wrapping\_div][FixedI32::wrapping_div]</code>
2507 /// and <code>FixedU32::[wrapping\_div][FixedU32::wrapping_div]</code>.
2508 ///
2509 /// # Panics
2510 ///
2511 /// Panics if the divisor is zero.
2512 #[must_use = "this returns the result of the operation, without modifying the original"]
2513 fn wrapping_div(self, rhs: Self) -> Self;
2514
2515 /// Wrapping reciprocal.
2516 ///
2517 /// See also
2518 /// <code>FixedI32::[wrapping\_recip][FixedI32::wrapping_recip]</code> and
2519 /// <code>FixedU32::[wrapping\_recip][FixedU32::wrapping_recip]</code>.
2520 ///
2521 /// # Panics
2522 ///
2523 /// Panics if `self` is zero.
2524 #[must_use]
2525 fn wrapping_recip(self) -> Self;
2526
2527 /// Wrapping next multiple of `other`.
2528 ///
2529 /// See also
2530 /// <code>FixedI32::[wrapping\_next\_multiple\_of][FixedI32::wrapping_next_multiple_of]</code>
2531 /// and
2532 /// <code>FixedU32::[wrapping\_next\_multiple\_of][FixedU32::wrapping_next_multiple_of]</code>.
2533 ///
2534 /// # Panics
2535 ///
2536 /// Panics if `other` is zero.
2537 #[must_use]
2538 fn wrapping_next_multiple_of(self, other: Self) -> Self;
2539
2540 /// Wrapping multiply and add. Returns `self` × `mul` + `add`, wrapping on overflow.
2541 ///
2542 /// See also
2543 /// <code>FixedI32::[wrapping\_mul\_add][FixedI32::wrapping_mul_add]</code>
2544 /// and
2545 /// <code>FixedU32::[wrapping\_mul\_add][FixedU32::wrapping_mul_add]</code>.
2546 #[must_use = "this returns the result of the operation, without modifying the original"]
2547 fn wrapping_mul_add(self, mul: Self, add: Self) -> Self;
2548
2549 /// Adds `self` to the product `a` × `b`, wrapping on overflow.
2550 ///
2551 /// See also
2552 /// <code>FixedI32::[wrapping\_add\_prod][FixedI32::wrapping_add_prod]</code>
2553 /// and
2554 /// <code>FixedU32::[wrapping\_add\_prod][FixedU32::wrapping_add_prod]</code>.
2555 #[must_use]
2556 fn wrapping_add_prod(self, a: Self, b: Self) -> Self;
2557
2558 /// Wrapping multiply and accumulate. Adds (`a` × `b`) to `self`, wrapping on overflow.
2559 ///
2560 /// See also
2561 /// <code>FixedI32::[wrapping\_mul\_acc][FixedI32::wrapping_mul_acc]</code>
2562 /// and
2563 /// <code>FixedU32::[wrapping\_mul\_acc][FixedU32::wrapping_mul_acc]</code>.
2564 fn wrapping_mul_acc(&mut self, a: Self, b: Self);
2565
2566 /// Wrapping Euclidean division. Returns the quotient, wrapping on overflow.
2567 ///
2568 /// See also
2569 /// <code>FixedI32::[wrapping\_div\_euclid][FixedI32::wrapping_div_euclid]</code>
2570 /// and
2571 /// <code>FixedU32::[wrapping\_div\_euclid][FixedU32::wrapping_div_euclid]</code>.
2572 ///
2573 /// # Panics
2574 ///
2575 /// Panics if the divisor is zero.
2576 #[must_use = "this returns the result of the operation, without modifying the original"]
2577 fn wrapping_div_euclid(self, rhs: Self) -> Self;
2578
2579 /// Wrapping multiplication by an integer. Returns the product, wrapping on overflow.
2580 ///
2581 /// See also
2582 /// <code>FixedI32::[wrapping\_mul\_int][FixedI32::wrapping_mul_int]</code>
2583 /// and
2584 /// <code>FixedU32::[wrapping\_mul\_int][FixedU32::wrapping_mul_int]</code>.
2585 #[must_use = "this returns the result of the operation, without modifying the original"]
2586 fn wrapping_mul_int(self, rhs: Self::Bits) -> Self;
2587
2588 /// Wrapping division by an integer. Returns the quotient, wrapping on overflow.
2589 ///
2590 /// Overflow can only occur when dividing the minimum value by −1.
2591 ///
2592 /// See also
2593 /// <code>FixedI32::[wrapping\_div\_int][FixedI32::wrapping_div_int]</code>
2594 /// and
2595 /// <code>FixedU32::[wrapping\_div\_int][FixedU32::wrapping_div_int]</code>.
2596 ///
2597 /// # Panics
2598 ///
2599 /// Panics if the divisor is zero.
2600 #[must_use = "this returns the result of the operation, without modifying the original"]
2601 fn wrapping_div_int(self, rhs: Self::Bits) -> Self;
2602
2603 /// Wrapping Euclidean division by an integer. Returns the
2604 /// quotient, wrapping on overflow.
2605 ///
2606 /// Overflow can only occur when dividing the minimum value by −1.
2607 ///
2608 /// See also
2609 /// <code>FixedI32::[wrapping\_div\_euclid\_int][FixedI32::wrapping_div_euclid_int]</code>
2610 /// and
2611 /// <code>FixedU32::[wrapping\_div\_euclid\_int][FixedU32::wrapping_div_euclid_int]</code>.
2612 ///
2613 /// # Panics
2614 ///
2615 /// Panics if the divisor is zero.
2616 #[must_use = "this returns the result of the operation, without modifying the original"]
2617 fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self;
2618
2619 /// Wrapping remainder for Euclidean division by an integer.
2620 /// Returns the remainder, wrapping on overflow.
2621 ///
2622 /// See also
2623 /// <code>FixedI32::[wrapping\_rem\_euclid\_int][FixedI32::wrapping_rem_euclid_int]</code>
2624 /// and
2625 /// <code>FixedU32::[wrapping\_rem\_euclid\_int][FixedU32::wrapping_rem_euclid_int]</code>.
2626 ///
2627 /// # Panics
2628 ///
2629 /// Panics if the divisor is zero.
2630 #[must_use = "this returns the result of the operation, without modifying the original"]
2631 fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2632
2633 /// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of
2634 /// bits, then shifts and returns the number.
2635 ///
2636 /// See also <code>FixedI32::[wrapping\_shl][FixedI32::wrapping_shl]</code>
2637 /// and <code>FixedU32::[wrapping\_shl][FixedU32::wrapping_shl]</code>.
2638 #[must_use = "this returns the result of the operation, without modifying the original"]
2639 fn wrapping_shl(self, rhs: u32) -> Self;
2640
2641 /// Wrapping shift right. Wraps `rhs` if `rhs` ≥ the number of
2642 /// bits, then shifts and returns the number.
2643 ///
2644 /// See also <code>FixedI32::[wrapping\_shr][FixedI32::wrapping_shr]</code>
2645 /// and <code>FixedU32::[wrapping\_shr][FixedU32::wrapping_shr]</code>.
2646 #[must_use = "this returns the result of the operation, without modifying the original"]
2647 fn wrapping_shr(self, rhs: u32) -> Self;
2648
2649 /// Wrapping distance. Returns the distance from `self` to `other`, wrapping
2650 /// on overflow.
2651 ///
2652 /// See also
2653 /// <code>FixedI32::[wrapping\_dist][FixedI32::wrapping_dist]</code> and
2654 /// <code>FixedU32::[wrapping\_dist][FixedU32::wrapping_dist]</code>.
2655 #[must_use = "this returns the result of the operation, without modifying the original"]
2656 fn wrapping_dist(self, other: Self) -> Self;
2657
2658 /// Compute the hypotenuse of a right triange, wrapping on overflow.
2659 ///
2660 /// See also
2661 /// <code>FixedI32::[wrapping\_hypot][FixedI32::wrapping_hypot]</code> and
2662 /// <code>FixedU32::[wrapping\_hypot][FixedU32::wrapping_hypot]</code>.
2663 #[must_use = "this returns the result of the operation, without modifying the original"]
2664 fn wrapping_hypot(self, other: Self) -> Self;
2665
2666 /// Returns the square root, wrapping on overflow.
2667 ///
2668 /// See also
2669 /// <code>FixedI32::[wrapping\_sqrt][FixedI32::wrapping_sqrt]</code> and
2670 /// <code>FixedU32::[wrapping\_sqrt][FixedU32::wrapping_sqrt]</code>.
2671 ///
2672 /// # Panics
2673 ///
2674 /// Panics if the number is negative.
2675 fn wrapping_sqrt(self) -> Self;
2676
2677 /// Linear interpolation between `start` and `end`, wrapping on overflow.
2678 ///
2679 /// See also
2680 /// <code>FixedI32::[wrapping\_lerp][FixedI32::wrapping_lerp]</code> and
2681 /// <code>FixedU32::[wrapping\_lerp][FixedU32::wrapping_lerp]</code>.
2682 #[must_use]
2683 fn wrapping_lerp(self, start: Self, end: Self) -> Self;
2684
2685 /// Inverse linear interpolation between `start` and `end`, wrapping on
2686 /// overflow.
2687 ///
2688 /// See also
2689 /// <code>FixedI32::[wrapping\_inv\_lerp][FixedI32::wrapping_inv_lerp]</code>
2690 /// and
2691 /// <code>FixedU32::[wrapping\_inv\_lerp][FixedU32::wrapping_inv_lerp]</code>.
2692 #[must_use]
2693 fn wrapping_inv_lerp(self, start: Self, end: Self) -> Self;
2694
2695 /// Unwrapped negation. Returns the negated value, panicking on overflow.
2696 ///
2697 /// See also
2698 /// <code>FixedI32::[unwrapped\_neg][FixedI32::unwrapped_neg]</code> and
2699 /// <code>FixedU32::[unwrapped\_neg][FixedU32::unwrapped_neg]</code>.
2700 ///
2701 /// # Panics
2702 ///
2703 /// Panics if the result does not fit.
2704 #[track_caller]
2705 #[must_use]
2706 fn unwrapped_neg(self) -> Self;
2707
2708 /// Unwrapped addition. Returns the sum, panicking on overflow.
2709 ///
2710 /// See also
2711 /// <code>FixedI32::[unwrapped\_add][FixedI32::unwrapped_add]</code> and
2712 /// <code>FixedU32::[unwrapped\_add][FixedU32::unwrapped_add]</code>.
2713 ///
2714 /// # Panics
2715 ///
2716 /// Panics if the result does not fit.
2717 #[track_caller]
2718 #[must_use = "this returns the result of the operation, without modifying the original"]
2719 fn unwrapped_add(self, rhs: Self) -> Self;
2720
2721 /// Unwrapped subtraction. Returns the difference, panicking on overflow.
2722 ///
2723 /// See also
2724 /// <code>FixedI32::[unwrapped\_sub][FixedI32::unwrapped_sub]</code> and
2725 /// <code>FixedU32::[unwrapped\_sub][FixedU32::unwrapped_sub]</code>.
2726 ///
2727 /// # Panics
2728 ///
2729 /// Panics if the result does not fit.
2730 #[track_caller]
2731 #[must_use = "this returns the result of the operation, without modifying the original"]
2732 fn unwrapped_sub(self, rhs: Self) -> Self;
2733
2734 /// Unwrapped multiplication. Returns the product, panicking on overflow.
2735 ///
2736 /// See also
2737 /// <code>FixedI32::[unwrapped\_mul][FixedI32::unwrapped_mul]</code> and
2738 /// <code>FixedU32::[unwrapped\_mul][FixedU32::unwrapped_mul]</code>.
2739 ///
2740 /// # Panics
2741 ///
2742 /// Panics if the result does not fit.
2743 #[track_caller]
2744 #[must_use = "this returns the result of the operation, without modifying the original"]
2745 fn unwrapped_mul(self, rhs: Self) -> Self;
2746
2747 /// Unwrapped division. Returns the quotient, panicking on overflow.
2748 ///
2749 /// See also
2750 /// <code>FixedI32::[unwrapped\_div][FixedI32::unwrapped_div]</code> and
2751 /// <code>FixedU32::[unwrapped\_div][FixedU32::unwrapped_div]</code>.
2752 ///
2753 /// # Panics
2754 ///
2755 /// Panics if the divisor is zero or if the result does not fit.
2756 #[track_caller]
2757 #[must_use = "this returns the result of the operation, without modifying the original"]
2758 fn unwrapped_div(self, rhs: Self) -> Self;
2759
2760 /// Unwrapped remainder. Returns the quotient, panicking if the divisor is zero.
2761 ///
2762 /// See also
2763 /// <code>FixedI32::[unwrapped\_rem][FixedI32::unwrapped_rem]</code> and
2764 /// <code>FixedU32::[unwrapped\_rem][FixedU32::unwrapped_rem]</code>.
2765 ///
2766 /// # Panics
2767 ///
2768 /// Panics if the divisor is zero.
2769 #[track_caller]
2770 #[must_use = "this returns the result of the operation, without modifying the original"]
2771 fn unwrapped_rem(self, rhs: Self) -> Self;
2772
2773 /// Unwrapped reciprocal. Returns reciprocal, panicking on overflow.
2774 ///
2775 /// See also
2776 /// <code>FixedI32::[unwrapped\_recip][FixedI32::unwrapped_recip]</code> and
2777 /// <code>FixedU32::[unwrapped\_recip][FixedU32::unwrapped_recip]</code>.
2778 ///
2779 /// # Panics
2780 ///
2781 /// Panics if `self` is zero or on overflow.
2782 #[track_caller]
2783 #[must_use]
2784 fn unwrapped_recip(self) -> Self;
2785
2786 /// Unwrapped next multiple of `other`. Returns the next multiple, panicking
2787 /// on overflow.
2788 ///
2789 /// See also
2790 /// <code>FixedI32::[unwrapped\_next\_multiple\_of][FixedI32::unwrapped_next_multiple_of]</code>
2791 /// and
2792 /// <code>FixedU32::[unwrapped\_next\_multiple\_of][FixedU32::unwrapped_next_multiple_of]</code>.
2793 ///
2794 /// # Panics
2795 ///
2796 /// Panics if `other` is zero or on overflow.
2797 #[track_caller]
2798 #[must_use]
2799 fn unwrapped_next_multiple_of(self, other: Self) -> Self;
2800
2801 /// Unwrapped multiply and add. Returns `self` × `mul` + `add`, panicking on overflow.
2802 ///
2803 /// See also
2804 /// <code>FixedI32::[unwrapped\_mul\_add][FixedI32::unwrapped_mul_add]</code>
2805 /// and
2806 /// <code>FixedU32::[unwrapped\_mul\_add][FixedU32::unwrapped_mul_add]</code>.
2807 ///
2808 /// # Panics
2809 ///
2810 /// Panics if the result does not fit.
2811 #[track_caller]
2812 #[must_use = "this returns the result of the operation, without modifying the original"]
2813 fn unwrapped_mul_add(self, mul: Self, add: Self) -> Self;
2814
2815 /// Adds `self` to the product `a` × `b`, panicking on overflow.
2816 ///
2817 /// See also
2818 /// <code>FixedI32::[unwrapped\_add\_prod][FixedI32::unwrapped_add_prod]</code>
2819 /// and
2820 /// <code>FixedU32::[unwrapped\_add\_prod][FixedU32::unwrapped_add_prod]</code>.
2821 ///
2822 /// # Panics
2823 ///
2824 /// Panics if the result does not fit.
2825 #[track_caller]
2826 #[must_use]
2827 fn unwrapped_add_prod(self, a: Self, b: Self) -> Self;
2828
2829 /// Unwrapped multiply and accumulate. Adds (`a` × `b`) to `self`, panicking on overflow.
2830 ///
2831 /// See also
2832 /// <code>FixedI32::[unwrapped\_mul\_acc][FixedI32::unwrapped_mul_acc]</code>
2833 /// and
2834 /// <code>FixedU32::[unwrapped\_mul\_acc][FixedU32::unwrapped_mul_acc]</code>.
2835 ///
2836 /// # Panics
2837 ///
2838 /// Panics if the result does not fit.
2839 #[track_caller]
2840 fn unwrapped_mul_acc(&mut self, a: Self, b: Self);
2841
2842 /// Unwrapped Euclidean division. Returns the quotient, panicking on overflow.
2843 ///
2844 /// See also
2845 /// <code>FixedI32::[unwrapped\_div\_euclid][FixedI32::unwrapped_div_euclid]</code>
2846 /// and
2847 /// <code>FixedU32::[unwrapped\_div\_euclid][FixedU32::unwrapped_div_euclid]</code>.
2848 ///
2849 /// # Panics
2850 ///
2851 /// Panics if the divisor is zero or if the result does not fit.
2852 #[track_caller]
2853 #[must_use = "this returns the result of the operation, without modifying the original"]
2854 fn unwrapped_div_euclid(self, rhs: Self) -> Self;
2855
2856 /// Unwrapped remainder for Euclidean division. Returns the
2857 /// remainder, panicking if the divisor is zero.
2858 ///
2859 /// See also
2860 /// <code>FixedI32::[unwrapped\_rem\_euclid][FixedI32::unwrapped_rem_euclid]</code>
2861 /// and
2862 /// <code>FixedU32::[unwrapped\_rem\_euclid][FixedU32::unwrapped_rem_euclid]</code>.
2863 ///
2864 /// # Panics
2865 ///
2866 /// Panics if the divisor is zero.
2867 #[track_caller]
2868 #[must_use = "this returns the result of the operation, without modifying the original"]
2869 fn unwrapped_rem_euclid(self, rhs: Self) -> Self;
2870
2871 /// Unwrapped multiplication by an integer. Returns the product, panicking on overflow.
2872 ///
2873 /// See also
2874 /// <code>FixedI32::[unwrapped\_mul\_int][FixedI32::unwrapped_mul_int]</code>
2875 /// and
2876 /// <code>FixedU32::[unwrapped\_mul\_int][FixedU32::unwrapped_mul_int]</code>.
2877 ///
2878 /// # Panics
2879 ///
2880 /// Panics if the result does not fit.
2881 #[track_caller]
2882 #[must_use = "this returns the result of the operation, without modifying the original"]
2883 fn unwrapped_mul_int(self, rhs: Self::Bits) -> Self;
2884
2885 /// Unwrapped division by an integer. Returns the quotient, panicking on overflow.
2886 ///
2887 /// Overflow can only occur when dividing the minimum value by −1.
2888 ///
2889 /// See also
2890 /// <code>FixedI32::[unwrapped\_div\_int][FixedI32::unwrapped_div_int]</code>
2891 /// and
2892 /// <code>FixedU32::[unwrapped\_div\_int][FixedU32::unwrapped_div_int]</code>.
2893 ///
2894 /// # Panics
2895 ///
2896 /// Panics if the divisor is zero or if the result does not fit.
2897 #[track_caller]
2898 #[must_use = "this returns the result of the operation, without modifying the original"]
2899 fn unwrapped_div_int(self, rhs: Self::Bits) -> Self;
2900
2901 /// Unwrapped remainder for division by an integer. Returns the
2902 /// remainder, panicking if the divisor is zero.
2903 ///
2904 /// See also
2905 /// <code>FixedI32::[unwrapped\_rem\_int][FixedI32::unwrapped_rem_int]</code>
2906 /// and
2907 /// <code>FixedU32::[unwrapped\_rem\_int][FixedU32::unwrapped_rem_int]</code>.
2908 ///
2909 /// # Panics
2910 ///
2911 /// Panics if the divisor is zero.
2912 #[track_caller]
2913 #[must_use = "this returns the result of the operation, without modifying the original"]
2914 fn unwrapped_rem_int(self, rhs: Self::Bits) -> Self;
2915
2916 /// Unwrapped Euclidean division by an integer. Returns the
2917 /// quotient, panicking on overflow.
2918 ///
2919 /// Overflow can only occur when dividing the minimum value by −1.
2920 ///
2921 /// See also
2922 /// <code>FixedI32::[unwrapped\_div\_euclid\_int][FixedI32::unwrapped_div_euclid_int]</code>
2923 /// and
2924 /// <code>FixedU32::[unwrapped\_div\_euclid\_int][FixedU32::unwrapped_div_euclid_int]</code>.
2925 ///
2926 /// # Panics
2927 ///
2928 /// Panics if the divisor is zero or if the result does not fit.
2929 #[track_caller]
2930 #[must_use = "this returns the result of the operation, without modifying the original"]
2931 fn unwrapped_div_euclid_int(self, rhs: Self::Bits) -> Self;
2932
2933 /// Unwrapped remainder for Euclidean division by an integer.
2934 /// Returns the remainder, panicking on overflow.
2935 ///
2936 /// See also
2937 /// <code>FixedI32::[unwrapped\_rem\_euclid\_int][FixedI32::unwrapped_rem_euclid_int]</code>
2938 /// and
2939 /// <code>FixedU32::[unwrapped\_rem\_euclid\_int][FixedU32::unwrapped_rem_euclid_int]</code>.
2940 ///
2941 /// # Panics
2942 ///
2943 /// Panics if the divisor is zero or if the result does not fit.
2944 #[track_caller]
2945 #[must_use = "this returns the result of the operation, without modifying the original"]
2946 fn unwrapped_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2947
2948 /// Unwrapped shift left. Panics if `rhs` ≥ the number of bits.
2949 ///
2950 /// See also
2951 /// <code>FixedI32::[unwrapped\_shl][FixedI32::unwrapped_shl]</code> and
2952 /// <code>FixedU32::[unwrapped\_shl][FixedU32::unwrapped_shl]</code>.
2953 ///
2954 /// # Panics
2955 ///
2956 /// Panics if `rhs` ≥ the number of bits.
2957 #[track_caller]
2958 #[must_use = "this returns the result of the operation, without modifying the original"]
2959 fn unwrapped_shl(self, rhs: u32) -> Self;
2960
2961 /// Unwrapped shift right. Panics if `rhs` ≥ the number of bits.
2962 ///
2963 /// See also
2964 /// <code>FixedI32::[unwrapped\_shr][FixedI32::unwrapped_shr]</code> and
2965 /// <code>FixedU32::[unwrapped\_shr][FixedU32::unwrapped_shr]</code>.
2966 ///
2967 /// # Panics
2968 ///
2969 /// Panics if `rhs` ≥ the number of bits.
2970 #[track_caller]
2971 #[must_use = "this returns the result of the operation, without modifying the original"]
2972 fn unwrapped_shr(self, rhs: u32) -> Self;
2973
2974 /// Unwrapped distance. Returns the distance from `self` to `other`,
2975 /// panicking on overflow.
2976 ///
2977 /// # Panics
2978 ///
2979 /// Panics if the result does not fit.
2980 ///
2981 /// See also
2982 /// <code>FixedI32::[unwrapped\_dist][FixedI32::unwrapped_dist]</code> and
2983 /// <code>FixedU32::[unwrapped\_dist][FixedU32::unwrapped_dist]</code>.
2984 #[track_caller]
2985 #[must_use = "this returns the result of the operation, without modifying the original"]
2986 fn unwrapped_dist(self, other: Self) -> Self;
2987
2988 /// Compute the hypotenuse of a right triange, panicking on overflow.
2989 ///
2990 /// # Panics
2991 ///
2992 /// Panics if the result does not fit.
2993 ///
2994 /// See also
2995 /// <code>FixedI32::[unwrapped\_hypot][FixedI32::unwrapped_hypot]</code> and
2996 /// <code>FixedU32::[unwrapped\_hypot][FixedU32::unwrapped_hypot]</code>.
2997 #[track_caller]
2998 #[must_use = "this returns the result of the operation, without modifying the original"]
2999 fn unwrapped_hypot(self, other: Self) -> Self;
3000
3001 /// Returns the square root, panicking if the number is negative or on overflow.
3002 ///
3003 /// See also
3004 /// <code>FixedI32::[unwrapped\_sqrt][FixedI32::unwrapped_sqrt]</code> and
3005 /// <code>FixedU32::[unwrapped\_sqrt][FixedU32::unwrapped_sqrt]</code>.
3006 ///
3007 /// # Panics
3008 ///
3009 /// Panics if the number is negative or on overflow.
3010 fn unwrapped_sqrt(self) -> Self;
3011
3012 /// Linear interpolation between `start` and `end`, panicking on overflow.
3013 ///
3014 /// # Panics
3015 ///
3016 /// Panics if the result does not fit.
3017 ///
3018 /// See also
3019 /// <code>FixedI32::[unwrapped\_lerp][FixedI32::unwrapped_lerp]</code> and
3020 /// <code>FixedU32::[unwrapped\_lerp][FixedU32::unwrapped_lerp]</code>.
3021 #[track_caller]
3022 #[must_use]
3023 fn unwrapped_lerp(self, start: Self, end: Self) -> Self;
3024
3025 /// Inverse linear interpolation between `start` and `end`, panicking on overflow.
3026 ///
3027 /// # Panics
3028 ///
3029 /// Panics when `start` = `end` or when the results overflows.
3030 ///
3031 /// See also
3032 /// <code>FixedI32::[unwrapped\_inv\_lerp][FixedI32::unwrapped_inv_lerp]</code>
3033 /// and
3034 /// <code>FixedU32::[unwrapped\_inv\_lerp][FixedU32::unwrapped_inv_lerp]</code>.
3035 #[track_caller]
3036 #[must_use]
3037 fn unwrapped_inv_lerp(self, start: Self, end: Self) -> Self;
3038
3039 /// Overflowing negation.
3040 ///
3041 /// Returns a [tuple] of the negated value and a [`bool`],
3042 /// indicating whether an overflow has occurred. On overflow, the
3043 /// wrapped value is returned.
3044 ///
3045 /// See also
3046 /// <code>FixedI32::[overflowing\_neg][FixedI32::overflowing_neg]</code> and
3047 /// <code>FixedU32::[overflowing\_neg][FixedU32::overflowing_neg]</code>.
3048 fn overflowing_neg(self) -> (Self, bool);
3049
3050 /// Overflowing addition.
3051 ///
3052 /// Returns a [tuple] of the sum and a [`bool`], indicating whether
3053 /// an overflow has occurred. On overflow, the wrapped value is
3054 /// returned.
3055 ///
3056 /// See also
3057 /// <code>FixedI32::[overflowing\_add][FixedI32::overflowing_add]</code> and
3058 /// <code>FixedU32::[overflowing\_add][FixedU32::overflowing_add]</code>.
3059 #[must_use = "this returns the result of the operation, without modifying the original"]
3060 fn overflowing_add(self, rhs: Self) -> (Self, bool);
3061
3062 /// Overflowing subtraction.
3063 ///
3064 /// Returns a [tuple] of the difference and a [`bool`], indicating
3065 /// whether an overflow has occurred. On overflow, the wrapped
3066 /// value is returned.
3067 ///
3068 /// See also
3069 /// <code>FixedI32::[overflowing\_sub][FixedI32::overflowing_sub]</code> and
3070 /// <code>FixedU32::[overflowing\_sub][FixedU32::overflowing_sub]</code>.
3071 #[must_use = "this returns the result of the operation, without modifying the original"]
3072 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
3073
3074 /// Overflowing multiplication.
3075 ///
3076 /// Returns a [tuple] of the product and a [`bool`], indicating
3077 /// whether an overflow has occurred. On overflow, the wrapped
3078 /// value is returned.
3079 ///
3080 /// See also
3081 /// <code>FixedI32::[overflowing\_mul][FixedI32::overflowing_mul]</code> and
3082 /// <code>FixedU32::[overflowing\_mul][FixedU32::overflowing_mul]</code>.
3083 #[must_use = "this returns the result of the operation, without modifying the original"]
3084 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
3085
3086 /// Overflowing division.
3087 ///
3088 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3089 /// whether an overflow has occurred. On overflow, the wrapped
3090 /// value is returned.
3091 ///
3092 /// See also
3093 /// <code>FixedI32::[overflowing\_div][FixedI32::overflowing_div]</code> and
3094 /// <code>FixedU32::[overflowing\_div][FixedU32::overflowing_div]</code>.
3095 ///
3096 /// # Panics
3097 ///
3098 /// Panics if the divisor is zero.
3099 #[must_use = "this returns the result of the operation, without modifying the original"]
3100 fn overflowing_div(self, rhs: Self) -> (Self, bool);
3101
3102 /// Overflowing reciprocal.
3103 ///
3104 /// Returns a [tuple] of the reciprocal of `self` and a [`bool`],
3105 /// indicating whether an overflow has occurred. On overflow, the
3106 /// wrapped value is returned.
3107 ///
3108 /// See also
3109 /// <code>FixedI32::[overflowing\_recip][FixedI32::overflowing_recip]</code>
3110 /// and
3111 /// <code>FixedU32::[overflowing\_recip][FixedU32::overflowing_recip]</code>.
3112 ///
3113 /// # Panics
3114 ///
3115 /// Panics if `self` is zero.
3116 fn overflowing_recip(self) -> (Self, bool);
3117
3118 /// Overflowing next multiple of `other`.
3119 ///
3120 /// Returns a [tuple] of the next multiple and a [`bool`], indicating
3121 /// whether an overflow has occurred. On overflow, the wrapped value is
3122 /// returned.
3123 ///
3124 /// See also
3125 /// <code>FixedI32::[overflowing\_next\_multiple\_of][FixedI32::overflowing_next_multiple_of]</code>
3126 /// and
3127 /// <code>FixedU32::[overflowing\_next\_multiple\_of][FixedU32::overflowing_next_multiple_of]</code>.
3128 ///
3129 /// # Panics
3130 ///
3131 /// Panics if `other` is zero.
3132 fn overflowing_next_multiple_of(self, other: Self) -> (Self, bool);
3133
3134 /// Overflowing multiply and add.
3135 ///
3136 /// Returns a [tuple] of `self` × `mul` + `add` and a [`bool`],
3137 /// indicating whether an overflow has occurred. On overflow, the
3138 /// wrapped value is returned.
3139 ///
3140 /// See also
3141 /// <code>FixedI32::[overflowing\_mul\_add][FixedI32::overflowing_mul_add]</code>
3142 /// and
3143 /// <code>FixedU32::[overflowing\_mul\_add][FixedU32::overflowing_mul_add]</code>.
3144 #[must_use = "this returns the result of the operation, without modifying the original"]
3145 fn overflowing_mul_add(self, mul: Self, add: Self) -> (Self, bool);
3146
3147 /// Adds `self` to the product `a` × `b`.
3148 ///
3149 /// Returns a [tuple] of the result and a [`bool`] indicating whether an
3150 /// overflow has occurred. On overflow, the wrapped value is returned.
3151 ///
3152 /// See also
3153 /// <code>FixedI32::[overflowing\_add\_prod][FixedI32::overflowing_add_prod]</code>
3154 /// and
3155 /// <code>FixedU32::[overflowing\_add\_prod][FixedU32::overflowing_add_prod]</code>.
3156 #[must_use = "this returns the result of the operation, without modifying the original"]
3157 fn overflowing_add_prod(self, a: Self, b: Self) -> (Self, bool);
3158
3159 /// Overflowing multiply and accumulate. Adds (`a` × `b`) to `self`,
3160 /// wrapping and returning [`true`] if overflow occurs.
3161 ///
3162 /// See also
3163 /// <code>FixedI32::[overflowing\_mul\_acc][FixedI32::overflowing_mul_acc]</code>
3164 /// and
3165 /// <code>FixedU32::[overflowing\_mul\_acc][FixedU32::overflowing_mul_acc]</code>.
3166 #[must_use = "this returns whether overflow occurs; use `wrapping_mul_acc` if the flag is not needed"]
3167 fn overflowing_mul_acc(&mut self, a: Self, b: Self) -> bool;
3168
3169 /// Overflowing Euclidean division.
3170 ///
3171 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3172 /// whether an overflow has occurred. On overflow, the wrapped
3173 /// value is returned.
3174 ///
3175 /// See also
3176 /// <code>FixedI32::[overflowing\_div\_euclid][FixedI32::overflowing_div_euclid]</code>
3177 /// and
3178 /// <code>FixedU32::[overflowing\_div\_euclid][FixedU32::overflowing_div_euclid]</code>.
3179 ///
3180 /// # Panics
3181 ///
3182 /// Panics if the divisor is zero.
3183 #[must_use = "this returns the result of the operation, without modifying the original"]
3184 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
3185
3186 /// Overflowing multiplication by an integer.
3187 ///
3188 /// Returns a [tuple] of the product and a [`bool`], indicating
3189 /// whether an overflow has occurred. On overflow, the wrapped
3190 /// value is returned.
3191 ///
3192 /// See also
3193 /// <code>FixedI32::[overflowing\_mul\_int][FixedI32::overflowing_mul_int]</code>
3194 /// and
3195 /// <code>FixedU32::[overflowing\_mul\_int][FixedU32::overflowing_mul_int]</code>.
3196 #[must_use = "this returns the result of the operation, without modifying the original"]
3197 fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool);
3198
3199 /// Overflowing division by an integer.
3200 ///
3201 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3202 /// whether an overflow has occurred. On overflow, the wrapped
3203 /// value is returned.
3204 ///
3205 /// See also
3206 /// <code>FixedI32::[overflowing\_div\_int][FixedI32::overflowing_div_int]</code>
3207 /// and
3208 /// <code>FixedU32::[overflowing\_div\_int][FixedU32::overflowing_div_int]</code>.
3209 ///
3210 /// # Panics
3211 ///
3212 /// Panics if the divisor is zero.
3213 #[must_use = "this returns the result of the operation, without modifying the original"]
3214 fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool);
3215
3216 /// Overflowing Euclidean division by an integer.
3217 ///
3218 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3219 /// whether an overflow has occurred. On overflow, the wrapped
3220 /// value is returned.
3221 ///
3222 /// See also
3223 /// <code>FixedI32::[overflowing\_div\_euclid\_int][FixedI32::overflowing_div_euclid_int]</code>
3224 /// and
3225 /// <code>FixedU32::[overflowing\_div\_euclid\_int][FixedU32::overflowing_div_euclid_int]</code>.
3226 ///
3227 /// # Panics
3228 ///
3229 /// Panics if the divisor is zero.
3230 #[must_use = "this returns the result of the operation, without modifying the original"]
3231 fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
3232
3233 /// Overflowing remainder for Euclidean division by an integer.
3234 ///
3235 /// Returns a [tuple] of the remainder and a [`bool`], indicating
3236 /// whether an overflow has occurred. On overflow, the wrapped
3237 /// value is returned.
3238 ///
3239 /// See also
3240 /// <code>FixedI32::[overflowing\_rem\_euclid\_int][FixedI32::overflowing_rem_euclid_int]</code>
3241 /// and
3242 /// <code>FixedU32::[overflowing\_rem\_euclid\_int][FixedU32::overflowing_rem_euclid_int]</code>.
3243 ///
3244 /// # Panics
3245 ///
3246 /// Panics if the divisor is zero.
3247 #[must_use = "this returns the result of the operation, without modifying the original"]
3248 fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
3249
3250 /// Overflowing shift left.
3251 ///
3252 /// Returns a [tuple] of the shifted value and a [`bool`],
3253 /// indicating whether an overflow has occurred. On overflow, the
3254 /// wrapped value is returned.
3255 ///
3256 /// See also
3257 /// <code>FixedI32::[overflowing\_shl][FixedI32::overflowing_shl]</code> and
3258 /// <code>FixedU32::[overflowing\_shl][FixedU32::overflowing_shl]</code>.
3259 #[must_use = "this returns the result of the operation, without modifying the original"]
3260 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
3261
3262 /// Overflowing shift right.
3263 ///
3264 /// Returns a [tuple] of the shifted value and a [`bool`],
3265 /// indicating whether an overflow has occurred. On overflow, the
3266 /// wrapped value is returned.
3267 ///
3268 /// See also
3269 /// <code>FixedI32::[overflowing\_shr][FixedI32::overflowing_shr]</code> and
3270 /// <code>FixedU32::[overflowing\_shr][FixedU32::overflowing_shr]</code>.
3271 #[must_use = "this returns the result of the operation, without modifying the original"]
3272 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
3273
3274 /// Overflowing distance.
3275 ///
3276 /// Returns a [tuple] of the distance from `self` to `other` and a [`bool`],
3277 /// indicating whether an overflow has occurred. On overflow, the wrapped
3278 /// value is returned.
3279 ///
3280 /// See also
3281 /// <code>FixedI32::[overflowing\_dist][FixedI32::overflowing_dist]</code>
3282 /// and
3283 /// <code>FixedU32::[overflowing\_dist][FixedU32::overflowing_dist]</code>.
3284 #[must_use = "this returns the result of the operation, without modifying the original"]
3285 fn overflowing_dist(self, other: Self) -> (Self, bool);
3286
3287 /// Compute the hypotenuse of a right triange.
3288 ///
3289 /// Returns a [tuple] of the hypotenuse and a [`bool`], indicating whether
3290 /// an overflow has occurred. On overflow, the wrapped value is returned.
3291 ///
3292 /// See also
3293 /// <code>FixedI32::[overflowing\_hypot][FixedI32::overflowing_hypot]</code>
3294 /// and
3295 /// <code>FixedU32::[overflowing\_hypot][FixedU32::overflowing_hypot]</code>.
3296 #[must_use = "this returns the result of the operation, without modifying the original"]
3297 fn overflowing_hypot(self, other: Self) -> (Self, bool);
3298
3299 /// Compute the square root.
3300 ///
3301 /// Returns a [tuple] of the square root and a [`bool`], indicating whether
3302 /// an overflow has occurred. On overflow, the wrapped value is returned.
3303 ///
3304 /// See also
3305 /// <code>FixedI32::[overflowing\_sqrt][FixedI32::overflowing_sqrt]</code>
3306 /// and
3307 /// <code>FixedU32::[overflowing\_sqrt][FixedU32::overflowing_sqrt]</code>.
3308 ///
3309 /// # Panics
3310 ///
3311 /// Panics if the number is negative.
3312 fn overflowing_sqrt(self) -> (Self, bool);
3313
3314 /// Overflowing linear interpolation between `start` and `end`.
3315 ///
3316 /// Returns a [tuple] of the interpolated value and a [`bool`], indicating
3317 /// whether an overflow has occurred. On overflow, the wrapped value is
3318 /// returned.
3319 ///
3320 /// See also
3321 /// <code>FixedI32::[overflowing\_lerp][FixedI32::overflowing_lerp]</code>
3322 /// and
3323 /// <code>FixedU32::[overflowing\_lerp][FixedU32::overflowing_lerp]</code>.
3324 fn overflowing_lerp(self, start: Self, end: Self) -> (Self, bool);
3325
3326 /// Overflowing inverse linear interpolation between `start` and `end`.
3327 ///
3328 /// Returns a [tuple] of the computed value and a [`bool`], indicating
3329 /// whether an overflow has occurred. On overflow, the wrapped value is
3330 /// returned.
3331 ///
3332 /// See also
3333 /// <code>FixedI32::[overflowing\_inv\_lerp][FixedI32::overflowing_inv_lerp]</code>
3334 /// and
3335 /// <code>FixedU32::[overflowing\_inv\_lerp][FixedU32::overflowing_inv_lerp]</code>.
3336 fn overflowing_inv_lerp(self, start: Self, end: Self) -> (Self, bool);
3337
3338 /// Unchecked addition. Computes `self` + `rhs`, assuming
3339 /// overflow cannot occur.
3340 ///
3341 /// See also
3342 /// <code>FixedI32::[unchecked\_add][FixedI32::unchecked_add]</code> and
3343 /// <code>FixedU32::[unchecked\_add][FixedU32::unchecked_add]</code>.
3344 ///
3345 /// # Safety
3346 ///
3347 /// This results in undefined behavior when
3348 /// `self` + `rhs` \> [`MAX`][Self::MAX] or
3349 /// `self` + `rhs` \< [`MIN`][Self::MIN].
3350 unsafe fn unchecked_add(self, rhs: Self) -> Self;
3351
3352 /// Unchecked subtraction. Computes `self` − `rhs`, assuming
3353 /// overflow cannot occur.
3354 ///
3355 /// See also
3356 /// <code>FixedI32::[unchecked\_sub][FixedI32::unchecked_sub]</code> and
3357 /// <code>FixedU32::[unchecked\_sub][FixedU32::unchecked_sub]</code>.
3358 ///
3359 /// # Safety
3360 ///
3361 /// This results in undefined behavior when
3362 /// `self` − `rhs` \> [`MAX`][Self::MAX] or
3363 /// `self` − `rhs` \< [`MIN`][Self::MIN].
3364 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
3365
3366 /// Unchecked multiplication by an integer. Computes
3367 /// `self` × `rhs`, assuming overflow cannot occur.
3368 ///
3369 /// See also
3370 /// <code>FixedI32::[unchecked\_mul\_int][FixedI32::unchecked_mul_int]</code>
3371 /// and
3372 /// <code>FixedU32::[unchecked\_mul\_int][FixedU32::unchecked_mul_int]</code>.
3373 ///
3374 /// # Safety
3375 ///
3376 /// This results in undefined behavior when
3377 /// `self` × `rhs` \> [`MAX`][Self::MAX] or
3378 /// `self` × `rhs` \< [`MIN`][Self::MIN].
3379 unsafe fn unchecked_mul_int(self, rhs: Self::Bits) -> Self;
3380
3381 /// Rounds to the nearest integer, with ties rounded to even.
3382 #[must_use]
3383 #[deprecated(since = "1.28.0", note = "renamed to `round_ties_even`")]
3384 fn round_ties_to_even(self) -> Self {
3385 self.round_ties_even()
3386 }
3387
3388 /// Checked round. Rounds to the nearest integer, with ties rounded to even,
3389 /// returning [`None`] on overflow.
3390 #[deprecated(since = "1.28.0", note = "renamed to `checked_round_ties_even`")]
3391 fn checked_round_ties_to_even(self) -> Option<Self> {
3392 self.checked_round_ties_even()
3393 }
3394
3395 /// Saturating round. Rounds to the nearest integer, with ties rounded
3396 /// to_even, and saturating on overflow.
3397 #[must_use]
3398 #[deprecated(since = "1.28.0", note = "renamed to `saturating_round_ties_even`")]
3399 fn saturating_round_ties_to_even(self) -> Self {
3400 self.saturating_round_ties_even()
3401 }
3402
3403 /// Wrapping round. Rounds to the next integer to the nearest, with ties
3404 /// rounded to even, and wrapping on overflow.
3405 #[must_use]
3406 #[deprecated(since = "1.28.0", note = "renamed to `wrapping_round_ties_even`")]
3407 fn wrapping_round_ties_to_even(self) -> Self {
3408 self.wrapping_round_ties_even()
3409 }
3410
3411 /// Unwrapped round. Rounds to the next integer to the nearest, with ties
3412 /// rounded to even, and panicking on overflow.
3413 ///
3414 /// # Panics
3415 ///
3416 /// Panics if the result does not fit.
3417 #[track_caller]
3418 #[must_use]
3419 #[deprecated(since = "1.28.0", note = "renamed to `unwrapped_round_ties_even`")]
3420 fn unwrapped_round_ties_to_even(self) -> Self {
3421 self.unwrapped_round_ties_even()
3422 }
3423
3424 /// Overflowing round. Rounds to the next integer to the nearest, with ties
3425 /// rounded to even.
3426 ///
3427 /// Returns a [tuple] of the fixed-point number and a [`bool`], indicating
3428 /// whether an overflow has occurred. On overflow, the wrapped value is
3429 /// returned.
3430 #[deprecated(since = "1.28.0", note = "renamed to `overflowing_round_ties_even`")]
3431 fn overflowing_round_ties_to_even(self) -> (Self, bool) {
3432 self.overflowing_round_ties_even()
3433 }
3434}
3435
3436/// This trait provides methods common to all signed fixed-point numbers.
3437///
3438/// Methods common to all fixed-point numbers including unsigned
3439/// fixed-point numbers are provided by the [`Fixed`] supertrait.
3440///
3441/// This trait is sealed and cannot be implemented for more types; it
3442/// is implemented for [`FixedI8`], [`FixedI16`], [`FixedI32`],
3443/// [`FixedI64`], and [`FixedI128`].
3444pub trait FixedSigned: Fixed
3445where
3446 Self: Neg<Output = Self>,
3447{
3448 /// Negative one if the fixed-point number can represent it, otherwise
3449 /// [`None`].
3450 const TRY_NEG_ONE: Option<Self>;
3451
3452 /// Returns the number of bits required to represent the value.
3453 ///
3454 /// See also <code>FixedI32::[signed\_bits][FixedI32::signed_bits]</code>.
3455 fn signed_bits(self) -> u32;
3456
3457 /// Returns [`true`] if the number is > 0.
3458 ///
3459 /// See also <code>FixedI32::[is\_positive][FixedI32::is_positive]</code>.
3460 fn is_positive(self) -> bool;
3461
3462 /// Returns [`true`] if the number is < 0.
3463 ///
3464 /// See also <code>FixedI32::[is\_negative][FixedI32::is_negative]</code>.
3465 fn is_negative(self) -> bool;
3466
3467 /// Returns the absolute value.
3468 ///
3469 /// See also <code>FixedI32::[abs][FixedI32::abs]</code>.
3470 #[must_use]
3471 fn abs(self) -> Self;
3472
3473 /// Returns the absolute value using an unsigned type without any
3474 /// wrapping or panicking.
3475 ///
3476 /// See also <code>FixedI32::[unsigned\_abs][FixedI32::unsigned_abs]</code>.
3477 fn unsigned_abs(self) -> Self::Unsigned;
3478
3479 /// Returns the distance from `self` to `other` using an unsigned type
3480 /// without any wrapping or panicking.
3481 ///
3482 /// See also
3483 /// <code>FixedI32::[unsigned\_dist][FixedI32::unsigned_dist]</code>.
3484 fn unsigned_dist(self, other: Self) -> Self::Unsigned;
3485
3486 /// Returns a number representing the sign of `self`.
3487 ///
3488 /// See also <code>FixedI32::[signum][FixedI32::signum]</code>.
3489 ///
3490 /// # Panics
3491 ///
3492 /// When debug assertions are enabled, this method panics
3493 /// * if the value is positive and the fixed-point number has
3494 /// zero or one integer bits such that it cannot hold the
3495 /// value 1.
3496 /// * if the value is negative and the fixed-point number has
3497 /// zero integer bits, such that it cannot hold the value −1.
3498 ///
3499 /// When debug assertions are not enabled, the wrapped value can
3500 /// be returned in those cases, but it is not considered a
3501 /// breaking change if in the future it panics; using this method
3502 /// when 1 and −1 cannot be represented is almost certainly a bug.
3503 #[must_use]
3504 fn signum(self) -> Self;
3505
3506 /// Addition with an unsigned fixed-point number.
3507 ///
3508 /// See also <code>FixedI32::[add\_unsigned][FixedI32::add_unsigned]</code>.
3509 #[must_use]
3510 fn add_unsigned(self, rhs: Self::Unsigned) -> Self;
3511
3512 /// Subtraction with an unsigned fixed-point number.
3513 ///
3514 /// See also <code>FixedI32::[sub\_unsigned][FixedI32::sub_unsigned]</code>.
3515 #[must_use]
3516 fn sub_unsigned(self, rhs: Self::Unsigned) -> Self;
3517
3518 /// Checked absolute value. Returns the absolute value, or [`None`] on overflow.
3519 ///
3520 /// Overflow can only occur when trying to find the absolute value of the minimum value.
3521 ///
3522 /// See also <code>FixedI32::[checked\_abs][FixedI32::checked_abs]</code>.
3523 fn checked_abs(self) -> Option<Self>;
3524
3525 /// Checked signum. Returns a number representing the sign of
3526 /// `self`, or [`None`] on overflow.
3527 ///
3528 /// Overflow can only occur
3529 /// * if the value is positive and the fixed-point number has zero
3530 /// or one integer bits such that it cannot hold the value 1.
3531 /// * if the value is negative and the fixed-point number has zero
3532 /// integer bits, such that it cannot hold the value −1.
3533 ///
3534 /// See also
3535 /// <code>FixedI32::[checked\_signum][FixedI32::checked_signum]</code>.
3536 fn checked_signum(self) -> Option<Self>;
3537
3538 /// Checked addition with an unsigned fixed-point number. Returns the sum,
3539 /// or [`None`] on overflow.
3540 ///
3541 /// See also
3542 /// <code>FixedI32::[checked\_add\_unsigned][FixedI32::checked_add_unsigned]</code>.
3543 #[must_use]
3544 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
3545
3546 /// Checked subtraction with an unsigned fixed-point number. Returns the
3547 /// difference, or [`None`] on overflow.
3548 ///
3549 /// See also <code>FixedI32::[checked\_sub\_unsigned][FixedI32::checked_sub_unsigned]</code>.
3550 #[must_use]
3551 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
3552
3553 /// Saturating absolute value. Returns the absolute value, saturating on overflow.
3554 ///
3555 /// Overflow can only occur when trying to find the absolute value of the minimum value.
3556 ///
3557 /// See also
3558 /// <code>FixedI32::[saturating\_abs][FixedI32::saturating_abs]</code>.
3559 #[must_use]
3560 fn saturating_abs(self) -> Self;
3561
3562 /// Saturating signum. Returns a number representing the sign of
3563 /// `self`, saturating on overflow.
3564 ///
3565 /// Overflow can only occur
3566 /// * if the value is positive and the fixed-point number has zero
3567 /// or one integer bits such that it cannot hold the value 1.
3568 /// * if the value is negative and the fixed-point number has zero
3569 /// integer bits, such that it cannot hold the value −1.
3570 ///
3571 /// See also
3572 /// <code>FixedI32::[saturating\_signum][FixedI32::saturating_signum]</code>.
3573 #[must_use]
3574 fn saturating_signum(self) -> Self;
3575
3576 /// Saturating addition with an unsigned fixed-point number. Returns the
3577 /// sum, saturating on overflow.
3578 ///
3579 /// See also
3580 /// <code>FixedI32::[saturating\_add\_unsigned][FixedI32::saturating_add_unsigned]</code>.
3581 #[must_use]
3582 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
3583
3584 /// Saturating subtraction with an unsigned fixed-point number. Returns the
3585 /// difference, saturating on overflow.
3586 ///
3587 /// See also
3588 /// <code>FixedI32::[saturating\_sub\_unsigned][FixedI32::saturating_sub_unsigned]</code>.
3589 #[must_use]
3590 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
3591
3592 /// Wrapping absolute value. Returns the absolute value, wrapping on overflow.
3593 ///
3594 /// Overflow can only occur when trying to find the absolute value of the minimum value.
3595 ///
3596 /// See also <code>FixedI32::[wrapping\_abs][FixedI32::wrapping_abs]</code>.
3597 #[must_use]
3598 fn wrapping_abs(self) -> Self;
3599
3600 /// Wrapping signum. Returns a number representing the sign of
3601 /// `self`, wrapping on overflow.
3602 ///
3603 /// Overflow can only occur
3604 /// * if the value is positive and the fixed-point number has zero
3605 /// or one integer bits such that it cannot hold the value 1.
3606 /// * if the value is negative and the fixed-point number has zero
3607 /// integer bits, such that it cannot hold the value −1.
3608 ///
3609 /// See also
3610 /// <code>FixedI32::[wrapping\_signum][FixedI32::wrapping_signum]</code>.
3611 #[must_use]
3612 fn wrapping_signum(self) -> Self;
3613
3614 /// Wrapping addition with an unsigned fixed-point number. Returns the sum,
3615 /// wrapping on overflow.
3616 ///
3617 /// See also
3618 /// <code>FixedI32::[wrapping\_add\_unsigned][FixedI32::wrapping_add_unsigned]</code>.
3619 #[must_use]
3620 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
3621
3622 /// Wrapping subtraction with an unsigned fixed-point number. Returns the
3623 /// difference, wrapping on overflow.
3624 ///
3625 /// See also
3626 /// <code>FixedI32::[wrapping\_sub\_unsigned][FixedI32::wrapping_sub_unsigned]</code>.
3627 #[must_use]
3628 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
3629
3630 /// Unwrapped absolute value. Returns the absolute value, panicking on overflow.
3631 ///
3632 /// Overflow can only occur when trying to find the absolute value of the minimum value.
3633 ///
3634 /// See also
3635 /// <code>FixedI32::[unwrapped\_abs][FixedI32::unwrapped_abs]</code>.
3636 ///
3637 /// # Panics
3638 ///
3639 /// Panics if the result does not fit.
3640 #[track_caller]
3641 #[must_use]
3642 fn unwrapped_abs(self) -> Self;
3643
3644 /// Unwrapped signum. Returns a number representing the sign of
3645 /// `self`, panicking on overflow.
3646 ///
3647 /// Overflow can only occur
3648 /// * if the value is positive and the fixed-point number has zero
3649 /// or one integer bits such that it cannot hold the value 1.
3650 /// * if the value is negative and the fixed-point number has zero
3651 /// integer bits, such that it cannot hold the value −1.
3652 ///
3653 /// See also
3654 /// <code>FixedI32::[unwrapped\_signum][FixedI32::unwrapped_signum]</code>.
3655 ///
3656 /// # Panics
3657 ///
3658 /// Panics if the result does not fit.
3659 #[track_caller]
3660 #[must_use]
3661 fn unwrapped_signum(self) -> Self;
3662
3663 /// Unwrapped addition with an unsigned fixed-point number. Returns the sum,
3664 /// panicking on overflow.
3665 ///
3666 /// See also
3667 /// <code>FixedI32::[unwrapped\_add\_unsigned][FixedI32::unwrapped_add_unsigned]</code>.
3668 ///
3669 /// # Panics
3670 ///
3671 /// Panics if the result does not fit.
3672 #[track_caller]
3673 #[must_use]
3674 fn unwrapped_add_unsigned(self, rhs: Self::Unsigned) -> Self;
3675
3676 /// Unwrapped subtraction with an unsigned fixed-point number. Returns the
3677 /// difference, panicking on overflow.
3678 ///
3679 /// See also
3680 /// <code>FixedI32::[unwrapped\_sub\_unsigned][FixedI32::unwrapped_sub_unsigned]</code>.
3681 ///
3682 /// # Panics
3683 ///
3684 /// Panics if the result does not fit.
3685 #[track_caller]
3686 #[must_use]
3687 fn unwrapped_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
3688
3689 /// Overflowing absolute value.
3690 ///
3691 /// Returns a [tuple] of the fixed-point number and a [`bool`],
3692 /// indicating whether an overflow has occurred. On overflow, the
3693 /// wrapped value is returned.
3694 ///
3695 /// See also
3696 /// <code>FixedI32::[overflowing\_abs][FixedI32::overflowing_abs]</code>.
3697 fn overflowing_abs(self) -> (Self, bool);
3698
3699 /// Overflowing signum.
3700 ///
3701 /// Returns a [tuple] of the signum and a [`bool`], indicating
3702 /// whether an overflow has occurred. On overflow, the wrapped
3703 /// value is returned.
3704 ///
3705 /// Overflow can only occur
3706 /// * if the value is positive and the fixed-point number has zero
3707 /// or one integer bits such that it cannot hold the value 1.
3708 /// * if the value is negative and the fixed-point number has zero
3709 /// integer bits, such that it cannot hold the value −1.
3710 ///
3711 /// See also
3712 /// <code>FixedI32::[overflowing\_signum][FixedI32::overflowing_signum]</code>.
3713 fn overflowing_signum(self) -> (Self, bool);
3714
3715 /// Overflowing addition with an unsigned fixed-point number.
3716 ///
3717 /// Returns a [tuple] of the sum and a [`bool`], indicating whether an
3718 /// overflow has occurred. On overflow, the wrapped value is returned.
3719 ///
3720 /// See also
3721 /// <code>FixedI32::[overflowing\_add\_unsigned][FixedI32::overflowing_add_unsigned]</code>.
3722 #[must_use]
3723 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
3724
3725 /// Overflowing subtraction with an unsigned fixed-point number.
3726 ///
3727 /// Returns a [tuple] of the difference and a [`bool`], indicating whether
3728 /// an overflow has occurred. On overflow, the wrapped value is returned.
3729 ///
3730 /// See also
3731 /// <code>FixedI32::[overflowing\_sub\_unsigned][FixedI32::overflowing_sub_unsigned]</code>.
3732 #[must_use]
3733 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
3734}
3735
3736/// This trait provides methods common to all unsigned fixed-point numbers.
3737///
3738/// Methods common to all fixed-point numbers including signed
3739/// fixed-point numbers are provided by the [`Fixed`] supertrait.
3740///
3741/// This trait is sealed and cannot be implemented for more types; it
3742/// is implemented for [`FixedU8`], [`FixedU16`], [`FixedU32`],
3743/// [`FixedU64`], and [`FixedU128`].
3744pub trait FixedUnsigned: Fixed
3745where
3746 Self: Div<<Self as Fixed>::NonZeroBits, Output = Self>,
3747 Self: DivAssign<<Self as Fixed>::NonZeroBits>,
3748{
3749 /// Returns the number of bits required to represent the value.
3750 ///
3751 /// See also
3752 /// <code>FixedU32::[significant\_bits][FixedU32::significant_bits]</code>.
3753 fn significant_bits(self) -> u32;
3754
3755 /// Returns [`true`] if the fixed-point number is
3756 /// 2<sup><i>k</i></sup> for some integer <i>k</i>.
3757 ///
3758 /// See also
3759 /// <code>FixedU32::[is\_power\_of\_two][FixedU32::is_power_of_two]</code>.
3760 fn is_power_of_two(self) -> bool;
3761
3762 /// Returns the highest one in the binary representation, or zero
3763 /// if `self` is zero.
3764 ///
3765 /// See also <code>FixedU32::[highest\_one][FixedU32::highest_one]</code>.
3766 #[must_use]
3767 fn highest_one(self) -> Self;
3768
3769 /// Returns the smallest power of two that is ≥ `self`.
3770 ///
3771 /// See also
3772 /// <code>FixedU32::[next\_power\_of\_two][FixedU32::next_power_of_two]</code>.
3773 #[must_use]
3774 fn next_power_of_two(self) -> Self;
3775
3776 /// Addition with an signed fixed-point number.
3777 ///
3778 /// See also <code>FixedU32::[add\_signed][FixedU32::add_signed]</code>.
3779 #[must_use]
3780 fn add_signed(self, rhs: Self::Signed) -> Self;
3781
3782 /// Subtraction with an signed fixed-point number.
3783 ///
3784 /// See also <code>FixedU32::[sub\_signed][FixedU32::sub_signed]</code>.
3785 #[must_use]
3786 fn sub_signed(self, rhs: Self::Signed) -> Self;
3787
3788 /// Returns the smallest power of two that is ≥ `self`, or [`None`] if the
3789 /// next power of two is too large to represent.
3790 ///
3791 /// See also
3792 /// <code>FixedU32::[checked\_next\_power\_of\_two][FixedU32::checked_next_power_of_two]</code>.
3793 fn checked_next_power_of_two(self) -> Option<Self>;
3794
3795 /// Checked addition with an signed fixed-point number. Returns the sum,
3796 /// or [`None`] on overflow.
3797 ///
3798 /// See also
3799 /// <code>FixedU32::[checked\_add\_signed][FixedU32::checked_add_signed]</code>.
3800 #[must_use]
3801 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
3802
3803 /// Checked subtraction with an signed fixed-point number. Returns the
3804 /// difference, or [`None`] on overflow.
3805 ///
3806 /// See also <code>FixedU32::[checked\_sub\_signed][FixedU32::checked_sub_signed]</code>.
3807 #[must_use]
3808 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
3809
3810 /// Saturating addition with an signed fixed-point number. Returns the
3811 /// sum, saturating on overflow.
3812 ///
3813 /// See also
3814 /// <code>FixedU32::[saturating\_add\_signed][FixedU32::saturating_add_signed]</code>.
3815 #[must_use]
3816 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
3817
3818 /// Saturating subtraction with an signed fixed-point number. Returns the
3819 /// difference, saturating on overflow.
3820 ///
3821 /// See also
3822 /// <code>FixedU32::[saturating\_sub\_signed][FixedU32::saturating_sub_signed]</code>.
3823 #[must_use]
3824 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
3825
3826 /// Returns the smallest power of two that is ≥ `self`, wrapping
3827 /// to 0 if the next power of two is too large to represent.
3828 ///
3829 /// See also
3830 /// <code>FixedU32::[wrapping\_next\_power\_of\_two][FixedU32::wrapping_next_power_of_two]</code>.
3831 #[must_use]
3832 fn wrapping_next_power_of_two(self) -> Self;
3833
3834 /// Wrapping addition with an signed fixed-point number. Returns the sum,
3835 /// wrapping on overflow.
3836 ///
3837 /// See also
3838 /// <code>FixedU32::[wrapping\_add\_signed][FixedU32::wrapping_add_signed]</code>.
3839 #[must_use]
3840 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
3841
3842 /// Wrapping subtraction with an signed fixed-point number. Returns the
3843 /// difference, wrapping on overflow.
3844 ///
3845 /// See also
3846 /// <code>FixedU32::[wrapping\_sub\_signed][FixedU32::wrapping_sub_signed]</code>.
3847 #[must_use]
3848 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
3849
3850 /// Returns the smallest power of two that is ≥ `self`, panicking
3851 /// if the next power of two is too large to represent.
3852 ///
3853 /// See also
3854 /// <code>FixedU32::[unwrapped\_next\_power\_of\_two][FixedU32::unwrapped_next_power_of_two]</code>.
3855 ///
3856 /// # Panics
3857 ///
3858 /// Panics if the result does not fit.
3859 #[track_caller]
3860 #[must_use]
3861 fn unwrapped_next_power_of_two(self) -> Self;
3862
3863 /// Unwrapped addition with an signed fixed-point number. Returns the sum,
3864 /// panicking on overflow.
3865 ///
3866 /// See also
3867 /// <code>FixedU32::[unwrapped\_add\_signed][FixedU32::unwrapped_add_signed]</code>.
3868 ///
3869 /// # Panics
3870 ///
3871 /// Panics if the result does not fit.
3872 #[track_caller]
3873 #[must_use]
3874 fn unwrapped_add_signed(self, rhs: Self::Signed) -> Self;
3875
3876 /// Unwrapped subtraction with an signed fixed-point number. Returns the
3877 /// difference, panicking on overflow.
3878 ///
3879 /// See also
3880 /// <code>FixedU32::[unwrapped\_sub\_signed][FixedU32::unwrapped_sub_signed]</code>.
3881 ///
3882 /// # Panics
3883 ///
3884 /// Panics if the result does not fit.
3885 #[track_caller]
3886 #[must_use]
3887 fn unwrapped_sub_signed(self, rhs: Self::Signed) -> Self;
3888
3889 /// Overflowing addition with an signed fixed-point number.
3890 ///
3891 /// Returns a [tuple] of the sum and a [`bool`], indicating whether an
3892 /// overflow has occurred. On overflow, the wrapped value is returned.
3893 ///
3894 /// See also
3895 /// <code>FixedU32::[overflowing\_add\_signed][FixedU32::overflowing_add_signed]</code>.
3896 #[must_use]
3897 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
3898
3899 /// Overflowing subtraction with an signed fixed-point number.
3900 ///
3901 /// Returns a [tuple] of the difference and a [`bool`], indicating whether
3902 /// an overflow has occurred. On overflow, the wrapped value is returned.
3903 ///
3904 /// See also
3905 /// <code>FixedU32::[overflowing\_sub\_signed][FixedU32::overflowing_sub_signed]</code>.
3906 #[must_use]
3907 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
3908}
3909
3910/// This trait provides lossless conversions that might be fallible.
3911///
3912/// This trait is implemented for conversions between integer
3913/// primitives, floating-point primitives and fixed-point numbers.
3914///
3915/// # Examples
3916///
3917/// ```rust
3918/// use fixed::traits::LosslessTryFrom;
3919/// use fixed::types::{I24F8, I4F12};
3920/// // original is 0x000001.23, lossless is 0x1.230
3921/// let original = I24F8::from_bits(0x0000_0123);
3922/// let lossless = I4F12::lossless_try_from(original);
3923/// assert_eq!(lossless, Some(I4F12::from_bits(0x1230)));
3924/// // too_large is 0x000012.34, 0x12.340 does not fit in I4F12
3925/// let too_large = I24F8::from_bits(0x0000_1234);
3926/// let overflow = I4F12::lossless_try_from(too_large);
3927/// assert_eq!(overflow, None);
3928/// ```
3929pub trait LosslessTryFrom<Src>: Sized {
3930 /// Performs the conversion.
3931 fn lossless_try_from(src: Src) -> Option<Self>;
3932}
3933
3934/// This trait provides lossless conversions that might be fallible.
3935/// This is the reciprocal of [`LosslessTryFrom`].
3936///
3937/// Usually [`LosslessTryFrom`] should be implemented instead of this
3938/// trait; there is a blanket implementation which provides this trait
3939/// when [`LosslessTryFrom`] is implemented (similar to [`Into`] and
3940/// [`From`]).
3941///
3942/// # Examples
3943///
3944/// ```rust
3945/// use fixed::traits::LosslessTryInto;
3946/// use fixed::types::{I24F8, I4F12};
3947/// // original is 0x000001.23, lossless is 0x1.230
3948/// let original = I24F8::from_bits(0x0000_0123);
3949/// let lossless: Option<I4F12> = original.lossless_try_into();
3950/// assert_eq!(lossless, Some(I4F12::from_bits(0x1230)));
3951/// // too_large is 0x000012.34, 0x12.340 does not fit in I4F12
3952/// let too_large = I24F8::from_bits(0x0000_1234);
3953/// let overflow: Option<I4F12> = too_large.lossless_try_into();
3954/// assert_eq!(overflow, None);
3955/// ```
3956pub trait LosslessTryInto<Dst> {
3957 /// Performs the conversion.
3958 fn lossless_try_into(self) -> Option<Dst>;
3959}
3960
3961impl<Src, Dst> LosslessTryInto<Dst> for Src
3962where
3963 Dst: LosslessTryFrom<Src>,
3964{
3965 fn lossless_try_into(self) -> Option<Dst> {
3966 Dst::lossless_try_from(self)
3967 }
3968}
3969
3970/// This trait provides infallible conversions that might be lossy.
3971///
3972/// This trait is implemented for conversions between integer
3973/// primitives, floating-point primitives and fixed-point numbers.
3974///
3975/// # Examples
3976///
3977/// ```rust
3978/// use fixed::traits::LossyFrom;
3979/// use fixed::types::{I12F4, I8F24};
3980/// // original is 0x12.345678, lossy is 0x012.3
3981/// let original = I8F24::from_bits(0x1234_5678);
3982/// let lossy = I12F4::lossy_from(original);
3983/// assert_eq!(lossy, I12F4::from_bits(0x0123));
3984/// ```
3985pub trait LossyFrom<Src> {
3986 /// Performs the conversion.
3987 fn lossy_from(src: Src) -> Self;
3988}
3989
3990/// This trait provides infallible conversions that might be lossy.
3991/// This is the reciprocal of [`LossyFrom`].
3992///
3993/// Usually [`LossyFrom`] should be implemented instead of this trait;
3994/// there is a blanket implementation which provides this trait when
3995/// [`LossyFrom`] is implemented (similar to [`Into`] and [`From`]).
3996///
3997/// # Examples
3998///
3999/// ```rust
4000/// use fixed::traits::LossyInto;
4001/// use fixed::types::{I12F4, I8F24};
4002/// // original is 0x12.345678, lossy is 0x012.3
4003/// let original = I8F24::from_bits(0x1234_5678);
4004/// let lossy: I12F4 = original.lossy_into();
4005/// assert_eq!(lossy, I12F4::from_bits(0x0123));
4006/// ```
4007pub trait LossyInto<Dst> {
4008 /// Performs the conversion.
4009 fn lossy_into(self) -> Dst;
4010}
4011
4012impl<Src, Dst> LossyInto<Dst> for Src
4013where
4014 Dst: LossyFrom<Src>,
4015{
4016 fn lossy_into(self) -> Dst {
4017 Dst::lossy_from(self)
4018 }
4019}
4020
4021/// This trait provides checked conversions from fixed-point numbers.
4022///
4023/// This trait is implemented for conversions between integer
4024/// primitives, floating-point primitives and fixed-point numbers.
4025///
4026/// # Examples
4027///
4028/// ```rust
4029/// use fixed::traits::FromFixed;
4030/// use fixed::types::U8F8;
4031/// // 0x87.65
4032/// let f = U8F8::from_bits(0x8765);
4033/// assert_eq!(f32::from_fixed(f), f32::from(0x8765u16) / 256.0);
4034/// assert_eq!(i32::checked_from_fixed(f), Some(0x87));
4035/// assert_eq!(u8::saturating_from_fixed(f), 0x87);
4036/// // no fit
4037/// assert_eq!(i8::checked_from_fixed(f), None);
4038/// assert_eq!(i8::saturating_from_fixed(f), i8::MAX);
4039/// assert_eq!(i8::wrapping_from_fixed(f), 0x87u8 as i8);
4040/// assert_eq!(i8::overflowing_from_fixed(f), (0x87u8 as i8, true));
4041/// ```
4042pub trait FromFixed {
4043 /// Converts from a fixed-point number.
4044 ///
4045 /// Any extra fractional bits are discarded, which rounds towards −∞.
4046 ///
4047 /// # Panics
4048 ///
4049 /// When debug assertions are enabled, panics if the value does
4050 /// not fit. When debug assertions are not enabled, the wrapped
4051 /// value can be returned, but it is not considered a breaking
4052 /// change if in the future it panics; if wrapping is required use
4053 /// [`wrapping_from_fixed`] instead.
4054 ///
4055 /// [`wrapping_from_fixed`]: FromFixed::wrapping_from_fixed
4056 fn from_fixed<F: Fixed>(src: F) -> Self;
4057
4058 /// Converts from a fixed-point number if it fits, otherwise returns [`None`].
4059 ///
4060 /// Any extra fractional bits are discarded, which rounds towards −∞.
4061 fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
4062 where
4063 Self: Sized;
4064
4065 /// Converts from a fixed-point number, saturating if it does not fit.
4066 ///
4067 /// Any extra fractional bits are discarded, which rounds towards −∞.
4068 fn saturating_from_fixed<F: Fixed>(src: F) -> Self;
4069
4070 /// Converts from a fixed-point number, wrapping if it does not fit.
4071 ///
4072 /// Any extra fractional bits are discarded, which rounds towards −∞.
4073 fn wrapping_from_fixed<F: Fixed>(src: F) -> Self;
4074
4075 /// Converts from a fixed-point number.
4076 ///
4077 /// Returns a [tuple] of the value and a [`bool`] indicating whether
4078 /// an overflow has occurred. On overflow, the wrapped value is
4079 /// returned.
4080 ///
4081 /// Any extra fractional bits are discarded, which rounds towards −∞.
4082 fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
4083 where
4084 Self: Sized;
4085
4086 /// Converts from a fixed-point number, panicking if the value
4087 /// does not fit.
4088 ///
4089 /// Any extra fractional bits are discarded, which rounds towards −∞.
4090 ///
4091 /// # Panics
4092 ///
4093 /// Panics if the value does not fit, even when debug assertions
4094 /// are not enabled.
4095 #[inline]
4096 #[track_caller]
4097 fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self
4098 where
4099 Self: Sized,
4100 {
4101 match Self::overflowing_from_fixed(src) {
4102 (val, false) => val,
4103 (_, true) => panic!("overflow"),
4104 }
4105 }
4106}
4107
4108/// This trait provides checked conversions to fixed-point numbers.
4109///
4110/// This trait is implemented for conversions between integer
4111/// primitives, floating-point primitives and fixed-point numbers.
4112///
4113/// # Examples
4114///
4115/// ```rust
4116/// use fixed::traits::ToFixed;
4117/// use fixed::types::{U8F8, U16F16};
4118/// let f: U8F8 = 13.5f32.to_fixed();
4119/// assert_eq!(f, U8F8::from_bits((13 << 8) | (1 << 7)));
4120/// // 0x1234.5678 is too large and can be wrapped to 0x34.56
4121/// let too_large = U16F16::from_bits(0x1234_5678);
4122/// let checked: Option<U8F8> = too_large.checked_to_num();
4123/// assert_eq!(checked, None);
4124/// let saturating: U8F8 = too_large.saturating_to_num();
4125/// assert_eq!(saturating, U8F8::MAX);
4126/// let wrapping: U8F8 = too_large.wrapping_to_num();
4127/// assert_eq!(wrapping, U8F8::from_bits(0x3456));
4128/// let overflowing: (U8F8, bool) = too_large.overflowing_to_num();
4129/// assert_eq!(overflowing, (U8F8::from_bits(0x3456), true));
4130/// ```
4131pub trait ToFixed {
4132 /// Converts to a fixed-point number.
4133 ///
4134 /// Any extra fractional bits are discarded, which rounds towards −∞.
4135 ///
4136 /// # Panics
4137 ///
4138 /// Panics if `self` is a floating-point number that is not [finite].
4139 ///
4140 /// When debug assertions are enabled, also panics if the value
4141 /// does not fit. When debug assertions are not enabled, the
4142 /// wrapped value can be returned, but it is not considered a
4143 /// breaking change if in the future it panics; if wrapping is
4144 /// required use [`wrapping_to_fixed`] instead.
4145 ///
4146 /// [`wrapping_to_fixed`]: ToFixed::wrapping_to_fixed
4147 /// [finite]: f64::is_finite
4148 fn to_fixed<F: Fixed>(self) -> F;
4149
4150 /// Converts to a fixed-point number if it fits, otherwise returns [`None`].
4151 ///
4152 /// Any extra fractional bits are discarded, which rounds towards −∞.
4153 fn checked_to_fixed<F: Fixed>(self) -> Option<F>;
4154
4155 /// Converts to a fixed-point number, saturating if it does not fit.
4156 ///
4157 /// Any extra fractional bits are discarded, which rounds towards −∞.
4158 ///
4159 /// # Panics
4160 ///
4161 /// Panics if `self` is a floating-point number that is [NaN].
4162 ///
4163 /// [NaN]: f64::is_nan
4164 fn saturating_to_fixed<F: Fixed>(self) -> F;
4165
4166 /// Converts to a fixed-point number, wrapping if it does not fit.
4167 ///
4168 /// Any extra fractional bits are discarded, which rounds towards −∞.
4169 ///
4170 /// # Panics
4171 ///
4172 /// Panics if `self` is a floating-point number that is not [finite].
4173 ///
4174 /// [finite]: f64::is_finite
4175 fn wrapping_to_fixed<F: Fixed>(self) -> F;
4176
4177 /// Converts to a fixed-point number.
4178 ///
4179 /// Returns a [tuple] of the fixed-point number and a [`bool`]
4180 /// indicating whether an overflow has occurred. On overflow, the
4181 /// wrapped value is returned.
4182 ///
4183 /// Any extra fractional bits are discarded, which rounds towards −∞.
4184 ///
4185 /// # Panics
4186 ///
4187 /// Panics if `self` is a floating-point number that is not [finite].
4188 ///
4189 /// [finite]: f64::is_finite
4190 fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool);
4191
4192 /// Converts to a fixed-point number, panicking if it does not fit.
4193 ///
4194 /// Any extra fractional bits are discarded, which rounds towards −∞.
4195 ///
4196 /// # Panics
4197 ///
4198 /// Panics if `self` is a floating-point number that is not
4199 /// [finite] or if the value does not fit, even if debug
4200 /// assertions are not enabled.
4201 ///
4202 /// [finite]: f64::is_finite
4203 #[inline]
4204 #[track_caller]
4205 fn unwrapped_to_fixed<F: Fixed>(self) -> F
4206 where
4207 Self: Sized,
4208 {
4209 match self.overflowing_to_fixed() {
4210 (val, false) => val,
4211 (_, true) => panic!("overflow"),
4212 }
4213 }
4214}
4215
4216/// This trait provides a way to convert a number to/from an equivalent
4217/// fixed-point number.
4218///
4219/// Implementations are provided for the signed integer primitives [`i8`],
4220/// [`i16`], [`i32`], [`i64`] and [`i128`], which have equivalent fixed-point
4221/// types [`I8F0`], [`I16F0`], [`I32F0`], [`I64F0`] and [`I128F0`]. Similar
4222/// implementations are provided for the unsigned integer primitives [`u8`],
4223/// [`u16`], [`u32`], [`u64`] and [`u128`].
4224///
4225/// # Examples
4226///
4227/// An [`i32`] can be treated as an [`I32F0`].
4228///
4229/// ```rust
4230/// use fixed::traits::{Fixed, FixedEquiv};
4231///
4232/// fn next_up<F: Fixed>(f: &mut F) {
4233/// *f += F::DELTA;
4234/// }
4235///
4236/// let mut i = 12i32;
4237/// // next_up is called with &mut i converted to &mut I32F0
4238/// next_up(i.as_fixed_equiv_mut());
4239/// assert_eq!(i, 13);
4240/// ```
4241///
4242/// Simlarly, an [`I32F0`] can be treated as an [`i32`].
4243///
4244/// ```rust
4245/// use fixed::traits::FixedEquiv;
4246/// use fixed::types::I32F0;
4247///
4248/// fn increase_by_5(i: &mut i32) {
4249/// *i += 5;
4250/// }
4251///
4252/// let mut f = I32F0::from_num(12);
4253/// // increase_by_5 is called with &mut f converted to &mut i32
4254/// increase_by_5(i32::mut_from_fixed_equiv(&mut f));
4255/// assert_eq!(f, 17);
4256/// ```
4257///
4258/// [`I8F0`]: crate::types::I8F0
4259/// [`I16F0`]: crate::types::I16F0
4260/// [`I32F0`]: crate::types::I32F0
4261/// [`I64F0`]: crate::types::I64F0
4262/// [`I128F0`]: crate::types::I128F0
4263pub trait FixedEquiv {
4264 /// The equivalent fixed-point type.
4265 type Equiv: Fixed;
4266
4267 /// Converts an owned value to the equivalent fixed-point type.
4268 fn to_fixed_equiv(self) -> Self::Equiv;
4269
4270 /// Converts a reference into a reference to the equivalent fixed-point
4271 /// type.
4272 fn as_fixed_equiv(&self) -> &Self::Equiv;
4273
4274 /// Converts a mutable reference into a mutable reference to the equivalent
4275 /// fixed-point type.
4276 fn as_fixed_equiv_mut(&mut self) -> &mut Self::Equiv;
4277
4278 /// Converts an owned equivalent fixed-point type to this type.
4279 fn from_fixed_equiv(f: Self::Equiv) -> Self;
4280
4281 /// Converts a reference to the equivalent fixed-point type into a reference
4282 /// to this type.
4283 fn ref_from_fixed_equiv(f: &Self::Equiv) -> &Self;
4284
4285 /// Converts a mutable reference to the equivalent fixed-point type into a
4286 /// mutable reference to this type.
4287 fn mut_from_fixed_equiv(f: &mut Self::Equiv) -> &mut Self;
4288}
4289
4290macro_rules! trait_delegate {
4291 (fn $method:ident($($param:ident: $Param:ty),*$(,)?) -> $Ret:ty) => {
4292 #[inline]
4293 fn $method($($param: $Param),*) -> $Ret {
4294 Self::$method($($param),*)
4295 }
4296 };
4297 (fn $method:ident(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4298 #[inline]
4299 fn $method(self $(, $param: $Param)*) -> $Ret {
4300 self.$method($($param),*)
4301 }
4302 };
4303 (unsafe fn $method:ident(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4304 #[inline]
4305 unsafe fn $method(self $(, $param: $Param)*) -> $Ret {
4306 unsafe { self.$method($($param),*) }
4307 }
4308 };
4309 (fn $method:ident(&mut self $(, $param:ident: $Param:ty)*) $(-> $Ret:ty)*) => {
4310 #[inline]
4311 fn $method(&mut self $(, $param: $Param)*) $(-> $Ret)* {
4312 self.$method($($param),*)
4313 }
4314 };
4315 (fn $method:ident<$Gen:ident: $Trait:ident>($($param:ident: $Param:ty),*) -> $Ret:ty) => {
4316 #[inline]
4317 fn $method<$Gen: $Trait>($($param: $Param),*) -> $Ret {
4318 Self::$method($($param),*)
4319 }
4320 };
4321 (fn $method:ident<$Gen:ident: $Trait:ident>(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4322 #[inline]
4323 fn $method<$Gen: $Trait>(self $(, $param: $Param)*) -> $Ret {
4324 self.$method($($param),*)
4325 }
4326 };
4327}
4328
4329macro_rules! impl_fixed {
4330 (
4331 $Fixed:ident, $IFixed:ident, $UFixed:ident, $LeEqU:ident, $Bits:ident, $Signedness:ident
4332 ) => {
4333 impl<Frac: $LeEqU> FixedOptionalArbitrary for $Fixed<Frac> {}
4334 impl<Frac: $LeEqU> FixedOptionalBorsh for $Fixed<Frac> {}
4335 impl<Frac: $LeEqU> FixedOptionalNum for $Fixed<Frac> {}
4336 impl<Frac: $LeEqU> FixedOptionalSerde for $Fixed<Frac> {}
4337 impl<Frac: $LeEqU> FixedOptionalNightlyFloat for $Fixed<Frac> {}
4338 impl<Frac: $LeEqU> FixedOptionalFeatures for $Fixed<Frac> {}
4339
4340 impl<Frac: $LeEqU> Fixed for $Fixed<Frac> {
4341 type Bits = $Bits;
4342 type NonZeroBits = NonZero<$Bits>;
4343 type Bytes = [u8; size_of::<$Bits>()];
4344 type Frac = Frac;
4345 type Signed = $IFixed<Frac>;
4346 type Unsigned = $UFixed<Frac>;
4347 const ZERO: Self = Self::ZERO;
4348 const TRY_ONE: Option<Self> = Self::TRY_ONE;
4349 const DELTA: Self = Self::DELTA;
4350 const MIN: Self = Self::MIN;
4351 const MAX: Self = Self::MAX;
4352 const IS_SIGNED: bool = Self::IS_SIGNED;
4353 const INT_NBITS: u32 = Self::INT_NBITS;
4354 const FRAC_NBITS: u32 = Self::FRAC_NBITS;
4355 trait_delegate! { fn from_bits(bits: Self::Bits) -> Self }
4356 trait_delegate! { fn to_bits(self) -> Self::Bits }
4357 trait_delegate! { fn from_be(fixed: Self) -> Self }
4358 trait_delegate! { fn from_le(fixed: Self) -> Self }
4359 trait_delegate! { fn to_be(self) -> Self }
4360 trait_delegate! { fn to_le(self) -> Self }
4361 trait_delegate! { fn swap_bytes(self) -> Self }
4362 trait_delegate! { fn from_be_bytes(bits: Self::Bytes) -> Self }
4363 trait_delegate! { fn from_le_bytes(bits: Self::Bytes) -> Self }
4364 trait_delegate! { fn from_ne_bytes(bits: Self::Bytes) -> Self }
4365 trait_delegate! { fn to_be_bytes(self) -> Self::Bytes }
4366 trait_delegate! { fn to_le_bytes(self) -> Self::Bytes }
4367 trait_delegate! { fn to_ne_bytes(self) -> Self::Bytes }
4368 trait_delegate! { fn from_num<Src: ToFixed>(src: Src) -> Self }
4369 trait_delegate! { fn to_num<Dst: FromFixed>(self) -> Dst }
4370 trait_delegate! { fn checked_from_num<Src: ToFixed>(val: Src) -> Option<Self> }
4371 trait_delegate! { fn checked_to_num<Dst: FromFixed>(self) -> Option<Dst> }
4372 trait_delegate! { fn saturating_from_num<Src: ToFixed>(val: Src) -> Self }
4373 trait_delegate! { fn saturating_to_num<Dst: FromFixed>(self) -> Dst }
4374 trait_delegate! { fn wrapping_from_num<Src: ToFixed>(val: Src) -> Self }
4375 trait_delegate! { fn wrapping_to_num<Dst: FromFixed>(self) -> Dst }
4376 trait_delegate! { fn unwrapped_from_num<Src: ToFixed>(val: Src) -> Self }
4377 trait_delegate! { fn unwrapped_to_num<Dst: FromFixed>(self) -> Dst }
4378 trait_delegate! { fn overflowing_from_num<Src: ToFixed>(val: Src) -> (Self, bool) }
4379 trait_delegate! { fn overflowing_to_num<Dst: FromFixed>(self) -> (Dst, bool) }
4380 trait_delegate! { fn from_str_binary(src: &str) -> Result<Self, ParseFixedError> }
4381 trait_delegate! { fn from_str_octal(src: &str) -> Result<Self, ParseFixedError> }
4382 trait_delegate! { fn from_str_hex(src: &str) -> Result<Self, ParseFixedError> }
4383 trait_delegate! { fn from_ascii(src: &[u8]) -> Result<Self, ParseFixedError> }
4384 trait_delegate! { fn from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError> }
4385 trait_delegate! { fn from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError> }
4386 trait_delegate! { fn from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError> }
4387 trait_delegate! {
4388 fn saturating_from_str(src: &str) -> Result<Self, ParseFixedError>
4389 }
4390 trait_delegate! {
4391 fn saturating_from_str_binary(src: &str) -> Result<Self, ParseFixedError>
4392 }
4393 trait_delegate! {
4394 fn saturating_from_str_octal(src: &str) -> Result<Self, ParseFixedError>
4395 }
4396 trait_delegate! {
4397 fn saturating_from_str_hex(src: &str) -> Result<Self, ParseFixedError>
4398 }
4399 trait_delegate! {
4400 fn saturating_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>
4401 }
4402 trait_delegate! {
4403 fn saturating_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>
4404 }
4405 trait_delegate! {
4406 fn saturating_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>
4407 }
4408 trait_delegate! {
4409 fn saturating_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>
4410 }
4411 trait_delegate! {
4412 fn wrapping_from_str(src: &str) -> Result<Self, ParseFixedError>
4413 }
4414 trait_delegate! {
4415 fn wrapping_from_str_binary(src: &str) -> Result<Self, ParseFixedError>
4416 }
4417 trait_delegate! {
4418 fn wrapping_from_str_octal(src: &str) -> Result<Self, ParseFixedError>
4419 }
4420 trait_delegate! {
4421 fn wrapping_from_str_hex(src: &str) -> Result<Self, ParseFixedError>
4422 }
4423 trait_delegate! {
4424 fn wrapping_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>
4425 }
4426 trait_delegate! {
4427 fn wrapping_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>
4428 }
4429 trait_delegate! {
4430 fn wrapping_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>
4431 }
4432 trait_delegate! {
4433 fn wrapping_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>
4434 }
4435 trait_delegate! { fn unwrapped_from_str(src: &str) -> Self }
4436 trait_delegate! { fn unwrapped_from_str_binary(src: &str) -> Self }
4437 trait_delegate! { fn unwrapped_from_str_octal(src: &str) -> Self }
4438 trait_delegate! { fn unwrapped_from_str_hex(src: &str) -> Self }
4439 trait_delegate! { fn unwrapped_from_ascii(src: &[u8]) -> Self }
4440 trait_delegate! { fn unwrapped_from_ascii_binary(src: &[u8]) -> Self }
4441 trait_delegate! { fn unwrapped_from_ascii_octal(src: &[u8]) -> Self }
4442 trait_delegate! { fn unwrapped_from_ascii_hex(src: &[u8]) -> Self }
4443 trait_delegate! {
4444 fn overflowing_from_str(src: &str) -> Result<(Self, bool), ParseFixedError>
4445 }
4446 trait_delegate! {
4447 fn overflowing_from_str_binary(src: &str) -> Result<(Self, bool), ParseFixedError>
4448 }
4449 trait_delegate! {
4450 fn overflowing_from_str_octal(src: &str) -> Result<(Self, bool), ParseFixedError>
4451 }
4452 trait_delegate! {
4453 fn overflowing_from_str_hex(src: &str) -> Result<(Self, bool), ParseFixedError>
4454 }
4455 trait_delegate! {
4456 fn overflowing_from_ascii(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
4457 }
4458 trait_delegate! {
4459 fn overflowing_from_ascii_binary(
4460 src: &[u8],
4461 ) -> Result<(Self, bool), ParseFixedError>
4462 }
4463 trait_delegate! {
4464 fn overflowing_from_ascii_octal(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
4465 }
4466 trait_delegate! {
4467 fn overflowing_from_ascii_hex(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
4468 }
4469 trait_delegate! { fn int(self) -> Self }
4470 trait_delegate! { fn frac(self) -> Self }
4471 trait_delegate! { fn ceil(self) -> Self }
4472 trait_delegate! { fn floor(self) -> Self }
4473 trait_delegate! { fn round_to_zero(self) -> Self }
4474 trait_delegate! { fn round(self) -> Self }
4475 trait_delegate! { fn round_ties_even(self) -> Self }
4476 trait_delegate! { fn checked_ceil(self) -> Option<Self> }
4477 trait_delegate! { fn checked_floor(self) -> Option<Self> }
4478 trait_delegate! { fn checked_round(self) -> Option<Self> }
4479 trait_delegate! { fn checked_round_ties_even(self) -> Option<Self> }
4480 trait_delegate! { fn saturating_ceil(self) -> Self }
4481 trait_delegate! { fn saturating_floor(self) -> Self }
4482 trait_delegate! { fn saturating_round(self) -> Self }
4483 trait_delegate! { fn saturating_round_ties_even(self) -> Self }
4484 trait_delegate! { fn wrapping_ceil(self) -> Self }
4485 trait_delegate! { fn wrapping_floor(self) -> Self }
4486 trait_delegate! { fn wrapping_round(self) -> Self }
4487 trait_delegate! { fn wrapping_round_ties_even(self) -> Self }
4488 trait_delegate! { fn unwrapped_ceil(self) -> Self }
4489 trait_delegate! { fn unwrapped_floor(self) -> Self }
4490 trait_delegate! { fn unwrapped_round(self) -> Self }
4491 trait_delegate! { fn unwrapped_round_ties_even(self) -> Self }
4492 trait_delegate! { fn overflowing_ceil(self) -> (Self, bool) }
4493 trait_delegate! { fn overflowing_floor(self) -> (Self, bool) }
4494 trait_delegate! { fn overflowing_round(self) -> (Self, bool) }
4495 trait_delegate! { fn overflowing_round_ties_even(self) -> (Self, bool) }
4496 trait_delegate! { fn count_ones(self) -> u32 }
4497 trait_delegate! { fn count_zeros(self) -> u32 }
4498 trait_delegate! { fn leading_ones(self) -> u32 }
4499 trait_delegate! { fn leading_zeros(self) -> u32 }
4500 trait_delegate! { fn trailing_ones(self) -> u32 }
4501 trait_delegate! { fn trailing_zeros(self) -> u32 }
4502 trait_delegate! { fn int_log2(self) -> i32 }
4503 trait_delegate! { fn int_log10(self) -> i32 }
4504 trait_delegate! { fn int_log(self, base: u32) -> i32 }
4505 trait_delegate! { fn checked_int_log2(self) -> Option<i32> }
4506 trait_delegate! { fn checked_int_log10(self) -> Option<i32> }
4507 trait_delegate! { fn checked_int_log(self, base: u32) -> Option<i32> }
4508 trait_delegate! { fn reverse_bits(self) -> Self }
4509 trait_delegate! { fn rotate_left(self, n: u32) -> Self }
4510 trait_delegate! { fn rotate_right(self, n: u32) -> Self }
4511 trait_delegate! { fn is_zero(self) -> bool }
4512 trait_delegate! { fn dist(self, other: Self) -> Self }
4513 trait_delegate! { fn abs_diff(self, other: Self) -> Self::Unsigned }
4514 trait_delegate! { fn mean(self, other: Self) -> Self }
4515 trait_delegate! { fn hypot(self, other: Self) -> Self }
4516 trait_delegate! { fn recip(self) -> Self }
4517 trait_delegate! { fn next_multiple_of(self, other: Self) -> Self }
4518 trait_delegate! { fn mul_add(self, mul: Self, add: Self) -> Self }
4519 trait_delegate! { fn add_prod(self, a: Self, b: Self) -> Self }
4520 trait_delegate! { fn mul_acc(&mut self, a: Self, b: Self) }
4521 trait_delegate! { fn div_euclid(self, rhs: Self) -> Self }
4522 trait_delegate! { fn rem_euclid(self, rhs: Self) -> Self }
4523 trait_delegate! { fn div_euclid_int(self, rhs: Self::Bits) -> Self }
4524 trait_delegate! { fn rem_euclid_int(self, rhs: Self::Bits) -> Self }
4525 trait_delegate! { fn unbounded_shl(self, rhs: u32) -> Self }
4526 trait_delegate! { fn unbounded_shr(self, rhs: u32) -> Self }
4527 trait_delegate! { fn sqrt(self) -> Self }
4528 trait_delegate! { fn lerp(self, start: Self, end: Self) -> Self }
4529 trait_delegate! { fn inv_lerp(self, start: Self, end: Self) -> Self }
4530 trait_delegate! { fn checked_neg(self) -> Option<Self> }
4531 trait_delegate! { fn checked_add(self, rhs: Self) -> Option<Self> }
4532 trait_delegate! { fn checked_sub(self, rhs: Self) -> Option<Self> }
4533 trait_delegate! { fn checked_mul(self, rhs: Self) -> Option<Self> }
4534 trait_delegate! { fn checked_div(self, rhs: Self) -> Option<Self> }
4535 trait_delegate! { fn checked_rem(self, rhs: Self) -> Option<Self> }
4536 trait_delegate! { fn checked_recip(self) -> Option<Self> }
4537 trait_delegate! { fn checked_next_multiple_of(self, other: Self) -> Option<Self> }
4538 trait_delegate! { fn checked_mul_add(self, mul: Self, add: Self) -> Option<Self> }
4539 trait_delegate! { fn checked_add_prod(self, a: Self, b: Self) -> Option<Self> }
4540 trait_delegate! { fn checked_mul_acc(&mut self, a: Self, b: Self) -> Option<()> }
4541 trait_delegate! { fn checked_div_euclid(self, rhs: Self) -> Option<Self> }
4542 trait_delegate! { fn checked_rem_euclid(self, rhs: Self) -> Option<Self> }
4543 trait_delegate! { fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self> }
4544 trait_delegate! { fn checked_div_int(self, rhs: Self::Bits) -> Option<Self> }
4545 trait_delegate! { fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self> }
4546 trait_delegate! { fn checked_div_euclid_int(self, rhs: Self::Bits) -> Option<Self> }
4547 trait_delegate! { fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self> }
4548 trait_delegate! { fn checked_shl(self, rhs: u32) -> Option<Self> }
4549 trait_delegate! { fn checked_shr(self, rhs: u32) -> Option<Self> }
4550 trait_delegate! { fn checked_dist(self, other: Self) -> Option<Self> }
4551 trait_delegate! { fn checked_hypot(self, other: Self) -> Option<Self> }
4552 trait_delegate! { fn checked_sqrt(self) -> Option<Self> }
4553 trait_delegate! { fn checked_lerp(self, start: Self, end: Self) -> Option<Self> }
4554 trait_delegate! { fn checked_inv_lerp(self, start: Self, end: Self) -> Option<Self> }
4555 trait_delegate! { fn saturating_neg(self) -> Self }
4556 trait_delegate! { fn saturating_add(self, rhs: Self) -> Self }
4557 trait_delegate! { fn saturating_sub(self, rhs: Self) -> Self }
4558 trait_delegate! { fn saturating_mul(self, rhs: Self) -> Self }
4559 trait_delegate! { fn saturating_div(self, rhs: Self) -> Self }
4560 trait_delegate! { fn saturating_recip(self) -> Self }
4561 trait_delegate! { fn saturating_next_multiple_of(self, other: Self) -> Self }
4562 trait_delegate! { fn saturating_mul_add(self, mul: Self, add: Self) -> Self }
4563 trait_delegate! { fn saturating_add_prod(self, a: Self, b: Self) -> Self }
4564 trait_delegate! { fn saturating_mul_acc(&mut self, a: Self, b: Self) }
4565 trait_delegate! { fn saturating_div_euclid(self, rhs: Self) -> Self }
4566 trait_delegate! { fn saturating_mul_int(self, rhs: Self::Bits) -> Self }
4567 trait_delegate! { fn saturating_div_int(self, rhs: Self::Bits) -> Self }
4568 trait_delegate! { fn saturating_div_euclid_int(self, rhs: Self::Bits) -> Self }
4569 trait_delegate! { fn saturating_rem_euclid_int(self, rhs: Self::Bits) -> Self }
4570 trait_delegate! { fn saturating_dist(self, other: Self) -> Self }
4571 trait_delegate! { fn saturating_hypot(self, other: Self) -> Self }
4572 trait_delegate! { fn saturating_sqrt(self) -> Self }
4573 trait_delegate! { fn saturating_lerp(self, start: Self, end: Self) -> Self }
4574 trait_delegate! { fn saturating_inv_lerp(self, start: Self, end: Self) -> Self }
4575 trait_delegate! { fn wrapping_neg(self) -> Self }
4576 trait_delegate! { fn wrapping_add(self, rhs: Self) -> Self }
4577 trait_delegate! { fn wrapping_sub(self, rhs: Self) -> Self }
4578 trait_delegate! { fn wrapping_mul(self, rhs: Self) -> Self }
4579 trait_delegate! { fn wrapping_div(self, rhs: Self) -> Self }
4580 trait_delegate! { fn wrapping_recip(self) -> Self }
4581 trait_delegate! { fn wrapping_next_multiple_of(self, other: Self) -> Self }
4582 trait_delegate! { fn wrapping_mul_add(self, mul: Self, add: Self) -> Self }
4583 trait_delegate! { fn wrapping_add_prod(self, a: Self, b: Self) -> Self }
4584 trait_delegate! { fn wrapping_mul_acc(&mut self, a: Self, b: Self) }
4585 trait_delegate! { fn wrapping_div_euclid(self, rhs: Self) -> Self }
4586 trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self }
4587 trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self }
4588 trait_delegate! { fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self }
4589 trait_delegate! { fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self }
4590 trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self }
4591 trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self }
4592 trait_delegate! { fn wrapping_dist(self, other: Self) -> Self }
4593 trait_delegate! { fn wrapping_hypot(self, other: Self) -> Self }
4594 trait_delegate! { fn wrapping_sqrt(self) -> Self }
4595 trait_delegate! { fn wrapping_lerp(self, start: Self, end: Self) -> Self }
4596 trait_delegate! { fn wrapping_inv_lerp(self, start: Self, end: Self) -> Self }
4597 trait_delegate! { fn unwrapped_neg(self) -> Self }
4598 trait_delegate! { fn unwrapped_add(self, rhs: Self) -> Self }
4599 trait_delegate! { fn unwrapped_sub(self, rhs: Self) -> Self }
4600 trait_delegate! { fn unwrapped_mul(self, rhs: Self) -> Self }
4601 trait_delegate! { fn unwrapped_div(self, rhs: Self) -> Self }
4602 trait_delegate! { fn unwrapped_rem(self, rhs: Self) -> Self }
4603 trait_delegate! { fn unwrapped_recip(self) -> Self }
4604 trait_delegate! { fn unwrapped_next_multiple_of(self, other: Self) -> Self }
4605 trait_delegate! { fn unwrapped_mul_add(self, mul: Self, add: Self) -> Self }
4606 trait_delegate! { fn unwrapped_add_prod(self, a: Self, b: Self) -> Self }
4607 trait_delegate! { fn unwrapped_mul_acc(&mut self, a: Self, b: Self) }
4608 trait_delegate! { fn unwrapped_div_euclid(self, rhs: Self) -> Self }
4609 trait_delegate! { fn unwrapped_rem_euclid(self, rhs: Self) -> Self }
4610 trait_delegate! { fn unwrapped_mul_int(self, rhs: Self::Bits) -> Self }
4611 trait_delegate! { fn unwrapped_div_int(self, rhs: Self::Bits) -> Self }
4612 trait_delegate! { fn unwrapped_rem_int(self, rhs: Self::Bits) -> Self }
4613 trait_delegate! { fn unwrapped_div_euclid_int(self, rhs: Self::Bits) -> Self }
4614 trait_delegate! { fn unwrapped_rem_euclid_int(self, rhs: Self::Bits) -> Self }
4615 trait_delegate! { fn unwrapped_shl(self, rhs: u32) -> Self }
4616 trait_delegate! { fn unwrapped_shr(self, rhs: u32) -> Self }
4617 trait_delegate! { fn unwrapped_dist(self, other: Self) -> Self }
4618 trait_delegate! { fn unwrapped_hypot(self, other: Self) -> Self }
4619 trait_delegate! { fn unwrapped_sqrt(self) -> Self }
4620 trait_delegate! { fn unwrapped_lerp(self, start: Self, end: Self) -> Self }
4621 trait_delegate! { fn unwrapped_inv_lerp(self, start: Self, end: Self) -> Self }
4622 trait_delegate! { fn overflowing_neg(self) -> (Self, bool) }
4623 trait_delegate! { fn overflowing_add(self, rhs: Self) -> (Self, bool) }
4624 trait_delegate! { fn overflowing_sub(self, rhs: Self) -> (Self, bool) }
4625 trait_delegate! { fn overflowing_mul(self, rhs: Self) -> (Self, bool) }
4626 trait_delegate! { fn overflowing_div(self, rhs: Self) -> (Self, bool) }
4627 trait_delegate! { fn overflowing_recip(self) -> (Self, bool) }
4628 trait_delegate! { fn overflowing_next_multiple_of(self, other: Self) -> (Self, bool) }
4629 trait_delegate! { fn overflowing_mul_add(self, mul: Self, add: Self) -> (Self, bool) }
4630 trait_delegate! { fn overflowing_add_prod(self, a: Self, b: Self) -> (Self, bool) }
4631 trait_delegate! { fn overflowing_mul_acc(&mut self, a: Self, b: Self) -> bool }
4632 trait_delegate! { fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) }
4633 trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) }
4634 trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) }
4635 trait_delegate! { fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
4636 trait_delegate! { fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
4637 trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) }
4638 trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) }
4639 trait_delegate! { fn overflowing_dist(self, other: Self) -> (Self, bool) }
4640 trait_delegate! { fn overflowing_hypot(self, other: Self) -> (Self, bool) }
4641 trait_delegate! { fn overflowing_sqrt(self) -> (Self, bool) }
4642 trait_delegate! { fn overflowing_lerp(self, start: Self, end: Self) -> (Self, bool) }
4643 trait_delegate! {
4644 fn overflowing_inv_lerp(self, start: Self, end: Self) -> (Self, bool)
4645 }
4646 trait_delegate! { unsafe fn unchecked_add(self, rhs: Self) -> Self }
4647 trait_delegate! { unsafe fn unchecked_sub(self, rhs: Self) -> Self }
4648 trait_delegate! { unsafe fn unchecked_mul_int(self, rhs: Self::Bits) -> Self }
4649 }
4650
4651 impl<Frac: $LeEqU> FromFixed for $Fixed<Frac> {
4652 /// Converts a fixed-point number.
4653 ///
4654 /// Any extra fractional bits are discarded, which rounds towards −∞.
4655 ///
4656 /// # Panics
4657 ///
4658 /// When debug assertions are enabled, panics if the value
4659 /// does not fit. When debug assertions are not enabled,
4660 /// the wrapped value can be returned, but it is not
4661 /// considered a breaking change if in the future it
4662 /// panics; if wrapping is required use
4663 /// [`wrapping_from_fixed`] instead.
4664 ///
4665 /// [`wrapping_from_fixed`]: FromFixed::wrapping_from_fixed
4666 #[inline]
4667 fn from_fixed<F: Fixed>(src: F) -> Self {
4668 let (wrapped, overflow) = FromFixed::overflowing_from_fixed(src);
4669 debug_assert!(!overflow, "{} overflows", src);
4670 wrapped
4671 }
4672
4673 /// Converts a fixed-point number if it fits, otherwise returns [`None`].
4674 ///
4675 /// Any extra fractional bits are discarded, which rounds towards −∞.
4676 #[inline]
4677 fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self> {
4678 match FromFixed::overflowing_from_fixed(src) {
4679 (_, true) => None,
4680 (wrapped, false) => Some(wrapped),
4681 }
4682 }
4683
4684 /// Converts a fixed-point number, saturating if it does not fit.
4685 ///
4686 /// Any extra fractional bits are discarded, which rounds towards −∞.
4687 #[inline]
4688 fn saturating_from_fixed<F: Fixed>(src: F) -> Self {
4689 let conv = src.to_fixed_helper(Private, Self::FRAC_NBITS, Self::INT_NBITS);
4690 if conv.overflow {
4691 return if src < 0 { Self::MIN } else { Self::MAX };
4692 }
4693 let bits = if_signed_unsigned!(
4694 $Signedness,
4695 match conv.bits {
4696 Widest::Unsigned(bits) => {
4697 if (bits as $Bits) < 0 {
4698 return Self::MAX;
4699 }
4700 bits as $Bits
4701 }
4702 Widest::Negative(bits) => bits as $Bits,
4703 },
4704 match conv.bits {
4705 Widest::Unsigned(bits) => bits as $Bits,
4706 Widest::Negative(_) => {
4707 return Self::MIN;
4708 }
4709 },
4710 );
4711 Self::from_bits(bits)
4712 }
4713
4714 /// Converts a fixed-point number, wrapping if it does not fit.
4715 ///
4716 /// Any extra fractional bits are discarded, which rounds towards −∞.
4717 #[inline]
4718 fn wrapping_from_fixed<F: Fixed>(src: F) -> Self {
4719 let (wrapped, _) = FromFixed::overflowing_from_fixed(src);
4720 wrapped
4721 }
4722
4723 /// Converts a fixed-point number.
4724 ///
4725 /// Returns a [tuple] of the value and a [`bool`]
4726 /// indicating whether an overflow has occurred. On
4727 /// overflow, the wrapped value is returned.
4728 ///
4729 /// Any extra fractional bits are discarded, which rounds towards −∞.
4730 #[inline]
4731 fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool) {
4732 let conv = src.to_fixed_helper(Private, Self::FRAC_NBITS, Self::INT_NBITS);
4733 let mut new_overflow = false;
4734 let bits = if_signed_unsigned!(
4735 $Signedness,
4736 match conv.bits {
4737 Widest::Unsigned(bits) => {
4738 if (bits as $Bits) < 0 {
4739 new_overflow = true;
4740 }
4741 bits as $Bits
4742 }
4743 Widest::Negative(bits) => bits as $Bits,
4744 },
4745 match conv.bits {
4746 Widest::Unsigned(bits) => bits as $Bits,
4747 Widest::Negative(bits) => {
4748 new_overflow = true;
4749 bits as $Bits
4750 }
4751 },
4752 );
4753 (Self::from_bits(bits), conv.overflow || new_overflow)
4754 }
4755
4756 /// Converts a fixed-point number, panicking if it does not fit.
4757 ///
4758 /// Any extra fractional bits are discarded, which rounds towards −∞.
4759 ///
4760 /// # Panics
4761 ///
4762 /// Panics if the value does not fit, even when debug
4763 /// assertions are not enabled.
4764 #[inline]
4765 fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self {
4766 match FromFixed::overflowing_from_fixed(src) {
4767 (val, false) => val,
4768 (_, true) => panic!("overflow"),
4769 }
4770 }
4771 }
4772
4773 impl<Frac: $LeEqU> ToFixed for $Fixed<Frac> {
4774 /// Converts a fixed-point number.
4775 ///
4776 /// Any extra fractional bits are discarded, which rounds towards −∞.
4777 ///
4778 /// # Panics
4779 ///
4780 /// When debug assertions are enabled, panics if the value
4781 /// does not fit. When debug assertions are not enabled,
4782 /// the wrapped value can be returned, but it is not
4783 /// considered a breaking change if in the future it
4784 /// panics; if wrapping is required use
4785 /// [`wrapping_to_fixed`] instead.
4786 ///
4787 /// [`wrapping_to_fixed`]: ToFixed::wrapping_to_fixed
4788 #[inline]
4789 fn to_fixed<F: Fixed>(self) -> F {
4790 FromFixed::from_fixed(self)
4791 }
4792
4793 /// Converts a fixed-point number if it fits, otherwise returns [`None`].
4794 ///
4795 /// Any extra fractional bits are discarded, which rounds towards −∞.
4796 #[inline]
4797 fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
4798 FromFixed::checked_from_fixed(self)
4799 }
4800
4801 /// Converts a fixed-point number, saturating if it does not fit.
4802 ///
4803 /// Any extra fractional bits are discarded, which rounds towards −∞.
4804 #[inline]
4805 fn saturating_to_fixed<F: Fixed>(self) -> F {
4806 FromFixed::saturating_from_fixed(self)
4807 }
4808
4809 /// Converts a fixed-point number, wrapping if it does not fit.
4810 ///
4811 /// Any extra fractional bits are discarded, which rounds towards −∞.
4812 #[inline]
4813 fn wrapping_to_fixed<F: Fixed>(self) -> F {
4814 FromFixed::wrapping_from_fixed(self)
4815 }
4816
4817 /// Converts a fixed-point number.
4818 ///
4819 /// Returns a [tuple] of the value and a [`bool`]
4820 /// indicating whether an overflow has occurred. On
4821 /// overflow, the wrapped value is returned.
4822 ///
4823 /// Any extra fractional bits are discarded, which rounds towards −∞.
4824 #[inline]
4825 fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
4826 FromFixed::overflowing_from_fixed(self)
4827 }
4828 /// Converts a fixed-point number, panicking if it does not fit.
4829 ///
4830 /// Any extra fractional bits are discarded, which rounds towards −∞.
4831 ///
4832 /// # Panics
4833 ///
4834 /// Panics if the value does not fit, even when debug
4835 /// assertions are not enabled.
4836 #[inline]
4837 fn unwrapped_to_fixed<F: Fixed>(self) -> F {
4838 FromFixed::unwrapped_from_fixed(self)
4839 }
4840 }
4841
4842 if_signed! {
4843 $Signedness;
4844 impl<Frac: $LeEqU> FixedSigned for $Fixed<Frac> {
4845 const TRY_NEG_ONE: Option<Self> = Self::TRY_NEG_ONE;
4846 trait_delegate! { fn signed_bits(self) -> u32 }
4847 trait_delegate! { fn is_positive(self) -> bool }
4848 trait_delegate! { fn is_negative(self) -> bool }
4849 trait_delegate! { fn abs(self) -> Self }
4850 trait_delegate! { fn unsigned_abs(self) -> Self::Unsigned }
4851 trait_delegate! { fn unsigned_dist(self, other: Self) -> Self::Unsigned }
4852 trait_delegate! { fn signum(self) -> Self }
4853 trait_delegate! { fn add_unsigned(self, rhs: Self::Unsigned) -> Self }
4854 trait_delegate! { fn sub_unsigned(self, rhs: Self::Unsigned) -> Self }
4855 trait_delegate! { fn checked_abs(self) -> Option<Self> }
4856 trait_delegate! { fn checked_signum(self) -> Option<Self> }
4857 trait_delegate! {
4858 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>
4859 }
4860 trait_delegate! {
4861 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>
4862 }
4863 trait_delegate! { fn saturating_abs(self) -> Self }
4864 trait_delegate! { fn saturating_signum(self) -> Self }
4865 trait_delegate! { fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self }
4866 trait_delegate! { fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
4867 trait_delegate! { fn wrapping_abs(self) -> Self }
4868 trait_delegate! { fn wrapping_signum(self) -> Self }
4869 trait_delegate! { fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self }
4870 trait_delegate! { fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
4871 trait_delegate! { fn unwrapped_abs(self) -> Self }
4872 trait_delegate! { fn unwrapped_signum(self) -> Self }
4873 trait_delegate! { fn unwrapped_add_unsigned(self, rhs: Self::Unsigned) -> Self }
4874 trait_delegate! { fn unwrapped_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
4875 trait_delegate! { fn overflowing_abs(self) -> (Self, bool) }
4876 trait_delegate! { fn overflowing_signum(self) -> (Self, bool) }
4877 trait_delegate! {
4878 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)
4879 }
4880 trait_delegate! {
4881 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)
4882 }
4883 }
4884 }
4885
4886 if_unsigned! {
4887 $Signedness;
4888 impl<Frac: $LeEqU> FixedUnsigned for $Fixed<Frac> {
4889 trait_delegate! { fn significant_bits(self) -> u32 }
4890 trait_delegate! { fn is_power_of_two(self) -> bool }
4891 trait_delegate! { fn highest_one(self) -> Self }
4892 trait_delegate! { fn next_power_of_two(self) -> Self }
4893 trait_delegate! { fn add_signed(self, rhs: Self::Signed) -> Self }
4894 trait_delegate! { fn sub_signed(self, rhs: Self::Signed) -> Self }
4895 trait_delegate! { fn checked_next_power_of_two(self) -> Option<Self> }
4896 trait_delegate! { fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> }
4897 trait_delegate! { fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self> }
4898 trait_delegate! { fn saturating_add_signed(self, rhs: Self::Signed) -> Self }
4899 trait_delegate! { fn saturating_sub_signed(self, rhs: Self::Signed) -> Self }
4900 trait_delegate! { fn wrapping_next_power_of_two(self) -> Self }
4901 trait_delegate! { fn wrapping_add_signed(self, rhs: Self::Signed) -> Self }
4902 trait_delegate! { fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self }
4903 trait_delegate! { fn unwrapped_next_power_of_two(self) -> Self }
4904 trait_delegate! { fn unwrapped_add_signed(self, rhs: Self::Signed) -> Self }
4905 trait_delegate! { fn unwrapped_sub_signed(self, rhs: Self::Signed) -> Self }
4906 trait_delegate! {
4907 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool)
4908 }
4909 trait_delegate! {
4910 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool)
4911 }
4912 }
4913 }
4914 };
4915}
4916
4917impl_fixed! { FixedI8, FixedI8, FixedU8, LeEqU8, i8, Signed }
4918impl_fixed! { FixedI16, FixedI16, FixedU16, LeEqU16, i16, Signed }
4919impl_fixed! { FixedI32, FixedI32, FixedU32, LeEqU32, i32, Signed }
4920impl_fixed! { FixedI64, FixedI64, FixedU64, LeEqU64, i64, Signed }
4921impl_fixed! { FixedI128, FixedI128, FixedU128, LeEqU128, i128, Signed }
4922impl_fixed! { FixedU8, FixedI8, FixedU8, LeEqU8, u8, Unsigned }
4923impl_fixed! { FixedU16, FixedI16, FixedU16, LeEqU16, u16, Unsigned }
4924impl_fixed! { FixedU32, FixedI32, FixedU32, LeEqU32, u32, Unsigned }
4925impl_fixed! { FixedU64, FixedI64, FixedU64, LeEqU64, u64, Unsigned }
4926impl_fixed! { FixedU128, FixedI128, FixedU128, LeEqU128, u128, Unsigned }