fixed/traits.rs
1// Copyright © 2018–2026 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.[strict\_to\_fixed][ToFixed::strict_to_fixed]\()</code>.
934 ///
935 /// See also
936 /// <code>FixedI32::[strict\_from\_num][FixedI32::strict_from_num]</code>
937 /// and
938 /// <code>FixedU32::[strict\_from\_num][FixedU32::strict_from_num]</code>.
939 ///
940 /// # Panics
941 ///
942 /// Panics if the value does not fit.
943 #[track_caller]
944 fn strict_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::[strict\_from\_fixed][FromFixed::strict_from_fixed]\(self)</code>.
950 ///
951 /// See also
952 /// <code>FixedI32::[strict\_to\_num][FixedI32::strict_to_num]</code> and
953 /// <code>FixedU32::[strict\_to\_num][FixedU32::strict_to_num]</code>.
954 ///
955 /// # Panics
956 ///
957 /// Panics if the value does not fit.
958 #[track_caller]
959 fn strict_to_num<Dst: FromFixed>(self) -> Dst;
960
961 /// Creates a fixed-point number from another number.
962 ///
963 /// Returns the same value as
964 /// <code>src.[overflowing\_to\_fixed][ToFixed::overflowing_to_fixed]\()</code>.
965 ///
966 /// See also
967 /// <code>FixedI32::[overflowing\_from\_num][FixedI32::overflowing_from_num]</code>
968 /// and
969 /// <code>FixedU32::[overflowing\_from\_num][FixedU32::overflowing_from_num]</code>.
970 fn overflowing_from_num<Src: ToFixed>(src: Src) -> (Self, bool);
971
972 /// Converts a fixed-point number to another number.
973 ///
974 /// Returns the same value as
975 /// <code>Dst::[overflowing\_from\_fixed][FromFixed::overflowing_from_fixed]\(self)</code>.
976 ///
977 /// See also
978 /// <code>FixedI32::[overflowing\_to\_num][FixedI32::overflowing_to_num]</code>
979 /// and
980 /// <code>FixedU32::[overflowing\_to\_num][FixedU32::overflowing_to_num]</code>.
981 fn overflowing_to_num<Dst: FromFixed>(self) -> (Dst, bool);
982
983 /// Parses a string slice containing binary digits to return a fixed-point number.
984 ///
985 /// Rounding is to the nearest, with ties rounded to even.
986 ///
987 /// See also
988 /// <code>FixedI32::[from\_str\_binary][FixedI32::from_str_binary]</code>
989 /// and
990 /// <code>FixedU32::[from\_str\_binary][FixedU32::from_str_binary]</code>.
991 fn from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
992
993 /// Parses a string slice containing octal digits to return a fixed-point number.
994 ///
995 /// Rounding is to the nearest, with ties rounded to even.
996 ///
997 /// See also
998 /// <code>FixedI32::[from\_str\_octal][FixedI32::from_str_octal]</code> and
999 /// <code>FixedU32::[from\_str\_octal][FixedU32::from_str_octal]</code>.
1000 fn from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1001
1002 /// Parses a string slice containing hexadecimal digits to return a fixed-point number.
1003 ///
1004 /// Rounding is to the nearest, with ties rounded to even.
1005 ///
1006 /// See also <code>FixedI32::[from\_str\_hex][FixedI32::from_str_hex]</code>
1007 /// and <code>FixedU32::[from\_str\_hex][FixedU32::from_str_hex]</code>.
1008 fn from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1009
1010 /// Parses an ASCII-byte slice containing binary digits to return a fixed-point number.
1011 ///
1012 /// Rounding is to the nearest, with ties rounded to even.
1013 ///
1014 /// See also
1015 /// <code>FixedI32::[from\_ascii\_binary][FixedI32::from_ascii_binary]</code>
1016 /// and
1017 /// <code>FixedU32::[from\_ascii\_binary][FixedU32::from_ascii_binary]</code>.
1018 fn from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1019
1020 /// Parses an ASCII-byte slice containing binary digits to return a fixed-point number.
1021 ///
1022 /// Rounding is to the nearest, with ties rounded to even.
1023 ///
1024 /// See also
1025 /// <code>FixedI32::[from\_ascii\_binary][FixedI32::from_ascii_binary]</code>
1026 /// and
1027 /// <code>FixedU32::[from\_ascii\_binary][FixedU32::from_ascii_binary]</code>.
1028 fn from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1029
1030 /// Parses an ASCII-byte slice containing octal digits to return a fixed-point number.
1031 ///
1032 /// Rounding is to the nearest, with ties rounded to even.
1033 ///
1034 /// See also
1035 /// <code>FixedI32::[from\_ascii\_octal][FixedI32::from_ascii_octal]</code> and
1036 /// <code>FixedU32::[from\_ascii\_octal][FixedU32::from_ascii_octal]</code>.
1037 fn from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1038
1039 /// Parses an ASCII-byte slice containing hexadecimal digits to return a fixed-point number.
1040 ///
1041 /// Rounding is to the nearest, with ties rounded to even.
1042 ///
1043 /// See also <code>FixedI32::[from\_ascii\_hex][FixedI32::from_ascii_hex]</code>
1044 /// and <code>FixedU32::[from\_ascii\_hex][FixedU32::from_ascii_hex]</code>.
1045 fn from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1046
1047 /// Parses a string slice containing decimal digits to return a
1048 /// fixed-point number, saturating on overflow.
1049 ///
1050 /// Rounding is to the nearest, with ties rounded to even.
1051 ///
1052 /// See also
1053 /// <code>FixedI32::[saturating\_from\_str][FixedI32::saturating_from_str]</code>
1054 /// and
1055 /// <code>FixedU32::[saturating\_from\_str][FixedU32::saturating_from_str]</code>.
1056 fn saturating_from_str(src: &str) -> Result<Self, ParseFixedError>;
1057
1058 /// Parses a string slice containing binary digits to return a
1059 /// fixed-point number, saturating on overflow.
1060 ///
1061 /// Rounding is to the nearest, with ties rounded to even.
1062 ///
1063 /// See also
1064 /// <code>FixedI32::[saturating\_from\_str\_binary][FixedI32::saturating_from_str_binary]</code>
1065 /// and
1066 /// <code>FixedU32::[saturating\_from\_str\_binary][FixedU32::saturating_from_str_binary]</code>.
1067 fn saturating_from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
1068
1069 /// Parses a string slice containing octal digits to return a
1070 /// fixed-point number, saturating on overflow.
1071 ///
1072 /// Rounding is to the nearest, with ties rounded to even.
1073 ///
1074 /// See also
1075 /// <code>FixedI32::[saturating\_from\_str\_octal][FixedI32::saturating_from_str_octal]</code>
1076 /// and
1077 /// <code>FixedU32::[saturating\_from\_str\_octal][FixedU32::saturating_from_str_octal]</code>.
1078 fn saturating_from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1079
1080 /// Parses a string slice containing hexadecimal digits to return a
1081 /// fixed-point number, saturating on overflow.
1082 ///
1083 /// Rounding is to the nearest, with ties rounded to even.
1084 ///
1085 /// See also
1086 /// <code>FixedI32::[saturating\_from\_str\_hex][FixedI32::saturating_from_str_hex]</code>
1087 /// and
1088 /// <code>FixedU32::[saturating\_from\_str\_hex][FixedU32::saturating_from_str_hex]</code>.
1089 fn saturating_from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1090
1091 /// Parses an ASCII-byte slice containing decimal digits to return a
1092 /// fixed-point number, saturating on overflow.
1093 ///
1094 /// Rounding is to the nearest, with ties rounded to even.
1095 ///
1096 /// See also
1097 /// <code>FixedI32::[saturating\_from\_ascii][FixedI32::saturating_from_ascii]</code>
1098 /// and
1099 /// <code>FixedU32::[saturating\_from\_ascii][FixedU32::saturating_from_ascii]</code>.
1100 fn saturating_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1101
1102 /// Parses an ASCII-byte slice containing binary digits to return a
1103 /// fixed-point number, saturating on overflow.
1104 ///
1105 /// Rounding is to the nearest, with ties rounded to even.
1106 ///
1107 /// See also
1108 /// <code>FixedI32::[saturating\_from\_ascii\_binary][FixedI32::saturating_from_ascii_binary]</code>
1109 /// and
1110 /// <code>FixedU32::[saturating\_from\_ascii\_binary][FixedU32::saturating_from_ascii_binary]</code>.
1111 fn saturating_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1112
1113 /// Parses an ASCII-byte slice containing octal digits to return a
1114 /// fixed-point number, saturating on overflow.
1115 ///
1116 /// Rounding is to the nearest, with ties rounded to even.
1117 ///
1118 /// See also
1119 /// <code>FixedI32::[saturating\_from\_ascii\_octal][FixedI32::saturating_from_ascii_octal]</code>
1120 /// and
1121 /// <code>FixedU32::[saturating\_from\_ascii\_octal][FixedU32::saturating_from_ascii_octal]</code>.
1122 fn saturating_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1123
1124 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1125 /// fixed-point number, saturating on overflow.
1126 ///
1127 /// Rounding is to the nearest, with ties rounded to even.
1128 ///
1129 /// See also
1130 /// <code>FixedI32::[saturating\_from\_ascii\_hex][FixedI32::saturating_from_ascii_hex]</code>
1131 /// and
1132 /// <code>FixedU32::[saturating\_from\_ascii\_hex][FixedU32::saturating_from_ascii_hex]</code>.
1133 fn saturating_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1134
1135 /// Parses a string slice containing decimal digits to return a
1136 /// fixed-point number, wrapping on overflow.
1137 ///
1138 /// Rounding is to the nearest, with ties rounded to even.
1139 ///
1140 /// See also
1141 /// <code>FixedI32::[wrapping\_from\_str][FixedI32::wrapping_from_str]</code>
1142 /// and
1143 /// <code>FixedU32::[wrapping\_from\_str][FixedU32::wrapping_from_str]</code>.
1144 fn wrapping_from_str(src: &str) -> Result<Self, ParseFixedError>;
1145
1146 /// Parses a string slice containing binary digits to return a
1147 /// fixed-point number, wrapping on overflow.
1148 ///
1149 /// Rounding is to the nearest, with ties rounded to even.
1150 ///
1151 /// See also
1152 /// <code>FixedI32::[wrapping\_from\_str\_binary][FixedI32::wrapping_from_str_binary]</code>
1153 /// and
1154 /// <code>FixedU32::[wrapping\_from\_str\_binary][FixedU32::wrapping_from_str_binary]</code>.
1155 fn wrapping_from_str_binary(src: &str) -> Result<Self, ParseFixedError>;
1156
1157 /// Parses a string slice containing octal digits to return a
1158 /// fixed-point number, wrapping on overflow.
1159 ///
1160 /// Rounding is to the nearest, with ties rounded to even.
1161 ///
1162 /// See also
1163 /// <code>FixedI32::[wrapping\_from\_str\_octal][FixedI32::wrapping_from_str_octal]</code>
1164 /// and
1165 /// <code>FixedU32::[wrapping\_from\_str\_octal][FixedU32::wrapping_from_str_octal]</code>.
1166 fn wrapping_from_str_octal(src: &str) -> Result<Self, ParseFixedError>;
1167
1168 /// Parses a string slice containing hexadecimal digits to return a
1169 /// fixed-point number, wrapping on overflow.
1170 ///
1171 /// Rounding is to the nearest, with ties rounded to even.
1172 ///
1173 /// See also
1174 /// <code>FixedI32::[wrapping\_from\_str\_hex][FixedI32::wrapping_from_str_hex]</code>
1175 /// and
1176 /// <code>FixedU32::[wrapping\_from\_str\_hex][FixedU32::wrapping_from_str_hex]</code>.
1177 fn wrapping_from_str_hex(src: &str) -> Result<Self, ParseFixedError>;
1178
1179 /// Parses an ASCII-byte slice containing decimal digits to return a
1180 /// fixed-point number, wrapping on overflow.
1181 ///
1182 /// Rounding is to the nearest, with ties rounded to even.
1183 ///
1184 /// See also
1185 /// <code>FixedI32::[wrapping\_from\_ascii][FixedI32::wrapping_from_ascii]</code>
1186 /// and
1187 /// <code>FixedU32::[wrapping\_from\_ascii][FixedU32::wrapping_from_ascii]</code>.
1188 fn wrapping_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>;
1189
1190 /// Parses an ASCII-byte slice containing binary digits to return a
1191 /// fixed-point number, wrapping on overflow.
1192 ///
1193 /// Rounding is to the nearest, with ties rounded to even.
1194 ///
1195 /// See also
1196 /// <code>FixedI32::[wrapping\_from\_ascii\_binary][FixedI32::wrapping_from_ascii_binary]</code>
1197 /// and
1198 /// <code>FixedU32::[wrapping\_from\_ascii\_binary][FixedU32::wrapping_from_ascii_binary]</code>.
1199 fn wrapping_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>;
1200
1201 /// Parses an ASCII-byte slice containing octal digits to return a
1202 /// fixed-point number, wrapping on overflow.
1203 ///
1204 /// Rounding is to the nearest, with ties rounded to even.
1205 ///
1206 /// See also
1207 /// <code>FixedI32::[wrapping\_from\_ascii\_octal][FixedI32::wrapping_from_ascii_octal]</code>
1208 /// and
1209 /// <code>FixedU32::[wrapping\_from\_ascii\_octal][FixedU32::wrapping_from_ascii_octal]</code>.
1210 fn wrapping_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>;
1211
1212 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1213 /// fixed-point number, wrapping on overflow.
1214 ///
1215 /// Rounding is to the nearest, with ties rounded to even.
1216 ///
1217 /// See also
1218 /// <code>FixedI32::[wrapping\_from\_ascii\_hex][FixedI32::wrapping_from_ascii_hex]</code>
1219 /// and
1220 /// <code>FixedU32::[wrapping\_from\_ascii\_hex][FixedU32::wrapping_from_ascii_hex]</code>.
1221 fn wrapping_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>;
1222
1223 /// Parses a string slice containing decimal digits to return a
1224 /// fixed-point number, panicking on overflow.
1225 ///
1226 /// Rounding is to the nearest, with ties rounded to even.
1227 ///
1228 /// See also
1229 /// <code>FixedI32::[strict\_from\_str][FixedI32::strict_from_str]</code>
1230 /// and
1231 /// <code>FixedU32::[strict\_from\_str][FixedU32::strict_from_str]</code>.
1232 ///
1233 /// # Panics
1234 ///
1235 /// Panics if the value does not fit or if there is a parsing error.
1236 #[track_caller]
1237 fn strict_from_str(src: &str) -> Self;
1238
1239 /// Parses a string slice containing binary digits to return a
1240 /// fixed-point number, panicking on overflow.
1241 ///
1242 /// Rounding is to the nearest, with ties rounded to even.
1243 ///
1244 /// See also
1245 /// <code>FixedI32::[strict\_from\_str\_binary][FixedI32::strict_from_str_binary]</code>
1246 /// and
1247 /// <code>FixedU32::[strict\_from\_str\_binary][FixedU32::strict_from_str_binary]</code>.
1248 ///
1249 /// # Panics
1250 ///
1251 /// Panics if the value does not fit or if there is a parsing error.
1252 #[track_caller]
1253 fn strict_from_str_binary(src: &str) -> Self;
1254
1255 /// Parses a string slice containing octal digits to return a
1256 /// fixed-point number, panicking on overflow.
1257 ///
1258 /// Rounding is to the nearest, with ties rounded to even.
1259 ///
1260 /// See also
1261 /// <code>FixedI32::[strict\_from\_str\_octal][FixedI32::strict_from_str_octal]</code>
1262 /// and
1263 /// <code>FixedU32::[strict\_from\_str\_octal][FixedU32::strict_from_str_octal]</code>.
1264 ///
1265 /// # Panics
1266 ///
1267 /// Panics if the value does not fit or if there is a parsing error.
1268 #[track_caller]
1269 fn strict_from_str_octal(src: &str) -> Self;
1270
1271 /// Parses a string slice containing hexadecimal digits to return a
1272 /// fixed-point number, panicking on overflow.
1273 ///
1274 /// Rounding is to the nearest, with ties rounded to even.
1275 ///
1276 /// See also
1277 /// <code>FixedI32::[strict\_from\_str\_hex][FixedI32::strict_from_str_hex]</code>
1278 /// and
1279 /// <code>FixedU32::[strict\_from\_str\_hex][FixedU32::strict_from_str_hex]</code>.
1280 ///
1281 /// # Panics
1282 ///
1283 /// Panics if the value does not fit or if there is a parsing error.
1284 #[track_caller]
1285 fn strict_from_str_hex(src: &str) -> Self;
1286
1287 /// Parses an ASCII-byte slice containing decimal digits to return a
1288 /// fixed-point number, panicking on overflow.
1289 ///
1290 /// Rounding is to the nearest, with ties rounded to even.
1291 ///
1292 /// See also
1293 /// <code>FixedI32::[strict\_from\_ascii][FixedI32::strict_from_ascii]</code>
1294 /// and
1295 /// <code>FixedU32::[strict\_from\_ascii][FixedU32::strict_from_ascii]</code>.
1296 ///
1297 /// # Panics
1298 ///
1299 /// Panics if the value does not fit or if there is a parsing error.
1300 #[track_caller]
1301 fn strict_from_ascii(src: &[u8]) -> Self;
1302
1303 /// Parses an ASCII-byte slice containing binary digits to return a
1304 /// fixed-point number, panicking on overflow.
1305 ///
1306 /// Rounding is to the nearest, with ties rounded to even.
1307 ///
1308 /// See also
1309 /// <code>FixedI32::[strict\_from\_ascii\_binary][FixedI32::strict_from_ascii_binary]</code>
1310 /// and
1311 /// <code>FixedU32::[strict\_from\_ascii\_binary][FixedU32::strict_from_ascii_binary]</code>.
1312 ///
1313 /// # Panics
1314 ///
1315 /// Panics if the value does not fit or if there is a parsing error.
1316 #[track_caller]
1317 fn strict_from_ascii_binary(src: &[u8]) -> Self;
1318
1319 /// Parses an ASCII-byte slice containing octal digits to return a
1320 /// fixed-point number, panicking on overflow.
1321 ///
1322 /// Rounding is to the nearest, with ties rounded to even.
1323 ///
1324 /// See also
1325 /// <code>FixedI32::[strict\_from\_ascii\_octal][FixedI32::strict_from_ascii_octal]</code>
1326 /// and
1327 /// <code>FixedU32::[strict\_from\_ascii\_octal][FixedU32::strict_from_ascii_octal]</code>.
1328 ///
1329 /// # Panics
1330 ///
1331 /// Panics if the value does not fit or if there is a parsing error.
1332 #[track_caller]
1333 fn strict_from_ascii_octal(src: &[u8]) -> Self;
1334
1335 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1336 /// fixed-point number, panicking on overflow.
1337 ///
1338 /// Rounding is to the nearest, with ties rounded to even.
1339 ///
1340 /// See also
1341 /// <code>FixedI32::[strict\_from\_ascii\_hex][FixedI32::strict_from_ascii_hex]</code>
1342 /// and
1343 /// <code>FixedU32::[strict\_from\_ascii\_hex][FixedU32::strict_from_ascii_hex]</code>.
1344 ///
1345 /// # Panics
1346 ///
1347 /// Panics if the value does not fit or if there is a parsing error.
1348 #[track_caller]
1349 fn strict_from_ascii_hex(src: &[u8]) -> Self;
1350
1351 /// Parses a string slice containing decimal digits to return a
1352 /// fixed-point number.
1353 ///
1354 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1355 /// indicating whether an overflow has occurred. On overflow, the
1356 /// wrapped value is returned.
1357 ///
1358 /// Rounding is to the nearest, with ties rounded to even.
1359 ///
1360 /// See also
1361 /// <code>FixedI32::[overflowing\_from\_str][FixedI32::overflowing_from_str]</code>
1362 /// and
1363 /// <code>FixedU32::[overflowing\_from\_str][FixedU32::overflowing_from_str]</code>.
1364 fn overflowing_from_str(src: &str) -> Result<(Self, bool), ParseFixedError>;
1365
1366 /// Parses a string slice containing binary digits to return a
1367 /// fixed-point number.
1368 ///
1369 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1370 /// indicating whether an overflow has occurred. On overflow, the
1371 /// wrapped value is returned.
1372 ///
1373 /// Rounding is to the nearest, with ties rounded to even.
1374 ///
1375 /// See also
1376 /// <code>FixedI32::[overflowing\_from\_str\_binary][FixedI32::overflowing_from_str_binary]</code>
1377 /// and
1378 /// <code>FixedU32::[overflowing\_from\_str\_binary][FixedU32::overflowing_from_str_binary]</code>.
1379 fn overflowing_from_str_binary(src: &str) -> Result<(Self, bool), ParseFixedError>;
1380
1381 /// Parses a string slice containing octal digits to return a
1382 /// fixed-point number.
1383 ///
1384 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1385 /// indicating whether an overflow has occurred. On overflow, the
1386 /// wrapped value is returned.
1387 ///
1388 /// Rounding is to the nearest, with ties rounded to even.
1389 ///
1390 /// See also
1391 /// <code>FixedI32::[overflowing\_from\_str\_octal][FixedI32::overflowing_from_str_octal]</code>
1392 /// and
1393 /// <code>FixedU32::[overflowing\_from\_str\_octal][FixedU32::overflowing_from_str_octal]</code>.
1394 fn overflowing_from_str_octal(src: &str) -> Result<(Self, bool), ParseFixedError>;
1395
1396 /// Parses a string slice containing hexadecimal digits to return a
1397 /// fixed-point number.
1398 ///
1399 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1400 /// indicating whether an overflow has occurred. On overflow, the
1401 /// wrapped value is returned.
1402 ///
1403 /// Rounding is to the nearest, with ties rounded to even.
1404 ///
1405 /// See also
1406 /// <code>FixedI32::[overflowing\_from\_str\_hex][FixedI32::overflowing_from_str_hex]</code>
1407 /// and
1408 /// <code>FixedU32::[overflowing\_from\_str\_hex][FixedU32::overflowing_from_str_hex]</code>.
1409 fn overflowing_from_str_hex(src: &str) -> Result<(Self, bool), ParseFixedError>;
1410
1411 /// Parses an ASCII-byte slice containing decimal digits to return a
1412 /// fixed-point number.
1413 ///
1414 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1415 /// indicating whether an overflow has occurred. On overflow, the
1416 /// wrapped value is returned.
1417 ///
1418 /// Rounding is to the nearest, with ties rounded to even.
1419 ///
1420 /// See also
1421 /// <code>FixedI32::[overflowing\_from\_ascii][FixedI32::overflowing_from_ascii]</code>
1422 /// and
1423 /// <code>FixedU32::[overflowing\_from\_ascii][FixedU32::overflowing_from_ascii]</code>.
1424 fn overflowing_from_ascii(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1425
1426 /// Parses an ASCII-byte slice containing binary digits to return a
1427 /// fixed-point number.
1428 ///
1429 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1430 /// indicating whether an overflow has occurred. On overflow, the
1431 /// wrapped value is returned.
1432 ///
1433 /// Rounding is to the nearest, with ties rounded to even.
1434 ///
1435 /// See also
1436 /// <code>FixedI32::[overflowing\_from\_ascii\_binary][FixedI32::overflowing_from_ascii_binary]</code>
1437 /// and
1438 /// <code>FixedU32::[overflowing\_from\_ascii\_binary][FixedU32::overflowing_from_ascii_binary]</code>.
1439 fn overflowing_from_ascii_binary(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1440
1441 /// Parses an ASCII-byte slice containing octal digits to return a
1442 /// fixed-point number.
1443 ///
1444 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1445 /// indicating whether an overflow has occurred. On overflow, the
1446 /// wrapped value is returned.
1447 ///
1448 /// Rounding is to the nearest, with ties rounded to even.
1449 ///
1450 /// See also
1451 /// <code>FixedI32::[overflowing\_from\_ascii\_octal][FixedI32::overflowing_from_ascii_octal]</code>
1452 /// and
1453 /// <code>FixedU32::[overflowing\_from\_ascii\_octal][FixedU32::overflowing_from_ascii_octal]</code>.
1454 fn overflowing_from_ascii_octal(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1455
1456 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
1457 /// fixed-point number.
1458 ///
1459 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1460 /// indicating whether an overflow has occurred. On overflow, the
1461 /// wrapped value is returned.
1462 ///
1463 /// Rounding is to the nearest, with ties rounded to even.
1464 ///
1465 /// See also
1466 /// <code>FixedI32::[overflowing\_from\_ascii\_hex][FixedI32::overflowing_from_ascii_hex]</code>
1467 /// and
1468 /// <code>FixedU32::[overflowing\_from\_ascii\_hex][FixedU32::overflowing_from_ascii_hex]</code>.
1469 fn overflowing_from_ascii_hex(src: &[u8]) -> Result<(Self, bool), ParseFixedError>;
1470
1471 /// Returns the integer part.
1472 ///
1473 /// See also <code>FixedI32::[int][FixedI32::int]</code> and
1474 /// <code>FixedU32::[int][FixedU32::int]</code>.
1475 #[must_use]
1476 fn int(self) -> Self;
1477
1478 /// Returns the fractional part.
1479 ///
1480 /// See also <code>FixedI32::[frac][FixedI32::frac]</code> and
1481 /// <code>FixedU32::[frac][FixedU32::frac]</code>.
1482 #[must_use]
1483 fn frac(self) -> Self;
1484
1485 /// Rounds to the next integer towards 0.
1486 ///
1487 /// See also
1488 /// <code>FixedI32::[round\_to\_zero][FixedI32::round_to_zero]</code> and
1489 /// <code>FixedU32::[round\_to\_zero][FixedU32::round_to_zero]</code>.
1490 #[must_use]
1491 fn round_to_zero(self) -> Self;
1492
1493 /// Rounds to the next integer towards +∞.
1494 ///
1495 /// See also <code>FixedI32::[ceil][FixedI32::ceil]</code> and
1496 /// <code>FixedU32::[ceil][FixedU32::ceil]</code>.
1497 #[must_use]
1498 fn ceil(self) -> Self;
1499
1500 /// Rounds to the next integer towards −∞.
1501 ///
1502 /// See also <code>FixedI32::[floor][FixedI32::floor]</code> and
1503 /// <code>FixedU32::[floor][FixedU32::floor]</code>.
1504 #[must_use]
1505 fn floor(self) -> Self;
1506
1507 /// Rounds to the nearest integer, with ties rounded away from zero.
1508 ///
1509 /// See also <code>FixedI32::[round][FixedI32::round]</code> and
1510 /// <code>FixedU32::[round][FixedU32::round]</code>.
1511 #[must_use]
1512 fn round(self) -> Self;
1513
1514 /// Rounds to the nearest integer, with ties rounded to even.
1515 ///
1516 /// See also
1517 /// <code>FixedI32::[round\_ties\_even][FixedI32::round_ties_even]</code>
1518 /// and
1519 /// <code>FixedU32::[round\_ties\_even][FixedU32::round_ties_even]</code>.
1520 #[must_use]
1521 fn round_ties_even(self) -> Self;
1522
1523 /// Checked ceil. Rounds to the next integer towards +∞, returning
1524 /// [`None`] on overflow.
1525 ///
1526 /// See also <code>FixedI32::[checked\_ceil][FixedI32::checked_ceil]</code>
1527 /// and <code>FixedU32::[checked\_ceil][FixedU32::checked_ceil]</code>.
1528 fn checked_ceil(self) -> Option<Self>;
1529
1530 /// Checked floor. Rounds to the next integer towards −∞, returning
1531 /// [`None`] on overflow.
1532 ///
1533 /// See also
1534 /// <code>FixedI32::[checked\_floor][FixedI32::checked_floor]</code> and
1535 /// <code>FixedU32::[checked\_floor][FixedU32::checked_floor]</code>.
1536 fn checked_floor(self) -> Option<Self>;
1537
1538 /// Checked round. Rounds to the nearest integer, with ties
1539 /// rounded away from zero, returning [`None`] on overflow.
1540 ///
1541 /// See also
1542 /// <code>FixedI32::[checked\_round][FixedI32::checked_round]</code> and
1543 /// <code>FixedU32::[checked\_round][FixedU32::checked_round]</code>.
1544 fn checked_round(self) -> Option<Self>;
1545
1546 /// Checked round. Rounds to the nearest integer, with ties rounded to even,
1547 /// returning [`None`] on overflow.
1548 ///
1549 /// See also
1550 /// <code>FixedI32::[checked\_round\_ties\_even][FixedI32::checked_round_ties_even]</code>
1551 /// and
1552 /// <code>FixedU32::[checked\_round\_ties\_even][FixedU32::checked_round_ties_even]</code>.
1553 fn checked_round_ties_even(self) -> Option<Self>;
1554
1555 /// Saturating ceil. Rounds to the next integer towards +∞,
1556 /// saturating on overflow.
1557 ///
1558 /// See also
1559 /// <code>FixedI32::[saturating\_ceil][FixedI32::saturating_ceil]</code> and
1560 /// <code>FixedU32::[saturating\_ceil][FixedU32::saturating_ceil]</code>.
1561 #[must_use]
1562 fn saturating_ceil(self) -> Self;
1563
1564 /// Saturating floor. Rounds to the next integer towards −∞,
1565 /// saturating on overflow.
1566 ///
1567 /// See also
1568 /// <code>FixedI32::[saturating\_floor][FixedI32::saturating_floor]</code>
1569 /// and
1570 /// <code>FixedU32::[saturating\_floor][FixedU32::saturating_floor]</code>.
1571 #[must_use]
1572 fn saturating_floor(self) -> Self;
1573
1574 /// Saturating round. Rounds to the nearest integer, with ties
1575 /// rounded away from zero, and saturating on overflow.
1576 ///
1577 /// See also
1578 /// <code>FixedI32::[saturating\_round][FixedI32::saturating_round]</code>
1579 /// and
1580 /// <code>FixedU32::[saturating\_round][FixedU32::saturating_round]</code>.
1581 #[must_use]
1582 fn saturating_round(self) -> Self;
1583
1584 /// Saturating round. Rounds to the nearest integer, with ties rounded
1585 /// to_even, and saturating on overflow.
1586 ///
1587 /// See also
1588 /// <code>FixedI32::[saturating\_round\_ties\_even][FixedI32::saturating_round_ties_even]</code>
1589 /// and
1590 /// <code>FixedU32::[saturating\_round\_ties\_even][FixedU32::saturating_round_ties_even]</code>.
1591 #[must_use]
1592 fn saturating_round_ties_even(self) -> Self;
1593
1594 /// Wrapping ceil. Rounds to the next integer towards +∞, wrapping
1595 /// on overflow.
1596 ///
1597 /// See also
1598 /// <code>FixedI32::[wrapping\_ceil][FixedI32::wrapping_ceil]</code> and
1599 /// <code>FixedU32::[wrapping\_ceil][FixedU32::wrapping_ceil]</code>.
1600 #[must_use]
1601 fn wrapping_ceil(self) -> Self;
1602
1603 /// Wrapping floor. Rounds to the next integer towards −∞,
1604 /// wrapping on overflow.
1605 ///
1606 /// See also
1607 /// <code>FixedI32::[wrapping\_floor][FixedI32::wrapping_floor]</code> and
1608 /// <code>FixedU32::[wrapping\_floor][FixedU32::wrapping_floor]</code>.
1609 #[must_use]
1610 fn wrapping_floor(self) -> Self;
1611
1612 /// Wrapping round. Rounds to the next integer to the nearest,
1613 /// with ties rounded away from zero, and wrapping on overflow.
1614 ///
1615 /// See also
1616 /// <code>FixedI32::[wrapping\_round][FixedI32::wrapping_round]</code> and
1617 /// <code>FixedU32::[wrapping\_round][FixedU32::wrapping_round]</code>.
1618 #[must_use]
1619 fn wrapping_round(self) -> Self;
1620
1621 /// Wrapping round. Rounds to the next integer to the nearest, with ties
1622 /// rounded to even, and wrapping on overflow.
1623 ///
1624 /// See also
1625 /// <code>FixedI32::[wrapping\_round\_ties\_even][FixedI32::wrapping_round_ties_even]</code>
1626 /// and
1627 /// <code>FixedU32::[wrapping\_round\_ties\_even][FixedU32::wrapping_round_ties_even]</code>.
1628 #[must_use]
1629 fn wrapping_round_ties_even(self) -> Self;
1630
1631 /// Strict ceil. Rounds to the next integer towards +∞,
1632 /// panicking on overflow.
1633 ///
1634 /// See also <code>FixedI32::[strict\_ceil][FixedI32::strict_ceil]</code>
1635 /// and <code>FixedU32::[strict\_ceil][FixedU32::strict_ceil]</code>.
1636 ///
1637 /// # Panics
1638 ///
1639 /// Panics if the result does not fit.
1640 #[track_caller]
1641 #[must_use]
1642 fn strict_ceil(self) -> Self;
1643
1644 /// Strict floor. Rounds to the next integer towards −∞,
1645 /// panicking on overflow.
1646 ///
1647 /// See also <code>FixedI32::[strict\_floor][FixedI32::strict_floor]</code>
1648 /// and <code>FixedU32::[strict\_floor][FixedU32::strict_floor]</code>.
1649 ///
1650 /// # Panics
1651 ///
1652 /// Panics if the result does not fit.
1653 #[track_caller]
1654 #[must_use]
1655 fn strict_floor(self) -> Self;
1656
1657 /// Strict round. Rounds to the next integer to the nearest,
1658 /// with ties rounded away from zero, and panicking on overflow.
1659 ///
1660 /// See also <code>FixedI32::[strict\_round][FixedI32::strict_round]</code>
1661 /// and <code>FixedU32::[strict\_round][FixedU32::strict_round]</code>.
1662 ///
1663 /// # Panics
1664 ///
1665 /// Panics if the result does not fit.
1666 #[track_caller]
1667 #[must_use]
1668 fn strict_round(self) -> Self;
1669
1670 /// Strict round. Rounds to the next integer to the nearest, with ties
1671 /// rounded to even, and panicking on overflow.
1672 ///
1673 /// See also
1674 /// <code>FixedI32::[strict\_round\_ties\_even][FixedI32::strict_round_ties_even]</code>
1675 /// and
1676 /// <code>FixedU32::[strict\_round\_ties\_even][FixedU32::strict_round_ties_even]</code>.
1677 ///
1678 /// # Panics
1679 ///
1680 /// Panics if the result does not fit.
1681 #[track_caller]
1682 #[must_use]
1683 fn strict_round_ties_even(self) -> Self;
1684
1685 /// Overflowing ceil. Rounds to the next integer towards +∞.
1686 ///
1687 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1688 /// indicating whether an overflow has occurred. On overflow, the
1689 /// wrapped value is returned.
1690 ///
1691 /// See also
1692 /// <code>FixedI32::[overflowing\_ceil][FixedI32::overflowing_ceil]</code>
1693 /// and
1694 /// <code>FixedU32::[overflowing\_ceil][FixedU32::overflowing_ceil]</code>.
1695 fn overflowing_ceil(self) -> (Self, bool);
1696
1697 /// Overflowing floor. Rounds to the next integer towards −∞.
1698 ///
1699 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1700 /// indicating whether an overflow has occurred. On overflow, the
1701 /// wrapped value is returned.
1702 ///
1703 /// See also
1704 /// <code>FixedI32::[overflowing\_floor][FixedI32::overflowing_floor]</code>
1705 /// and
1706 /// <code>FixedU32::[overflowing\_floor][FixedU32::overflowing_floor]</code>.
1707 fn overflowing_floor(self) -> (Self, bool);
1708
1709 /// Overflowing round. Rounds to the next integer to the nearest,
1710 /// with ties rounded away from zero.
1711 ///
1712 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1713 /// indicating whether an overflow has occurred. On overflow, the
1714 /// wrapped value is returned.
1715 ///
1716 /// See also
1717 /// <code>FixedI32::[overflowing\_round][FixedI32::overflowing_round]</code>
1718 /// and
1719 /// <code>FixedU32::[overflowing\_round][FixedU32::overflowing_round]</code>.
1720 fn overflowing_round(self) -> (Self, bool);
1721
1722 /// Overflowing round. Rounds to the next integer to the nearest, with ties
1723 /// rounded to even.
1724 ///
1725 /// Returns a [tuple] of the fixed-point number and a [`bool`],
1726 /// indicating whether an overflow has occurred. On overflow, the
1727 /// wrapped value is returned.
1728 ///
1729 /// See also
1730 /// <code>FixedI32::[overflowing\_round\_ties\_even][FixedI32::overflowing_round_ties_even]</code>
1731 /// and
1732 /// <code>FixedU32::[overflowing\_round\_ties\_even][FixedU32::overflowing_round_ties_even]</code>.
1733 fn overflowing_round_ties_even(self) -> (Self, bool);
1734
1735 /// Returns the number of ones in the binary representation.
1736 ///
1737 /// See also <code>FixedI32::[count\_ones][FixedI32::count_ones]</code> and
1738 /// <code>FixedU32::[count\_ones][FixedU32::count_ones]</code>.
1739 #[doc(alias("popcount", "popcnt"))]
1740 fn count_ones(self) -> u32;
1741
1742 /// Returns the number of zeros in the binary representation.
1743 ///
1744 /// See also <code>FixedI32::[count\_zeros][FixedI32::count_zeros]</code>
1745 /// and <code>FixedU32::[count\_zeros][FixedU32::count_zeros]</code>.
1746 fn count_zeros(self) -> u32;
1747
1748 /// Returns the number of leading ones in the binary representation.
1749 ///
1750 /// See also <code>FixedI32::[leading\_ones][FixedI32::leading_ones]</code>
1751 /// and <code>FixedU32::[leading\_ones][FixedU32::leading_ones]</code>.
1752 fn leading_ones(self) -> u32;
1753
1754 /// Returns the number of leading zeros in the binary representation.
1755 ///
1756 /// See also
1757 /// <code>FixedI32::[leading\_zeros][FixedI32::leading_zeros]</code> and
1758 /// <code>FixedU32::[leading\_zeros][FixedU32::leading_zeros]</code>.
1759 fn leading_zeros(self) -> u32;
1760
1761 /// Returns the number of trailing ones in the binary representation.
1762 ///
1763 /// See also
1764 /// <code>FixedI32::[trailing\_ones][FixedI32::trailing_ones]</code> and
1765 /// <code>FixedU32::[trailing\_ones][FixedU32::trailing_ones]</code>.
1766 fn trailing_ones(self) -> u32;
1767
1768 /// Returns the number of trailing zeros in the binary representation.
1769 ///
1770 /// See also
1771 /// <code>FixedI32::[trailing\_zeros][FixedI32::trailing_zeros]</code> and
1772 /// <code>FixedU32::[trailing\_zeros][FixedU32::trailing_zeros]</code>.
1773 fn trailing_zeros(self) -> u32;
1774
1775 /// Integer base-2 logarithm, rounded down.
1776 ///
1777 /// See also <code>FixedI32::[int\_log2][FixedI32::int_log2]</code> and
1778 /// <code>FixedU32::[int\_log2][FixedU32::int_log2]</code>.
1779 ///
1780 /// # Panics
1781 ///
1782 /// Panics if the fixed-point number is ≤ 0.
1783 #[doc(alias("ilog2"))]
1784 fn int_log2(self) -> i32;
1785
1786 /// Integer base-10 logarithm, rounded down.
1787 ///
1788 /// See also <code>FixedI32::[int\_log10][FixedI32::int_log10]</code> and
1789 /// <code>FixedU32::[int\_log10][FixedU32::int_log10]</code>.
1790 ///
1791 /// # Panics
1792 ///
1793 /// Panics if the fixed-point number is ≤ 0.
1794 #[doc(alias("ilog10"))]
1795 fn int_log10(self) -> i32;
1796
1797 /// Integer logarithm to the specified base, rounded down.
1798 ///
1799 /// See also <code>FixedI32::[int\_log][FixedI32::int_log]</code> and
1800 /// <code>FixedU32::[int\_log][FixedU32::int_log]</code>.
1801 ///
1802 /// # Panics
1803 ///
1804 /// Panics if the fixed-point number is ≤ 0 or if the base is < 2.
1805 #[doc(alias("ilog"))]
1806 fn int_log(self, base: u32) -> i32;
1807
1808 /// Checked integer base-2 logarithm, rounded down. Returns the
1809 /// logarithm or [`None`] if the fixed-point number is ≤ 0.
1810 ///
1811 /// See also
1812 /// <code>FixedI32::[checked\_int\_log2][FixedI32::checked_int_log2]</code>
1813 /// and
1814 /// <code>FixedU32::[checked\_int\_log2][FixedU32::checked_int_log2]</code>.
1815 #[doc(alias("checked_ilog2"))]
1816 fn checked_int_log2(self) -> Option<i32>;
1817
1818 /// Checked integer base-10 logarithm, rounded down. Returns the
1819 /// logarithm or [`None`] if the fixed-point number is ≤ 0.
1820 ///
1821 /// See also
1822 /// <code>FixedI32::[checked\_int\_log10][FixedI32::checked_int_log10]</code>
1823 /// and
1824 /// <code>FixedU32::[checked\_int\_log10][FixedU32::checked_int_log10]</code>.
1825 #[doc(alias("checked_ilog10"))]
1826 fn checked_int_log10(self) -> Option<i32>;
1827
1828 /// Checked integer logarithm to the specified base, rounded down. Returns
1829 /// the logarithm, or [`None`] if the fixed-point number is ≤ 0 or if
1830 /// the base is < 2.
1831 ///
1832 /// See also
1833 /// <code>FixedI32::[checked\_int\_log][FixedI32::checked_int_log]</code>
1834 /// and
1835 /// <code>FixedU32::[checked\_int\_log][FixedU32::checked_int_log]</code>.
1836 #[doc(alias("checked_ilog"))]
1837 fn checked_int_log(self, base: u32) -> Option<i32>;
1838
1839 /// Reverses the order of the bits of the fixed-point number.
1840 ///
1841 /// See also <code>FixedI32::[reverse\_bits][FixedI32::reverse_bits]</code>
1842 /// and <code>FixedU32::[reverse\_bits][FixedU32::reverse_bits]</code>.
1843 #[must_use = "this returns the result of the operation, without modifying the original"]
1844 fn reverse_bits(self) -> Self;
1845
1846 /// Shifts to the left by `n` bits, wrapping the truncated bits to the right end.
1847 ///
1848 /// See also <code>FixedI32::[rotate\_left][FixedI32::rotate_left]</code>
1849 /// and <code>FixedU32::[rotate\_left][FixedU32::rotate_left]</code>.
1850 #[must_use = "this returns the result of the operation, without modifying the original"]
1851 fn rotate_left(self, n: u32) -> Self;
1852
1853 /// Shifts to the right by `n` bits, wrapping the truncated bits to the left end.
1854 ///
1855 /// See also <code>FixedI32::[rotate\_right][FixedI32::rotate_right]</code>
1856 /// and <code>FixedU32::[rotate\_right][FixedU32::rotate_right]</code>.
1857 #[must_use = "this returns the result of the operation, without modifying the original"]
1858 fn rotate_right(self, n: u32) -> Self;
1859
1860 /// Returns [`true`] if the number is zero.
1861 ///
1862 /// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
1863 /// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
1864 fn is_zero(self) -> bool;
1865
1866 /// Returns the distance from `self` to `other`.
1867 ///
1868 /// See also <code>FixedI32::[dist][FixedI32::dist]</code> and
1869 /// <code>FixedU32::[dist][FixedU32::dist]</code>.
1870 #[must_use = "this returns the result of the operation, without modifying the original"]
1871 fn dist(self, other: Self) -> Self;
1872
1873 /// Returns the absolute value of the difference between `self` and `other`
1874 /// using an unsigned type without any wrapping or panicking.
1875 ///
1876 /// See also <code>FixedI32::[abs\_diff][FixedI32::abs_diff]</code> and
1877 /// <code>FixedU32::[abs\_diff][FixedU32::abs_diff]</code>.
1878 #[must_use = "this returns the result of the operation, without modifying the original"]
1879 fn abs_diff(self, other: Self) -> Self::Unsigned;
1880
1881 /// Returns the mean of `self` and `other`.
1882 ///
1883 /// See also <code>FixedI32::[mean][FixedI32::mean]</code> and
1884 /// <code>FixedU32::[mean][FixedU32::mean]</code>.
1885 #[must_use = "this returns the result of the operation, without modifying the original"]
1886 fn mean(self, other: Self) -> Self;
1887
1888 /// Compute the hypotenuse of a right triange.
1889 ///
1890 /// See also <code>FixedI32::[hypot][FixedI32::hypot]</code> and
1891 /// <code>FixedU32::[hypot][FixedU32::hypot]</code>.
1892 #[must_use = "this returns the result of the operation, without modifying the original"]
1893 fn hypot(self, other: Self) -> Self;
1894
1895 /// Returns the reciprocal.
1896 ///
1897 /// See also <code>FixedI32::[recip][FixedI32::recip]</code> and
1898 /// <code>FixedU32::[recip][FixedU32::recip]</code>.
1899 ///
1900 /// # Panics
1901 ///
1902 /// Panics if `self` is zero.
1903 #[must_use]
1904 fn recip(self) -> Self;
1905
1906 /// Returns the next multiple of `other`.
1907 ///
1908 /// See also
1909 /// <code>FixedI32::[next\_multiple\_of][FixedI32::next_multiple_of]</code>
1910 /// and
1911 /// <code>FixedU32::[next\_multiple\_of][FixedU32::next_multiple_of]</code>.
1912 ///
1913 /// # Panics
1914 ///
1915 /// Panics if `other` is zero.
1916 #[must_use]
1917 fn next_multiple_of(self, other: Self) -> Self;
1918
1919 /// Multiply and add. Returns `self` × `mul` + `add`.
1920 ///
1921 /// Note that the inherent [`mul_add`] method is more flexible
1922 /// than this method and allows the `mul` parameter to have a
1923 /// fixed-point type like `self` but with a different number of
1924 /// fractional bits.
1925 ///
1926 /// [`mul_add`]: FixedI32::mul_add
1927 ///
1928 /// See also <code>FixedI32::[mul\_add][FixedI32::mul_add]</code> and
1929 /// <code>FixedU32::[mul\_add][FixedU32::mul_add]</code>.
1930 #[must_use = "this returns the result of the operation, without modifying the original"]
1931 fn mul_add(self, mul: Self, add: Self) -> Self;
1932
1933 /// Adds `self` to the product `a` × `b`.
1934 ///
1935 /// Note that the inherent [`add_prod`] method is more flexible than this
1936 /// method and allows the `a` and `b` parameters to have a fixed-point type
1937 /// like `self` but with a different number of fractional bits.
1938 ///
1939 /// [`add_prod`]: FixedI32::add_prod
1940 ///
1941 /// See also <code>FixedI32::[add\_prod][FixedI32::add_prod]</code> and
1942 /// <code>FixedU32::[add\_prod][FixedU32::add_prod]</code>.
1943 #[must_use]
1944 fn add_prod(self, a: Self, b: Self) -> Self;
1945
1946 /// Multiply and accumulate. Adds (`a` × `b`) to `self`.
1947 ///
1948 /// Note that the inherent [`mul_acc`] method is more flexible than this
1949 /// method and allows the `a` and `b` parameters to have a fixed-point type
1950 /// like `self` but with a different number of fractional bits.
1951 ///
1952 /// [`mul_acc`]: FixedI32::mul_acc
1953 ///
1954 /// See also <code>FixedI32::[mul\_acc][FixedI32::mul_acc]</code> and
1955 /// <code>FixedU32::[mul\_acc][FixedU32::mul_acc]</code>.
1956 fn mul_acc(&mut self, a: Self, b: Self);
1957
1958 /// Euclidean division by an integer.
1959 ///
1960 /// See also <code>FixedI32::[div\_euclid][FixedI32::div_euclid]</code> and
1961 /// <code>FixedU32::[div\_euclid][FixedU32::div_euclid]</code>.
1962 ///
1963 /// # Panics
1964 ///
1965 /// Panics if the divisor is zero or if the division results in overflow.
1966 #[must_use = "this returns the result of the operation, without modifying the original"]
1967 fn div_euclid(self, rhs: Self) -> Self;
1968
1969 /// Remainder for Euclidean division.
1970 ///
1971 /// See also <code>FixedI32::[rem\_euclid][FixedI32::rem_euclid]</code> and
1972 /// <code>FixedU32::[rem\_euclid][FixedU32::rem_euclid]</code>.
1973 ///
1974 /// # Panics
1975 ///
1976 /// Panics if the divisor is zero.
1977 #[must_use = "this returns the result of the operation, without modifying the original"]
1978 fn rem_euclid(self, rhs: Self) -> Self;
1979
1980 /// Euclidean division by an integer.
1981 ///
1982 /// See also
1983 /// <code>FixedI32::[div\_euclid\_int][FixedI32::div_euclid_int]</code> and
1984 /// <code>FixedU32::[div\_euclid\_int][FixedU32::div_euclid_int]</code>.
1985 ///
1986 /// # Panics
1987 ///
1988 /// Panics if the divisor is zero or if the division results in overflow.
1989 #[must_use = "this returns the result of the operation, without modifying the original"]
1990 fn div_euclid_int(self, rhs: Self::Bits) -> Self;
1991
1992 /// Remainder for Euclidean division by an integer.
1993 ///
1994 /// See also
1995 /// <code>FixedI32::[rem\_euclid\_int][FixedI32::rem_euclid_int]</code> and
1996 /// <code>FixedU32::[rem\_euclid\_int][FixedU32::rem_euclid_int]</code>.
1997 ///
1998 /// # Panics
1999 ///
2000 /// Panics if the divisor is zero or if the division results in overflow.
2001 #[must_use = "this returns the result of the operation, without modifying the original"]
2002 fn rem_euclid_int(self, rhs: Self::Bits) -> Self;
2003
2004 /// Unbounded shift left. Computes `self << rhs`, without bounding the value
2005 /// of `rhs`.
2006 ///
2007 /// See also
2008 /// <code>FixedI32::[unbounded\_shl][FixedI32::unbounded_shl]</code> and
2009 /// <code>FixedU32::[unbounded\_shl][FixedU32::unbounded_shl]</code>.
2010 #[must_use = "this returns the result of the operation, without modifying the original"]
2011 fn unbounded_shl(self, rhs: u32) -> Self;
2012
2013 /// Unbounded shift right. Computes `self >> rhs`, without bounding the
2014 /// value of `rhs`.
2015 ///
2016 /// See also
2017 /// <code>FixedI32::[unbounded\_shr][FixedI32::unbounded_shr]</code> and
2018 /// <code>FixedU32::[unbounded\_shr][FixedU32::unbounded_shr]</code>.
2019 #[must_use = "this returns the result of the operation, without modifying the original"]
2020 fn unbounded_shr(self, rhs: u32) -> Self;
2021
2022 /// Returns the square root.
2023 ///
2024 /// See also <code>FixedI32::[sqrt][FixedI32::sqrt]</code> and
2025 /// <code>FixedU32::[sqrt][FixedU32::sqrt]</code>.
2026 ///
2027 /// # Panics
2028 ///
2029 /// Panics if the number is negative.
2030 fn sqrt(self) -> Self;
2031
2032 /// Linear interpolation between `start` and `end`.
2033 ///
2034 /// See also <code>FixedI32::[lerp][FixedI32::lerp]</code> and
2035 /// <code>FixedU32::[lerp][FixedU32::lerp]</code>.
2036 #[must_use]
2037 fn lerp(self, start: Self, end: Self) -> Self;
2038
2039 /// Inverse linear interpolation between `start` and `end`.
2040 ///
2041 /// See also <code>FixedI32::[inv\_lerp][FixedI32::inv_lerp]</code> and
2042 /// <code>FixedU32::[inv\_lerp][FixedU32::inv_lerp]</code>.
2043 #[must_use]
2044 fn inv_lerp(self, start: Self, end: Self) -> Self;
2045
2046 /// Checked negation. Returns the negated value, or [`None`] on overflow.
2047 ///
2048 /// See also <code>FixedI32::[checked\_neg][FixedI32::checked_neg]</code>
2049 /// and <code>FixedU32::[checked\_neg][FixedU32::checked_neg]</code>.
2050 fn checked_neg(self) -> Option<Self>;
2051
2052 /// Checked addition. Returns the sum, or [`None`] on overflow.
2053 ///
2054 /// See also <code>FixedI32::[checked\_add][FixedI32::checked_add]</code>
2055 /// and <code>FixedU32::[checked\_add][FixedU32::checked_add]</code>.
2056 #[must_use = "this returns the result of the operation, without modifying the original"]
2057 fn checked_add(self, rhs: Self) -> Option<Self>;
2058
2059 /// Checked subtraction. Returns the difference, or [`None`] on overflow.
2060 ///
2061 /// See also <code>FixedI32::[checked\_sub][FixedI32::checked_sub]</code>
2062 /// and <code>FixedU32::[checked\_sub][FixedU32::checked_sub]</code>.
2063 #[must_use = "this returns the result of the operation, without modifying the original"]
2064 fn checked_sub(self, rhs: Self) -> Option<Self>;
2065
2066 /// Checked multiplication. Returns the product, or [`None`] on overflow.
2067 ///
2068 /// See also <code>FixedI32::[checked\_mul][FixedI32::checked_mul]</code>
2069 /// and <code>FixedU32::[checked\_mul][FixedU32::checked_mul]</code>.
2070 #[must_use = "this returns the result of the operation, without modifying the original"]
2071 fn checked_mul(self, rhs: Self) -> Option<Self>;
2072
2073 /// Checked division. Returns the quotient, or [`None`] if the
2074 /// divisor is zero or on overflow.
2075 ///
2076 /// See also <code>FixedI32::[checked\_div][FixedI32::checked_div]</code>
2077 /// and <code>FixedU32::[checked\_div][FixedU32::checked_div]</code>.
2078 #[must_use = "this returns the result of the operation, without modifying the original"]
2079 fn checked_div(self, rhs: Self) -> Option<Self>;
2080
2081 /// Checked remainder. Returns the remainder, or [`None`] if the
2082 /// divisor is zero.
2083 ///
2084 /// See also <code>FixedI32::[checked\_rem][FixedI32::checked_rem]</code>
2085 /// and <code>FixedU32::[checked\_rem][FixedU32::checked_rem]</code>.
2086 #[must_use = "this returns the result of the operation, without modifying the original"]
2087 fn checked_rem(self, rhs: Self) -> Option<Self>;
2088
2089 /// Checked reciprocal. Returns the reciprocal, or [`None`] if
2090 /// `self` is zero or on overflow.
2091 ///
2092 /// See also
2093 /// <code>FixedI32::[checked\_recip][FixedI32::checked_recip]</code> and
2094 /// <code>FixedU32::[checked\_recip][FixedU32::checked_recip]</code>.
2095 fn checked_recip(self) -> Option<Self>;
2096
2097 /// Checked next multiple of `other`. Returns the next multiple, or [`None`]
2098 /// if `other` is zero or on overflow.
2099 ///
2100 /// See also
2101 /// <code>FixedI32::[checked\_next\_multiple\_of][FixedI32::checked_next_multiple_of]</code>
2102 /// and
2103 /// <code>FixedU32::[checked\_next\_multiple\_of][FixedU32::checked_next_multiple_of]</code>.
2104 #[must_use]
2105 fn checked_next_multiple_of(self, other: Self) -> Option<Self>;
2106
2107 /// Checked multiply and add. Returns `self` × `mul` + `add`, or [`None`] on overflow.
2108 ///
2109 /// See also
2110 /// <code>FixedI32::[checked\_mul\_add][FixedI32::checked_mul_add]</code>
2111 /// and
2112 /// <code>FixedU32::[checked\_mul\_add][FixedU32::checked_mul_add]</code>.
2113 #[must_use = "this returns the result of the operation, without modifying the original"]
2114 fn checked_mul_add(self, mul: Self, add: Self) -> Option<Self>;
2115
2116 /// Adds `self` to the product `a` × `b`, returning [`None`] on overflow.
2117 ///
2118 /// See also
2119 /// <code>FixedI32::[checked\_add\_prod][FixedI32::checked_add_prod]</code>
2120 /// and
2121 /// <code>FixedU32::[checked\_add\_prod][FixedU32::checked_add_prod]</code>.
2122 #[must_use = "this `Option` may be a `None` variant indicating overflow, which should be handled"]
2123 fn checked_add_prod(self, a: Self, b: Self) -> Option<Self>;
2124
2125 /// Checked multiply and accumulate. Adds (`a` × `b`) to `self`, or returns
2126 /// [`None`] on overflow.
2127 ///
2128 /// See also
2129 /// <code>FixedI32::[checked\_mul\_acc][FixedI32::checked_mul_acc]</code>
2130 /// and
2131 /// <code>FixedU32::[checked\_mul\_acc][FixedU32::checked_mul_acc]</code>.
2132 #[must_use = "this `Option` may be a `None` variant indicating overflow, which should be handled"]
2133 fn checked_mul_acc(&mut self, a: Self, b: Self) -> Option<()>;
2134
2135 /// Checked remainder for Euclidean division. Returns the
2136 /// remainder, or [`None`] if the divisor is zero or the division
2137 /// results in overflow.
2138 ///
2139 /// See also
2140 /// <code>FixedI32::[checked\_div\_euclid][FixedI32::checked_div_euclid]</code>
2141 /// and
2142 /// <code>FixedU32::[checked\_div\_euclid][FixedU32::checked_div_euclid]</code>.
2143 #[must_use = "this returns the result of the operation, without modifying the original"]
2144 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
2145
2146 /// Checked remainder for Euclidean division. Returns the
2147 /// remainder, or [`None`] if the divisor is zero.
2148 ///
2149 /// See also
2150 /// <code>FixedI32::[checked\_rem\_euclid][FixedI32::checked_rem_euclid]</code>
2151 /// and
2152 /// <code>FixedU32::[checked\_rem\_euclid][FixedU32::checked_rem_euclid]</code>.
2153 #[must_use = "this returns the result of the operation, without modifying the original"]
2154 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
2155
2156 /// Checked multiplication by an integer. Returns the product, or
2157 /// [`None`] on overflow.
2158 ///
2159 /// See also
2160 /// <code>FixedI32::[checked\_mul\_int][FixedI32::checked_mul_int]</code>
2161 /// and
2162 /// <code>FixedU32::[checked\_mul\_int][FixedU32::checked_mul_int]</code>.
2163 #[must_use = "this returns the result of the operation, without modifying the original"]
2164 fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self>;
2165
2166 /// Checked division by an integer. Returns the quotient, or
2167 /// [`None`] if the divisor is zero or if the division results in
2168 /// overflow.
2169 ///
2170 /// See also
2171 /// <code>FixedI32::[checked\_div\_int][FixedI32::checked_div_int]</code>
2172 /// and
2173 /// <code>FixedU32::[checked\_div\_int][FixedU32::checked_div_int]</code>.
2174 #[must_use = "this returns the result of the operation, without modifying the original"]
2175 fn checked_div_int(self, rhs: Self::Bits) -> Option<Self>;
2176
2177 /// Checked fixed-point remainder for division by an integer.
2178 /// Returns the remainder, or [`None`] if the divisor is zero or
2179 /// if the division results in overflow.
2180 ///
2181 /// See also
2182 /// <code>FixedI32::[checked\_rem\_int][FixedI32::checked_rem_int]</code>
2183 /// and
2184 /// <code>FixedU32::[checked\_rem\_int][FixedU32::checked_rem_int]</code>.
2185 #[must_use = "this returns the result of the operation, without modifying the original"]
2186 fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self>;
2187
2188 /// Checked Euclidean division by an integer. Returns the
2189 /// quotient, or [`None`] if the divisor is zero or if the
2190 /// division results in overflow.
2191 ///
2192 /// See also
2193 /// <code>FixedI32::[checked\_div\_euclid\_int][FixedI32::checked_div_euclid_int]</code>
2194 /// and
2195 /// <code>FixedU32::[checked\_div\_euclid\_int][FixedU32::checked_div_euclid_int]</code>.
2196 #[must_use = "this returns the result of the operation, without modifying the original"]
2197 fn checked_div_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
2198
2199 /// Checked remainder for Euclidean division by an integer.
2200 /// Returns the remainder, or [`None`] if the divisor is zero or
2201 /// if the remainder results in overflow.
2202 ///
2203 /// See also
2204 /// <code>FixedI32::[checked\_rem\_euclid\_int][FixedI32::checked_rem_euclid_int]</code>
2205 /// and
2206 /// <code>FixedU32::[checked\_rem\_euclid\_int][FixedU32::checked_rem_euclid_int]</code>.
2207 #[must_use = "this returns the result of the operation, without modifying the original"]
2208 fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
2209
2210 /// Checked shift left. Returns the shifted number, or [`None`] if
2211 /// `rhs` ≥ the number of bits.
2212 ///
2213 /// See also <code>FixedI32::[checked\_shl][FixedI32::checked_shl]</code>
2214 /// and <code>FixedU32::[checked\_shl][FixedU32::checked_shl]</code>.
2215 #[must_use = "this returns the result of the operation, without modifying the original"]
2216 fn checked_shl(self, rhs: u32) -> Option<Self>;
2217
2218 /// Checked shift right. Returns the shifted number, or [`None`]
2219 /// if `rhs` ≥ the number of bits.
2220 ///
2221 /// See also <code>FixedI32::[checked\_shr][FixedI32::checked_shr]</code>
2222 /// and <code>FixedU32::[checked\_shr][FixedU32::checked_shr]</code>.
2223 #[must_use = "this returns the result of the operation, without modifying the original"]
2224 fn checked_shr(self, rhs: u32) -> Option<Self>;
2225
2226 /// Checked distance. Returns the distance from `self` to `other`, or
2227 /// [`None`] on overflow.
2228 ///
2229 /// See also <code>FixedI32::[checked\_dist][FixedI32::checked_dist]</code>
2230 /// and <code>FixedU32::[checked\_dist][FixedU32::checked_dist]</code>.
2231 #[must_use = "this returns the result of the operation, without modifying the original"]
2232 fn checked_dist(self, other: Self) -> Option<Self>;
2233
2234 /// Compute the hypotenuse of a right triange, returning [`None`] on
2235 /// overflow.
2236 ///
2237 /// See also
2238 /// <code>FixedI32::[checked\_hypot][FixedI32::checked_hypot]</code> and
2239 /// <code>FixedU32::[checked\_hypot][FixedU32::checked_hypot]</code>.
2240 #[must_use = "this returns the result of the operation, without modifying the original"]
2241 fn checked_hypot(self, other: Self) -> Option<Self>;
2242
2243 /// Checked square root. Returns [`None`] for negative numbers or on overflow.
2244 ///
2245 /// See also <code>FixedI32::[checked\_sqrt][FixedI32::checked_sqrt]</code>
2246 /// and <code>FixedU32::[checked\_sqrt][FixedU32::checked_sqrt]</code>.
2247 fn checked_sqrt(self) -> Option<Self>;
2248
2249 /// Checked linear interpolation between `start` and `end`. Returns [`None`]
2250 /// on overflow.
2251 ///
2252 /// See also <code>FixedI32::[checked\_lerp][FixedI32::checked_lerp]</code>
2253 /// and <code>FixedU32::[checked\_lerp][FixedU32::checked_lerp]</code>.
2254 fn checked_lerp(self, start: Self, end: Self) -> Option<Self>;
2255
2256 /// Checked inverse linear interpolation between `start` and `end`. Returns
2257 /// [`None`] when `start` = `end` or on overflow.
2258 ///
2259 /// See also
2260 /// <code>FixedI32::[checked\_inv\_lerp][FixedI32::checked_inv_lerp]</code>
2261 /// and
2262 /// <code>FixedU32::[checked\_inv\_lerp][FixedU32::checked_inv_lerp]</code>.
2263 fn checked_inv_lerp(self, start: Self, end: Self) -> Option<Self>;
2264
2265 /// Saturated negation. Returns the negated value, saturating on overflow.
2266 ///
2267 /// See also
2268 /// <code>FixedI32::[saturating\_neg][FixedI32::saturating_neg]</code> and
2269 /// <code>FixedU32::[saturating\_neg][FixedU32::saturating_neg]</code>.
2270 #[must_use]
2271 fn saturating_neg(self) -> Self;
2272
2273 /// Saturating addition. Returns the sum, saturating on overflow.
2274 ///
2275 /// See also
2276 /// <code>FixedI32::[saturating\_add][FixedI32::saturating_add]</code> and
2277 /// <code>FixedU32::[saturating\_add][FixedU32::saturating_add]</code>.
2278 #[must_use = "this returns the result of the operation, without modifying the original"]
2279 fn saturating_add(self, rhs: Self) -> Self;
2280
2281 /// Saturating subtraction. Returns the difference, saturating on overflow.
2282 ///
2283 /// See also
2284 /// <code>FixedI32::[saturating\_sub][FixedI32::saturating_sub]</code> and
2285 /// <code>FixedU32::[saturating\_sub][FixedU32::saturating_sub]</code>.
2286 #[must_use = "this returns the result of the operation, without modifying the original"]
2287 fn saturating_sub(self, rhs: Self) -> Self;
2288
2289 /// Saturating multiplication. Returns the product, saturating on overflow.
2290 ///
2291 /// See also
2292 /// <code>FixedI32::[saturating\_mul][FixedI32::saturating_mul]</code> and
2293 /// <code>FixedU32::[saturating\_mul][FixedU32::saturating_mul]</code>.
2294 #[must_use = "this returns the result of the operation, without modifying the original"]
2295 fn saturating_mul(self, rhs: Self) -> Self;
2296
2297 /// Saturating division. Returns the quotient, saturating on overflow.
2298 ///
2299 /// See also
2300 /// <code>FixedI32::[saturating\_div][FixedI32::saturating_div]</code> and
2301 /// <code>FixedU32::[saturating\_div][FixedU32::saturating_div]</code>.
2302 ///
2303 /// # Panics
2304 ///
2305 /// Panics if the divisor is zero.
2306 #[must_use = "this returns the result of the operation, without modifying the original"]
2307 fn saturating_div(self, rhs: Self) -> Self;
2308
2309 /// Saturating reciprocal.
2310 ///
2311 /// See also
2312 /// <code>FixedI32::[saturating\_recip][FixedI32::saturating_recip]</code>
2313 /// and
2314 /// <code>FixedU32::[saturating\_recip][FixedU32::saturating_recip]</code>.
2315 ///
2316 /// # Panics
2317 ///
2318 /// Panics if `self` is zero.
2319 #[must_use]
2320 fn saturating_recip(self) -> Self;
2321
2322 /// Saturating next multiple of `other`.
2323 ///
2324 /// See also
2325 /// <code>FixedI32::[saturating\_next\_multiple\_of][FixedI32::saturating_next_multiple_of]</code>
2326 /// and
2327 /// <code>FixedU32::[saturating\_next\_multiple\_of][FixedU32::saturating_next_multiple_of]</code>.
2328 ///
2329 /// # Panics
2330 ///
2331 /// Panics if `other` is zero.
2332 #[must_use]
2333 fn saturating_next_multiple_of(self, other: Self) -> Self;
2334
2335 /// Saturating multiply and add. Returns `self` × `mul` + `add`, saturating on overflow.
2336 ///
2337 /// See also
2338 /// <code>FixedI32::[saturating\_mul\_add][FixedI32::saturating_mul_add]</code>
2339 /// and
2340 /// <code>FixedU32::[saturating\_mul\_add][FixedU32::saturating_mul_add]</code>.
2341 #[must_use = "this returns the result of the operation, without modifying the original"]
2342 fn saturating_mul_add(self, mul: Self, add: Self) -> Self;
2343
2344 /// Adds `self` to the product `a` × `b`, saturating on overflow.
2345 ///
2346 /// See also
2347 /// <code>FixedI32::[saturating\_add\_prod][FixedI32::saturating_add_prod]</code>
2348 /// and
2349 /// <code>FixedU32::[saturating\_add\_prod][FixedU32::saturating_add_prod]</code>.
2350 #[must_use]
2351 fn saturating_add_prod(self, a: Self, b: Self) -> Self;
2352
2353 /// Saturating multiply and add. Adds (`a` × `b`) to `self`, saturating on overflow.
2354 ///
2355 /// See also
2356 /// <code>FixedI32::[saturating\_mul\_acc][FixedI32::saturating_mul_acc]</code>
2357 /// and
2358 /// <code>FixedU32::[saturating\_mul\_acc][FixedU32::saturating_mul_acc]</code>.
2359 fn saturating_mul_acc(&mut self, a: Self, b: Self);
2360
2361 /// Saturating Euclidean division. Returns the quotient, saturating on overflow.
2362 ///
2363 /// See also
2364 /// <code>FixedI32::[saturating\_div\_euclid][FixedI32::saturating_div_euclid]</code>
2365 /// and
2366 /// <code>FixedU32::[saturating\_div\_euclid][FixedU32::saturating_div_euclid]</code>.
2367 ///
2368 /// # Panics
2369 ///
2370 /// Panics if the divisor is zero.
2371 #[must_use = "this returns the result of the operation, without modifying the original"]
2372 fn saturating_div_euclid(self, rhs: Self) -> Self;
2373
2374 /// Saturating multiplication by an integer. Returns the product, saturating on overflow.
2375 ///
2376 /// See also
2377 /// <code>FixedI32::[saturating\_mul\_int][FixedI32::saturating_mul_int]</code>
2378 /// and
2379 /// <code>FixedU32::[saturating\_mul\_int][FixedU32::saturating_mul_int]</code>.
2380 #[must_use = "this returns the result of the operation, without modifying the original"]
2381 fn saturating_mul_int(self, rhs: Self::Bits) -> Self;
2382
2383 /// Saturating division by an integer. Returns the quotient, saturating on overflow.
2384 ///
2385 /// Overflow can only occur when dividing the minimum value by −1.
2386 ///
2387 /// See also
2388 /// <code>FixedI32::[saturating\_div\_int][FixedI32::saturating_div_int]</code>
2389 /// and
2390 /// <code>FixedU32::[saturating\_div\_int][FixedU32::saturating_div_int]</code>.
2391 ///
2392 /// # Panics
2393 ///
2394 /// Panics if the divisor is zero.
2395 #[must_use = "this returns the result of the operation, without modifying the original"]
2396 fn saturating_div_int(self, rhs: Self::Bits) -> Self;
2397
2398 /// Saturating Euclidean division by an integer. Returns the
2399 /// quotient, saturating on overflow.
2400 ///
2401 /// See also
2402 /// <code>FixedI32::[saturating\_div\_euclid\_int][FixedI32::saturating_div_euclid_int]</code>
2403 /// and
2404 /// <code>FixedU32::[saturating\_div\_euclid\_int][FixedU32::saturating_div_euclid_int]</code>.
2405 ///
2406 /// # Panics
2407 ///
2408 /// Panics if the divisor is zero.
2409 #[must_use = "this returns the result of the operation, without modifying the original"]
2410 fn saturating_div_euclid_int(self, rhs: Self::Bits) -> Self;
2411
2412 /// Saturating remainder for Euclidean division by an integer.
2413 /// Returns the remainder, saturating on overflow.
2414 ///
2415 /// See also
2416 /// <code>FixedI32::[saturating\_rem\_euclid\_int][FixedI32::saturating_rem_euclid_int]</code>
2417 /// and
2418 /// <code>FixedU32::[saturating\_rem\_euclid\_int][FixedU32::saturating_rem_euclid_int]</code>.
2419 ///
2420 /// # Panics
2421 ///
2422 /// Panics if the divisor is zero.
2423 #[must_use = "this returns the result of the operation, without modifying the original"]
2424 fn saturating_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2425
2426 /// Saturating distance. Returns the distance from `self` to `other`,
2427 /// saturating on overflow.
2428 ///
2429 /// See also
2430 /// <code>FixedI32::[saturating\_dist][FixedI32::saturating_dist]</code> and
2431 /// <code>FixedU32::[saturating\_dist][FixedU32::saturating_dist]</code>.
2432 #[must_use = "this returns the result of the operation, without modifying the original"]
2433 fn saturating_dist(self, other: Self) -> Self;
2434
2435 /// Compute the hypotenuse of a right triange, saturating on overflow.
2436 ///
2437 /// See also
2438 /// <code>FixedI32::[saturating\_hypot][FixedI32::saturating_hypot]</code>
2439 /// and
2440 /// <code>FixedU32::[saturating\_hypot][FixedU32::saturating_hypot]</code>.
2441 #[must_use = "this returns the result of the operation, without modifying the original"]
2442 fn saturating_hypot(self, other: Self) -> Self;
2443
2444 /// Returns the square root, saturating on overflow.
2445 ///
2446 /// See also
2447 /// <code>FixedI32::[saturating\_sqrt][FixedI32::saturating_sqrt]</code> and
2448 /// <code>FixedU32::[saturating\_sqrt][FixedU32::saturating_sqrt]</code>.
2449 ///
2450 /// # Panics
2451 ///
2452 /// Panics if the number is negative.
2453 fn saturating_sqrt(self) -> Self;
2454
2455 /// Linear interpolation between `start` and `end`, saturating on overflow.
2456 ///
2457 /// See also
2458 /// <code>FixedI32::[saturating\_lerp][FixedI32::saturating_lerp]</code> and
2459 /// <code>FixedU32::[saturating\_lerp][FixedU32::saturating_lerp]</code>.
2460 #[must_use]
2461 fn saturating_lerp(self, start: Self, end: Self) -> Self;
2462
2463 /// Inverse linear interpolation between `start` and `end`, saturating on overflow.
2464 ///
2465 /// See also
2466 /// <code>FixedI32::[saturating\_inv\_lerp][FixedI32::saturating_inv_lerp]</code>
2467 /// and
2468 /// <code>FixedU32::[saturating\_inv\_lerp][FixedU32::saturating_inv_lerp]</code>.
2469 #[must_use]
2470 fn saturating_inv_lerp(self, start: Self, end: Self) -> Self;
2471
2472 /// Wrapping negation. Returns the negated value, wrapping on overflow.
2473 ///
2474 /// See also <code>FixedI32::[wrapping\_neg][FixedI32::wrapping_neg]</code>
2475 /// and <code>FixedU32::[wrapping\_neg][FixedU32::wrapping_neg]</code>.
2476 #[must_use]
2477 fn wrapping_neg(self) -> Self;
2478
2479 /// Wrapping addition. Returns the sum, wrapping on overflow.
2480 ///
2481 /// See also <code>FixedI32::[wrapping\_add][FixedI32::wrapping_add]</code>
2482 /// and <code>FixedU32::[wrapping\_add][FixedU32::wrapping_add]</code>.
2483 #[must_use = "this returns the result of the operation, without modifying the original"]
2484 fn wrapping_add(self, rhs: Self) -> Self;
2485
2486 /// Wrapping subtraction. Returns the difference, wrapping on overflow.
2487 ///
2488 /// See also <code>FixedI32::[wrapping\_sub][FixedI32::wrapping_sub]</code>
2489 /// and <code>FixedU32::[wrapping\_sub][FixedU32::wrapping_sub]</code>.
2490 #[must_use = "this returns the result of the operation, without modifying the original"]
2491 fn wrapping_sub(self, rhs: Self) -> Self;
2492
2493 /// Wrapping multiplication. Returns the product, wrapping on overflow.
2494 ///
2495 /// See also <code>FixedI32::[wrapping\_mul][FixedI32::wrapping_mul]</code>
2496 /// and <code>FixedU32::[wrapping\_mul][FixedU32::wrapping_mul]</code>.
2497 #[must_use = "this returns the result of the operation, without modifying the original"]
2498 fn wrapping_mul(self, rhs: Self) -> Self;
2499
2500 /// Wrapping division. Returns the quotient, wrapping on overflow.
2501 ///
2502 /// See also <code>FixedI32::[wrapping\_div][FixedI32::wrapping_div]</code>
2503 /// and <code>FixedU32::[wrapping\_div][FixedU32::wrapping_div]</code>.
2504 ///
2505 /// # Panics
2506 ///
2507 /// Panics if the divisor is zero.
2508 #[must_use = "this returns the result of the operation, without modifying the original"]
2509 fn wrapping_div(self, rhs: Self) -> Self;
2510
2511 /// Wrapping reciprocal.
2512 ///
2513 /// See also
2514 /// <code>FixedI32::[wrapping\_recip][FixedI32::wrapping_recip]</code> and
2515 /// <code>FixedU32::[wrapping\_recip][FixedU32::wrapping_recip]</code>.
2516 ///
2517 /// # Panics
2518 ///
2519 /// Panics if `self` is zero.
2520 #[must_use]
2521 fn wrapping_recip(self) -> Self;
2522
2523 /// Wrapping next multiple of `other`.
2524 ///
2525 /// See also
2526 /// <code>FixedI32::[wrapping\_next\_multiple\_of][FixedI32::wrapping_next_multiple_of]</code>
2527 /// and
2528 /// <code>FixedU32::[wrapping\_next\_multiple\_of][FixedU32::wrapping_next_multiple_of]</code>.
2529 ///
2530 /// # Panics
2531 ///
2532 /// Panics if `other` is zero.
2533 #[must_use]
2534 fn wrapping_next_multiple_of(self, other: Self) -> Self;
2535
2536 /// Wrapping multiply and add. Returns `self` × `mul` + `add`, wrapping on overflow.
2537 ///
2538 /// See also
2539 /// <code>FixedI32::[wrapping\_mul\_add][FixedI32::wrapping_mul_add]</code>
2540 /// and
2541 /// <code>FixedU32::[wrapping\_mul\_add][FixedU32::wrapping_mul_add]</code>.
2542 #[must_use = "this returns the result of the operation, without modifying the original"]
2543 fn wrapping_mul_add(self, mul: Self, add: Self) -> Self;
2544
2545 /// Adds `self` to the product `a` × `b`, wrapping on overflow.
2546 ///
2547 /// See also
2548 /// <code>FixedI32::[wrapping\_add\_prod][FixedI32::wrapping_add_prod]</code>
2549 /// and
2550 /// <code>FixedU32::[wrapping\_add\_prod][FixedU32::wrapping_add_prod]</code>.
2551 #[must_use]
2552 fn wrapping_add_prod(self, a: Self, b: Self) -> Self;
2553
2554 /// Wrapping multiply and accumulate. Adds (`a` × `b`) to `self`, wrapping on overflow.
2555 ///
2556 /// See also
2557 /// <code>FixedI32::[wrapping\_mul\_acc][FixedI32::wrapping_mul_acc]</code>
2558 /// and
2559 /// <code>FixedU32::[wrapping\_mul\_acc][FixedU32::wrapping_mul_acc]</code>.
2560 fn wrapping_mul_acc(&mut self, a: Self, b: Self);
2561
2562 /// Wrapping Euclidean division. Returns the quotient, wrapping on overflow.
2563 ///
2564 /// See also
2565 /// <code>FixedI32::[wrapping\_div\_euclid][FixedI32::wrapping_div_euclid]</code>
2566 /// and
2567 /// <code>FixedU32::[wrapping\_div\_euclid][FixedU32::wrapping_div_euclid]</code>.
2568 ///
2569 /// # Panics
2570 ///
2571 /// Panics if the divisor is zero.
2572 #[must_use = "this returns the result of the operation, without modifying the original"]
2573 fn wrapping_div_euclid(self, rhs: Self) -> Self;
2574
2575 /// Wrapping multiplication by an integer. Returns the product, wrapping on overflow.
2576 ///
2577 /// See also
2578 /// <code>FixedI32::[wrapping\_mul\_int][FixedI32::wrapping_mul_int]</code>
2579 /// and
2580 /// <code>FixedU32::[wrapping\_mul\_int][FixedU32::wrapping_mul_int]</code>.
2581 #[must_use = "this returns the result of the operation, without modifying the original"]
2582 fn wrapping_mul_int(self, rhs: Self::Bits) -> Self;
2583
2584 /// Wrapping division by an integer. Returns the quotient, wrapping on overflow.
2585 ///
2586 /// Overflow can only occur when dividing the minimum value by −1.
2587 ///
2588 /// See also
2589 /// <code>FixedI32::[wrapping\_div\_int][FixedI32::wrapping_div_int]</code>
2590 /// and
2591 /// <code>FixedU32::[wrapping\_div\_int][FixedU32::wrapping_div_int]</code>.
2592 ///
2593 /// # Panics
2594 ///
2595 /// Panics if the divisor is zero.
2596 #[must_use = "this returns the result of the operation, without modifying the original"]
2597 fn wrapping_div_int(self, rhs: Self::Bits) -> Self;
2598
2599 /// Wrapping Euclidean division by an integer. Returns the
2600 /// quotient, wrapping on overflow.
2601 ///
2602 /// Overflow can only occur when dividing the minimum value by −1.
2603 ///
2604 /// See also
2605 /// <code>FixedI32::[wrapping\_div\_euclid\_int][FixedI32::wrapping_div_euclid_int]</code>
2606 /// and
2607 /// <code>FixedU32::[wrapping\_div\_euclid\_int][FixedU32::wrapping_div_euclid_int]</code>.
2608 ///
2609 /// # Panics
2610 ///
2611 /// Panics if the divisor is zero.
2612 #[must_use = "this returns the result of the operation, without modifying the original"]
2613 fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self;
2614
2615 /// Wrapping remainder for Euclidean division by an integer.
2616 /// Returns the remainder, wrapping on overflow.
2617 ///
2618 /// See also
2619 /// <code>FixedI32::[wrapping\_rem\_euclid\_int][FixedI32::wrapping_rem_euclid_int]</code>
2620 /// and
2621 /// <code>FixedU32::[wrapping\_rem\_euclid\_int][FixedU32::wrapping_rem_euclid_int]</code>.
2622 ///
2623 /// # Panics
2624 ///
2625 /// Panics if the divisor is zero.
2626 #[must_use = "this returns the result of the operation, without modifying the original"]
2627 fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2628
2629 /// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of
2630 /// bits, then shifts and returns the number.
2631 ///
2632 /// See also <code>FixedI32::[wrapping\_shl][FixedI32::wrapping_shl]</code>
2633 /// and <code>FixedU32::[wrapping\_shl][FixedU32::wrapping_shl]</code>.
2634 #[must_use = "this returns the result of the operation, without modifying the original"]
2635 fn wrapping_shl(self, rhs: u32) -> Self;
2636
2637 /// Wrapping shift right. Wraps `rhs` if `rhs` ≥ the number of
2638 /// bits, then shifts and returns the number.
2639 ///
2640 /// See also <code>FixedI32::[wrapping\_shr][FixedI32::wrapping_shr]</code>
2641 /// and <code>FixedU32::[wrapping\_shr][FixedU32::wrapping_shr]</code>.
2642 #[must_use = "this returns the result of the operation, without modifying the original"]
2643 fn wrapping_shr(self, rhs: u32) -> Self;
2644
2645 /// Wrapping distance. Returns the distance from `self` to `other`, wrapping
2646 /// on overflow.
2647 ///
2648 /// See also
2649 /// <code>FixedI32::[wrapping\_dist][FixedI32::wrapping_dist]</code> and
2650 /// <code>FixedU32::[wrapping\_dist][FixedU32::wrapping_dist]</code>.
2651 #[must_use = "this returns the result of the operation, without modifying the original"]
2652 fn wrapping_dist(self, other: Self) -> Self;
2653
2654 /// Compute the hypotenuse of a right triange, wrapping on overflow.
2655 ///
2656 /// See also
2657 /// <code>FixedI32::[wrapping\_hypot][FixedI32::wrapping_hypot]</code> and
2658 /// <code>FixedU32::[wrapping\_hypot][FixedU32::wrapping_hypot]</code>.
2659 #[must_use = "this returns the result of the operation, without modifying the original"]
2660 fn wrapping_hypot(self, other: Self) -> Self;
2661
2662 /// Returns the square root, wrapping on overflow.
2663 ///
2664 /// See also
2665 /// <code>FixedI32::[wrapping\_sqrt][FixedI32::wrapping_sqrt]</code> and
2666 /// <code>FixedU32::[wrapping\_sqrt][FixedU32::wrapping_sqrt]</code>.
2667 ///
2668 /// # Panics
2669 ///
2670 /// Panics if the number is negative.
2671 fn wrapping_sqrt(self) -> Self;
2672
2673 /// Linear interpolation between `start` and `end`, wrapping on overflow.
2674 ///
2675 /// See also
2676 /// <code>FixedI32::[wrapping\_lerp][FixedI32::wrapping_lerp]</code> and
2677 /// <code>FixedU32::[wrapping\_lerp][FixedU32::wrapping_lerp]</code>.
2678 #[must_use]
2679 fn wrapping_lerp(self, start: Self, end: Self) -> Self;
2680
2681 /// Inverse linear interpolation between `start` and `end`, wrapping on
2682 /// overflow.
2683 ///
2684 /// See also
2685 /// <code>FixedI32::[wrapping\_inv\_lerp][FixedI32::wrapping_inv_lerp]</code>
2686 /// and
2687 /// <code>FixedU32::[wrapping\_inv\_lerp][FixedU32::wrapping_inv_lerp]</code>.
2688 #[must_use]
2689 fn wrapping_inv_lerp(self, start: Self, end: Self) -> Self;
2690
2691 /// Strict negation. Returns the negated value, panicking on overflow.
2692 ///
2693 /// See also <code>FixedI32::[strict\_neg][FixedI32::strict_neg]</code> and
2694 /// <code>FixedU32::[strict\_neg][FixedU32::strict_neg]</code>.
2695 ///
2696 /// # Panics
2697 ///
2698 /// Panics if the result does not fit.
2699 #[track_caller]
2700 #[must_use]
2701 fn strict_neg(self) -> Self;
2702
2703 /// Strict addition. Returns the sum, panicking on overflow.
2704 ///
2705 /// See also <code>FixedI32::[strict\_add][FixedI32::strict_add]</code> and
2706 /// <code>FixedU32::[strict\_add][FixedU32::strict_add]</code>.
2707 ///
2708 /// # Panics
2709 ///
2710 /// Panics if the result does not fit.
2711 #[track_caller]
2712 #[must_use = "this returns the result of the operation, without modifying the original"]
2713 fn strict_add(self, rhs: Self) -> Self;
2714
2715 /// Strict subtraction. Returns the difference, panicking on overflow.
2716 ///
2717 /// See also <code>FixedI32::[strict\_sub][FixedI32::strict_sub]</code> and
2718 /// <code>FixedU32::[strict\_sub][FixedU32::strict_sub]</code>.
2719 ///
2720 /// # Panics
2721 ///
2722 /// Panics if the result does not fit.
2723 #[track_caller]
2724 #[must_use = "this returns the result of the operation, without modifying the original"]
2725 fn strict_sub(self, rhs: Self) -> Self;
2726
2727 /// Strict multiplication. Returns the product, panicking on overflow.
2728 ///
2729 /// See also <code>FixedI32::[strict\_mul][FixedI32::strict_mul]</code> and
2730 /// <code>FixedU32::[strict\_mul][FixedU32::strict_mul]</code>.
2731 ///
2732 /// # Panics
2733 ///
2734 /// Panics if the result does not fit.
2735 #[track_caller]
2736 #[must_use = "this returns the result of the operation, without modifying the original"]
2737 fn strict_mul(self, rhs: Self) -> Self;
2738
2739 /// Strict division. Returns the quotient, panicking on overflow.
2740 ///
2741 /// See also <code>FixedI32::[strict\_div][FixedI32::strict_div]</code> and
2742 /// <code>FixedU32::[strict\_div][FixedU32::strict_div]</code>.
2743 ///
2744 /// # Panics
2745 ///
2746 /// Panics if the divisor is zero or if the result does not fit.
2747 #[track_caller]
2748 #[must_use = "this returns the result of the operation, without modifying the original"]
2749 fn strict_div(self, rhs: Self) -> Self;
2750
2751 /// Strict remainder. Returns the quotient, panicking if the divisor is zero.
2752 ///
2753 /// See also <code>FixedI32::[strict\_rem][FixedI32::strict_rem]</code> and
2754 /// <code>FixedU32::[strict\_rem][FixedU32::strict_rem]</code>.
2755 ///
2756 /// # Panics
2757 ///
2758 /// Panics if the divisor is zero.
2759 #[track_caller]
2760 #[must_use = "this returns the result of the operation, without modifying the original"]
2761 fn strict_rem(self, rhs: Self) -> Self;
2762
2763 /// Strict reciprocal. Returns reciprocal, panicking on overflow.
2764 ///
2765 /// See also <code>FixedI32::[strict\_recip][FixedI32::strict_recip]</code>
2766 /// and <code>FixedU32::[strict\_recip][FixedU32::strict_recip]</code>.
2767 ///
2768 /// # Panics
2769 ///
2770 /// Panics if `self` is zero or on overflow.
2771 #[track_caller]
2772 #[must_use]
2773 fn strict_recip(self) -> Self;
2774
2775 /// Strict next multiple of `other`. Returns the next multiple, panicking
2776 /// on overflow.
2777 ///
2778 /// See also
2779 /// <code>FixedI32::[strict\_next\_multiple\_of][FixedI32::strict_next_multiple_of]</code>
2780 /// and
2781 /// <code>FixedU32::[strict\_next\_multiple\_of][FixedU32::strict_next_multiple_of]</code>.
2782 ///
2783 /// # Panics
2784 ///
2785 /// Panics if `other` is zero or on overflow.
2786 #[track_caller]
2787 #[must_use]
2788 fn strict_next_multiple_of(self, other: Self) -> Self;
2789
2790 /// Strict multiply and add. Returns `self` × `mul` + `add`, panicking on overflow.
2791 ///
2792 /// See also
2793 /// <code>FixedI32::[strict\_mul\_add][FixedI32::strict_mul_add]</code> and
2794 /// <code>FixedU32::[strict\_mul\_add][FixedU32::strict_mul_add]</code>.
2795 ///
2796 /// # Panics
2797 ///
2798 /// Panics if the result does not fit.
2799 #[track_caller]
2800 #[must_use = "this returns the result of the operation, without modifying the original"]
2801 fn strict_mul_add(self, mul: Self, add: Self) -> Self;
2802
2803 /// Adds `self` to the product `a` × `b`, panicking on overflow.
2804 ///
2805 /// See also
2806 /// <code>FixedI32::[strict\_add\_prod][FixedI32::strict_add_prod]</code>
2807 /// and
2808 /// <code>FixedU32::[strict\_add\_prod][FixedU32::strict_add_prod]</code>.
2809 ///
2810 /// # Panics
2811 ///
2812 /// Panics if the result does not fit.
2813 #[track_caller]
2814 #[must_use]
2815 fn strict_add_prod(self, a: Self, b: Self) -> Self;
2816
2817 /// Strict multiply and accumulate. Adds (`a` × `b`) to `self`, panicking on overflow.
2818 ///
2819 /// See also
2820 /// <code>FixedI32::[strict\_mul\_acc][FixedI32::strict_mul_acc]</code> and
2821 /// <code>FixedU32::[strict\_mul\_acc][FixedU32::strict_mul_acc]</code>.
2822 ///
2823 /// # Panics
2824 ///
2825 /// Panics if the result does not fit.
2826 #[track_caller]
2827 fn strict_mul_acc(&mut self, a: Self, b: Self);
2828
2829 /// Strict Euclidean division. Returns the quotient, panicking on overflow.
2830 ///
2831 /// See also
2832 /// <code>FixedI32::[strict\_div\_euclid][FixedI32::strict_div_euclid]</code>
2833 /// and
2834 /// <code>FixedU32::[strict\_div\_euclid][FixedU32::strict_div_euclid]</code>.
2835 ///
2836 /// # Panics
2837 ///
2838 /// Panics if the divisor is zero or if the result does not fit.
2839 #[track_caller]
2840 #[must_use = "this returns the result of the operation, without modifying the original"]
2841 fn strict_div_euclid(self, rhs: Self) -> Self;
2842
2843 /// Strict remainder for Euclidean division. Returns the
2844 /// remainder, panicking if the divisor is zero.
2845 ///
2846 /// See also
2847 /// <code>FixedI32::[strict\_rem\_euclid][FixedI32::strict_rem_euclid]</code>
2848 /// and
2849 /// <code>FixedU32::[strict\_rem\_euclid][FixedU32::strict_rem_euclid]</code>.
2850 ///
2851 /// # Panics
2852 ///
2853 /// Panics if the divisor is zero.
2854 #[track_caller]
2855 #[must_use = "this returns the result of the operation, without modifying the original"]
2856 fn strict_rem_euclid(self, rhs: Self) -> Self;
2857
2858 /// Strict multiplication by an integer. Returns the product, panicking on overflow.
2859 ///
2860 /// See also
2861 /// <code>FixedI32::[strict\_mul\_int][FixedI32::strict_mul_int]</code> and
2862 /// <code>FixedU32::[strict\_mul\_int][FixedU32::strict_mul_int]</code>.
2863 ///
2864 /// # Panics
2865 ///
2866 /// Panics if the result does not fit.
2867 #[track_caller]
2868 #[must_use = "this returns the result of the operation, without modifying the original"]
2869 fn strict_mul_int(self, rhs: Self::Bits) -> Self;
2870
2871 /// Strict division by an integer. Returns the quotient, panicking on overflow.
2872 ///
2873 /// Overflow can only occur when dividing the minimum value by −1.
2874 ///
2875 /// See also
2876 /// <code>FixedI32::[strict\_div\_int][FixedI32::strict_div_int]</code> and
2877 /// <code>FixedU32::[strict\_div\_int][FixedU32::strict_div_int]</code>.
2878 ///
2879 /// # Panics
2880 ///
2881 /// Panics if the divisor is zero or if the result does not fit.
2882 #[track_caller]
2883 #[must_use = "this returns the result of the operation, without modifying the original"]
2884 fn strict_div_int(self, rhs: Self::Bits) -> Self;
2885
2886 /// Strict remainder for division by an integer. Returns the
2887 /// remainder, panicking if the divisor is zero.
2888 ///
2889 /// See also
2890 /// <code>FixedI32::[strict\_rem\_int][FixedI32::strict_rem_int]</code> and
2891 /// <code>FixedU32::[strict\_rem\_int][FixedU32::strict_rem_int]</code>.
2892 ///
2893 /// # Panics
2894 ///
2895 /// Panics if the divisor is zero.
2896 #[track_caller]
2897 #[must_use = "this returns the result of the operation, without modifying the original"]
2898 fn strict_rem_int(self, rhs: Self::Bits) -> Self;
2899
2900 /// Strict Euclidean division by an integer. Returns the
2901 /// quotient, panicking on overflow.
2902 ///
2903 /// Overflow can only occur when dividing the minimum value by −1.
2904 ///
2905 /// See also
2906 /// <code>FixedI32::[strict\_div\_euclid\_int][FixedI32::strict_div_euclid_int]</code>
2907 /// and
2908 /// <code>FixedU32::[strict\_div\_euclid\_int][FixedU32::strict_div_euclid_int]</code>.
2909 ///
2910 /// # Panics
2911 ///
2912 /// Panics if the divisor is zero or if the result does not fit.
2913 #[track_caller]
2914 #[must_use = "this returns the result of the operation, without modifying the original"]
2915 fn strict_div_euclid_int(self, rhs: Self::Bits) -> Self;
2916
2917 /// Strict remainder for Euclidean division by an integer.
2918 /// Returns the remainder, panicking on overflow.
2919 ///
2920 /// See also
2921 /// <code>FixedI32::[strict\_rem\_euclid\_int][FixedI32::strict_rem_euclid_int]</code>
2922 /// and
2923 /// <code>FixedU32::[strict\_rem\_euclid\_int][FixedU32::strict_rem_euclid_int]</code>.
2924 ///
2925 /// # Panics
2926 ///
2927 /// Panics if the divisor is zero or if the result does not fit.
2928 #[track_caller]
2929 #[must_use = "this returns the result of the operation, without modifying the original"]
2930 fn strict_rem_euclid_int(self, rhs: Self::Bits) -> Self;
2931
2932 /// Strict shift left. Panics if `rhs` ≥ the number of bits.
2933 ///
2934 /// See also <code>FixedI32::[strict\_shl][FixedI32::strict_shl]</code> and
2935 /// <code>FixedU32::[strict\_shl][FixedU32::strict_shl]</code>.
2936 ///
2937 /// # Panics
2938 ///
2939 /// Panics if `rhs` ≥ the number of bits.
2940 #[track_caller]
2941 #[must_use = "this returns the result of the operation, without modifying the original"]
2942 fn strict_shl(self, rhs: u32) -> Self;
2943
2944 /// Strict shift right. Panics if `rhs` ≥ the number of bits.
2945 ///
2946 /// See also <code>FixedI32::[strict\_shr][FixedI32::strict_shr]</code> and
2947 /// <code>FixedU32::[strict\_shr][FixedU32::strict_shr]</code>.
2948 ///
2949 /// # Panics
2950 ///
2951 /// Panics if `rhs` ≥ the number of bits.
2952 #[track_caller]
2953 #[must_use = "this returns the result of the operation, without modifying the original"]
2954 fn strict_shr(self, rhs: u32) -> Self;
2955
2956 /// Strict distance. Returns the distance from `self` to `other`,
2957 /// panicking on overflow.
2958 ///
2959 /// # Panics
2960 ///
2961 /// Panics if the result does not fit.
2962 ///
2963 /// See also <code>FixedI32::[strict\_dist][FixedI32::strict_dist]</code>
2964 /// and <code>FixedU32::[strict\_dist][FixedU32::strict_dist]</code>.
2965 #[track_caller]
2966 #[must_use = "this returns the result of the operation, without modifying the original"]
2967 fn strict_dist(self, other: Self) -> Self;
2968
2969 /// Compute the hypotenuse of a right triange, panicking on overflow.
2970 ///
2971 /// # Panics
2972 ///
2973 /// Panics if the result does not fit.
2974 ///
2975 /// See also <code>FixedI32::[strict\_hypot][FixedI32::strict_hypot]</code>
2976 /// and <code>FixedU32::[strict\_hypot][FixedU32::strict_hypot]</code>.
2977 #[track_caller]
2978 #[must_use = "this returns the result of the operation, without modifying the original"]
2979 fn strict_hypot(self, other: Self) -> Self;
2980
2981 /// Returns the square root, panicking if the number is negative or on overflow.
2982 ///
2983 /// See also <code>FixedI32::[strict\_sqrt][FixedI32::strict_sqrt]</code>
2984 /// and <code>FixedU32::[strict\_sqrt][FixedU32::strict_sqrt]</code>.
2985 ///
2986 /// # Panics
2987 ///
2988 /// Panics if the number is negative or on overflow.
2989 fn strict_sqrt(self) -> Self;
2990
2991 /// Linear interpolation between `start` and `end`, panicking on overflow.
2992 ///
2993 /// # Panics
2994 ///
2995 /// Panics if the result does not fit.
2996 ///
2997 /// See also <code>FixedI32::[strict\_lerp][FixedI32::strict_lerp]</code>
2998 /// and <code>FixedU32::[strict\_lerp][FixedU32::strict_lerp]</code>.
2999 #[track_caller]
3000 #[must_use]
3001 fn strict_lerp(self, start: Self, end: Self) -> Self;
3002
3003 /// Inverse linear interpolation between `start` and `end`, panicking on overflow.
3004 ///
3005 /// # Panics
3006 ///
3007 /// Panics when `start` = `end` or when the results overflows.
3008 ///
3009 /// See also
3010 /// <code>FixedI32::[strict\_inv\_lerp][FixedI32::strict_inv_lerp]</code>
3011 /// and
3012 /// <code>FixedU32::[strict\_inv\_lerp][FixedU32::strict_inv_lerp]</code>.
3013 #[track_caller]
3014 #[must_use]
3015 fn strict_inv_lerp(self, start: Self, end: Self) -> Self;
3016
3017 /// Overflowing negation.
3018 ///
3019 /// Returns a [tuple] of the negated value and a [`bool`],
3020 /// indicating whether an overflow has occurred. On overflow, the
3021 /// wrapped value is returned.
3022 ///
3023 /// See also
3024 /// <code>FixedI32::[overflowing\_neg][FixedI32::overflowing_neg]</code> and
3025 /// <code>FixedU32::[overflowing\_neg][FixedU32::overflowing_neg]</code>.
3026 fn overflowing_neg(self) -> (Self, bool);
3027
3028 /// Overflowing addition.
3029 ///
3030 /// Returns a [tuple] of the sum and a [`bool`], indicating whether
3031 /// an overflow has occurred. On overflow, the wrapped value is
3032 /// returned.
3033 ///
3034 /// See also
3035 /// <code>FixedI32::[overflowing\_add][FixedI32::overflowing_add]</code> and
3036 /// <code>FixedU32::[overflowing\_add][FixedU32::overflowing_add]</code>.
3037 #[must_use = "this returns the result of the operation, without modifying the original"]
3038 fn overflowing_add(self, rhs: Self) -> (Self, bool);
3039
3040 /// Overflowing subtraction.
3041 ///
3042 /// Returns a [tuple] of the difference and a [`bool`], indicating
3043 /// whether an overflow has occurred. On overflow, the wrapped
3044 /// value is returned.
3045 ///
3046 /// See also
3047 /// <code>FixedI32::[overflowing\_sub][FixedI32::overflowing_sub]</code> and
3048 /// <code>FixedU32::[overflowing\_sub][FixedU32::overflowing_sub]</code>.
3049 #[must_use = "this returns the result of the operation, without modifying the original"]
3050 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
3051
3052 /// Overflowing multiplication.
3053 ///
3054 /// Returns a [tuple] of the product and a [`bool`], indicating
3055 /// whether an overflow has occurred. On overflow, the wrapped
3056 /// value is returned.
3057 ///
3058 /// See also
3059 /// <code>FixedI32::[overflowing\_mul][FixedI32::overflowing_mul]</code> and
3060 /// <code>FixedU32::[overflowing\_mul][FixedU32::overflowing_mul]</code>.
3061 #[must_use = "this returns the result of the operation, without modifying the original"]
3062 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
3063
3064 /// Overflowing division.
3065 ///
3066 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3067 /// whether an overflow has occurred. On overflow, the wrapped
3068 /// value is returned.
3069 ///
3070 /// See also
3071 /// <code>FixedI32::[overflowing\_div][FixedI32::overflowing_div]</code> and
3072 /// <code>FixedU32::[overflowing\_div][FixedU32::overflowing_div]</code>.
3073 ///
3074 /// # Panics
3075 ///
3076 /// Panics if the divisor is zero.
3077 #[must_use = "this returns the result of the operation, without modifying the original"]
3078 fn overflowing_div(self, rhs: Self) -> (Self, bool);
3079
3080 /// Overflowing reciprocal.
3081 ///
3082 /// Returns a [tuple] of the reciprocal of `self` and a [`bool`],
3083 /// indicating whether an overflow has occurred. On overflow, the
3084 /// wrapped value is returned.
3085 ///
3086 /// See also
3087 /// <code>FixedI32::[overflowing\_recip][FixedI32::overflowing_recip]</code>
3088 /// and
3089 /// <code>FixedU32::[overflowing\_recip][FixedU32::overflowing_recip]</code>.
3090 ///
3091 /// # Panics
3092 ///
3093 /// Panics if `self` is zero.
3094 fn overflowing_recip(self) -> (Self, bool);
3095
3096 /// Overflowing next multiple of `other`.
3097 ///
3098 /// Returns a [tuple] of the next multiple and a [`bool`], indicating
3099 /// whether an overflow has occurred. On overflow, the wrapped value is
3100 /// returned.
3101 ///
3102 /// See also
3103 /// <code>FixedI32::[overflowing\_next\_multiple\_of][FixedI32::overflowing_next_multiple_of]</code>
3104 /// and
3105 /// <code>FixedU32::[overflowing\_next\_multiple\_of][FixedU32::overflowing_next_multiple_of]</code>.
3106 ///
3107 /// # Panics
3108 ///
3109 /// Panics if `other` is zero.
3110 fn overflowing_next_multiple_of(self, other: Self) -> (Self, bool);
3111
3112 /// Overflowing multiply and add.
3113 ///
3114 /// Returns a [tuple] of `self` × `mul` + `add` and a [`bool`],
3115 /// indicating whether an overflow has occurred. On overflow, the
3116 /// wrapped value is returned.
3117 ///
3118 /// See also
3119 /// <code>FixedI32::[overflowing\_mul\_add][FixedI32::overflowing_mul_add]</code>
3120 /// and
3121 /// <code>FixedU32::[overflowing\_mul\_add][FixedU32::overflowing_mul_add]</code>.
3122 #[must_use = "this returns the result of the operation, without modifying the original"]
3123 fn overflowing_mul_add(self, mul: Self, add: Self) -> (Self, bool);
3124
3125 /// Adds `self` to the product `a` × `b`.
3126 ///
3127 /// Returns a [tuple] of the result and a [`bool`] indicating whether an
3128 /// overflow has occurred. On overflow, the wrapped value is returned.
3129 ///
3130 /// See also
3131 /// <code>FixedI32::[overflowing\_add\_prod][FixedI32::overflowing_add_prod]</code>
3132 /// and
3133 /// <code>FixedU32::[overflowing\_add\_prod][FixedU32::overflowing_add_prod]</code>.
3134 #[must_use = "this returns the result of the operation, without modifying the original"]
3135 fn overflowing_add_prod(self, a: Self, b: Self) -> (Self, bool);
3136
3137 /// Overflowing multiply and accumulate. Adds (`a` × `b`) to `self`,
3138 /// wrapping and returning [`true`] if overflow occurs.
3139 ///
3140 /// See also
3141 /// <code>FixedI32::[overflowing\_mul\_acc][FixedI32::overflowing_mul_acc]</code>
3142 /// and
3143 /// <code>FixedU32::[overflowing\_mul\_acc][FixedU32::overflowing_mul_acc]</code>.
3144 #[must_use = "this returns whether overflow occurs; use `wrapping_mul_acc` if the flag is not needed"]
3145 fn overflowing_mul_acc(&mut self, a: Self, b: Self) -> bool;
3146
3147 /// Overflowing Euclidean division.
3148 ///
3149 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3150 /// whether an overflow has occurred. On overflow, the wrapped
3151 /// value is returned.
3152 ///
3153 /// See also
3154 /// <code>FixedI32::[overflowing\_div\_euclid][FixedI32::overflowing_div_euclid]</code>
3155 /// and
3156 /// <code>FixedU32::[overflowing\_div\_euclid][FixedU32::overflowing_div_euclid]</code>.
3157 ///
3158 /// # Panics
3159 ///
3160 /// Panics if the divisor is zero.
3161 #[must_use = "this returns the result of the operation, without modifying the original"]
3162 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
3163
3164 /// Overflowing multiplication by an integer.
3165 ///
3166 /// Returns a [tuple] of the product and a [`bool`], indicating
3167 /// whether an overflow has occurred. On overflow, the wrapped
3168 /// value is returned.
3169 ///
3170 /// See also
3171 /// <code>FixedI32::[overflowing\_mul\_int][FixedI32::overflowing_mul_int]</code>
3172 /// and
3173 /// <code>FixedU32::[overflowing\_mul\_int][FixedU32::overflowing_mul_int]</code>.
3174 #[must_use = "this returns the result of the operation, without modifying the original"]
3175 fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool);
3176
3177 /// Overflowing division by an integer.
3178 ///
3179 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3180 /// whether an overflow has occurred. On overflow, the wrapped
3181 /// value is returned.
3182 ///
3183 /// See also
3184 /// <code>FixedI32::[overflowing\_div\_int][FixedI32::overflowing_div_int]</code>
3185 /// and
3186 /// <code>FixedU32::[overflowing\_div\_int][FixedU32::overflowing_div_int]</code>.
3187 ///
3188 /// # Panics
3189 ///
3190 /// Panics if the divisor is zero.
3191 #[must_use = "this returns the result of the operation, without modifying the original"]
3192 fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool);
3193
3194 /// Overflowing Euclidean division by an integer.
3195 ///
3196 /// Returns a [tuple] of the quotient and a [`bool`], indicating
3197 /// whether an overflow has occurred. On overflow, the wrapped
3198 /// value is returned.
3199 ///
3200 /// See also
3201 /// <code>FixedI32::[overflowing\_div\_euclid\_int][FixedI32::overflowing_div_euclid_int]</code>
3202 /// and
3203 /// <code>FixedU32::[overflowing\_div\_euclid\_int][FixedU32::overflowing_div_euclid_int]</code>.
3204 ///
3205 /// # Panics
3206 ///
3207 /// Panics if the divisor is zero.
3208 #[must_use = "this returns the result of the operation, without modifying the original"]
3209 fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
3210
3211 /// Overflowing remainder for Euclidean division by an integer.
3212 ///
3213 /// Returns a [tuple] of the remainder and a [`bool`], indicating
3214 /// whether an overflow has occurred. On overflow, the wrapped
3215 /// value is returned.
3216 ///
3217 /// See also
3218 /// <code>FixedI32::[overflowing\_rem\_euclid\_int][FixedI32::overflowing_rem_euclid_int]</code>
3219 /// and
3220 /// <code>FixedU32::[overflowing\_rem\_euclid\_int][FixedU32::overflowing_rem_euclid_int]</code>.
3221 ///
3222 /// # Panics
3223 ///
3224 /// Panics if the divisor is zero.
3225 #[must_use = "this returns the result of the operation, without modifying the original"]
3226 fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
3227
3228 /// Overflowing shift left.
3229 ///
3230 /// Returns a [tuple] of the shifted value and a [`bool`],
3231 /// indicating whether an overflow has occurred. On overflow, the
3232 /// wrapped value is returned.
3233 ///
3234 /// See also
3235 /// <code>FixedI32::[overflowing\_shl][FixedI32::overflowing_shl]</code> and
3236 /// <code>FixedU32::[overflowing\_shl][FixedU32::overflowing_shl]</code>.
3237 #[must_use = "this returns the result of the operation, without modifying the original"]
3238 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
3239
3240 /// Overflowing shift right.
3241 ///
3242 /// Returns a [tuple] of the shifted value and a [`bool`],
3243 /// indicating whether an overflow has occurred. On overflow, the
3244 /// wrapped value is returned.
3245 ///
3246 /// See also
3247 /// <code>FixedI32::[overflowing\_shr][FixedI32::overflowing_shr]</code> and
3248 /// <code>FixedU32::[overflowing\_shr][FixedU32::overflowing_shr]</code>.
3249 #[must_use = "this returns the result of the operation, without modifying the original"]
3250 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
3251
3252 /// Overflowing distance.
3253 ///
3254 /// Returns a [tuple] of the distance from `self` to `other` and a [`bool`],
3255 /// indicating whether an overflow has occurred. On overflow, the wrapped
3256 /// value is returned.
3257 ///
3258 /// See also
3259 /// <code>FixedI32::[overflowing\_dist][FixedI32::overflowing_dist]</code>
3260 /// and
3261 /// <code>FixedU32::[overflowing\_dist][FixedU32::overflowing_dist]</code>.
3262 #[must_use = "this returns the result of the operation, without modifying the original"]
3263 fn overflowing_dist(self, other: Self) -> (Self, bool);
3264
3265 /// Compute the hypotenuse of a right triange.
3266 ///
3267 /// Returns a [tuple] of the hypotenuse and a [`bool`], indicating whether
3268 /// an overflow has occurred. On overflow, the wrapped value is returned.
3269 ///
3270 /// See also
3271 /// <code>FixedI32::[overflowing\_hypot][FixedI32::overflowing_hypot]</code>
3272 /// and
3273 /// <code>FixedU32::[overflowing\_hypot][FixedU32::overflowing_hypot]</code>.
3274 #[must_use = "this returns the result of the operation, without modifying the original"]
3275 fn overflowing_hypot(self, other: Self) -> (Self, bool);
3276
3277 /// Compute the square root.
3278 ///
3279 /// Returns a [tuple] of the square root and a [`bool`], indicating whether
3280 /// an overflow has occurred. On overflow, the wrapped value is returned.
3281 ///
3282 /// See also
3283 /// <code>FixedI32::[overflowing\_sqrt][FixedI32::overflowing_sqrt]</code>
3284 /// and
3285 /// <code>FixedU32::[overflowing\_sqrt][FixedU32::overflowing_sqrt]</code>.
3286 ///
3287 /// # Panics
3288 ///
3289 /// Panics if the number is negative.
3290 fn overflowing_sqrt(self) -> (Self, bool);
3291
3292 /// Overflowing linear interpolation between `start` and `end`.
3293 ///
3294 /// Returns a [tuple] of the interpolated value and a [`bool`], indicating
3295 /// whether an overflow has occurred. On overflow, the wrapped value is
3296 /// returned.
3297 ///
3298 /// See also
3299 /// <code>FixedI32::[overflowing\_lerp][FixedI32::overflowing_lerp]</code>
3300 /// and
3301 /// <code>FixedU32::[overflowing\_lerp][FixedU32::overflowing_lerp]</code>.
3302 fn overflowing_lerp(self, start: Self, end: Self) -> (Self, bool);
3303
3304 /// Overflowing inverse linear interpolation between `start` and `end`.
3305 ///
3306 /// Returns a [tuple] of the computed value and a [`bool`], indicating
3307 /// whether an overflow has occurred. On overflow, the wrapped value is
3308 /// returned.
3309 ///
3310 /// See also
3311 /// <code>FixedI32::[overflowing\_inv\_lerp][FixedI32::overflowing_inv_lerp]</code>
3312 /// and
3313 /// <code>FixedU32::[overflowing\_inv\_lerp][FixedU32::overflowing_inv_lerp]</code>.
3314 fn overflowing_inv_lerp(self, start: Self, end: Self) -> (Self, bool);
3315
3316 /// Unchecked addition. Computes `self` + `rhs`, assuming
3317 /// overflow cannot occur.
3318 ///
3319 /// See also
3320 /// <code>FixedI32::[unchecked\_add][FixedI32::unchecked_add]</code> and
3321 /// <code>FixedU32::[unchecked\_add][FixedU32::unchecked_add]</code>.
3322 ///
3323 /// # Safety
3324 ///
3325 /// This results in undefined behavior when
3326 /// `self` + `rhs` \> [`MAX`][Self::MAX] or
3327 /// `self` + `rhs` \< [`MIN`][Self::MIN].
3328 unsafe fn unchecked_add(self, rhs: Self) -> Self;
3329
3330 /// Unchecked subtraction. Computes `self` − `rhs`, assuming
3331 /// overflow cannot occur.
3332 ///
3333 /// See also
3334 /// <code>FixedI32::[unchecked\_sub][FixedI32::unchecked_sub]</code> and
3335 /// <code>FixedU32::[unchecked\_sub][FixedU32::unchecked_sub]</code>.
3336 ///
3337 /// # Safety
3338 ///
3339 /// This results in undefined behavior when
3340 /// `self` − `rhs` \> [`MAX`][Self::MAX] or
3341 /// `self` − `rhs` \< [`MIN`][Self::MIN].
3342 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
3343
3344 /// Unchecked shift left. Shifts `self` by `rhs` bits to the left, assuming
3345 /// that `rhs` is less than the number of bits in `self.
3346 ///
3347 /// # Safety
3348 ///
3349 /// This results in undefined behavior if `rhs` is larger than or equal to
3350 /// the number of bits in `self`, that is when
3351 /// [`checked_shl`][Self::checked_shl] would return [`None`].
3352 unsafe fn unchecked_shl(self, rhs: u32) -> Self;
3353
3354 /// Unchecked shift right. Shifts `self` by `rhs` bits to the right,
3355 /// assuming that `rhs` is less than the number of bits in `self.
3356 ///
3357 /// # Safety
3358 ///
3359 /// This results in undefined behavior if `rhs` is larger than or equal to
3360 /// the number of bits in `self`, that is when
3361 /// [`checked_shr`][Self::checked_shr] would return [`None`].
3362 unsafe fn unchecked_shr(self, rhs: u32) -> Self;
3363
3364 /// Unchecked multiplication by an integer. Computes
3365 /// `self` × `rhs`, assuming overflow cannot occur.
3366 ///
3367 /// See also
3368 /// <code>FixedI32::[unchecked\_mul\_int][FixedI32::unchecked_mul_int]</code>
3369 /// and
3370 /// <code>FixedU32::[unchecked\_mul\_int][FixedU32::unchecked_mul_int]</code>.
3371 ///
3372 /// # Safety
3373 ///
3374 /// This results in undefined behavior when
3375 /// `self` × `rhs` \> [`MAX`][Self::MAX] or
3376 /// `self` × `rhs` \< [`MIN`][Self::MIN].
3377 unsafe fn unchecked_mul_int(self, rhs: Self::Bits) -> Self;
3378
3379 /// Rounds to the nearest integer, with ties rounded to even.
3380 #[must_use]
3381 #[deprecated(since = "1.28.0", note = "renamed to `round_ties_even`")]
3382 fn round_ties_to_even(self) -> Self {
3383 self.round_ties_even()
3384 }
3385
3386 /// Checked round. Rounds to the nearest integer, with ties rounded to even,
3387 /// returning [`None`] on overflow.
3388 #[deprecated(since = "1.28.0", note = "renamed to `checked_round_ties_even`")]
3389 fn checked_round_ties_to_even(self) -> Option<Self> {
3390 self.checked_round_ties_even()
3391 }
3392
3393 /// Saturating round. Rounds to the nearest integer, with ties rounded
3394 /// to_even, and saturating on overflow.
3395 #[must_use]
3396 #[deprecated(since = "1.28.0", note = "renamed to `saturating_round_ties_even`")]
3397 fn saturating_round_ties_to_even(self) -> Self {
3398 self.saturating_round_ties_even()
3399 }
3400
3401 /// Wrapping round. Rounds to the next integer to the nearest, with ties
3402 /// rounded to even, and wrapping on overflow.
3403 #[must_use]
3404 #[deprecated(since = "1.28.0", note = "renamed to `wrapping_round_ties_even`")]
3405 fn wrapping_round_ties_to_even(self) -> Self {
3406 self.wrapping_round_ties_even()
3407 }
3408
3409 /// Strict round. Rounds to the next integer to the nearest, with ties
3410 /// rounded to even, and panicking on overflow.
3411 ///
3412 /// # Panics
3413 ///
3414 /// Panics if the result does not fit.
3415 #[track_caller]
3416 #[must_use]
3417 #[deprecated(since = "1.28.0", note = "renamed to `unwrapped_round_ties_even`")]
3418 fn unwrapped_round_ties_to_even(self) -> Self {
3419 self.strict_round_ties_even()
3420 }
3421
3422 /// Overflowing round. Rounds to the next integer to the nearest, with ties
3423 /// rounded to even.
3424 ///
3425 /// Returns a [tuple] of the fixed-point number and a [`bool`], indicating
3426 /// whether an overflow has occurred. On overflow, the wrapped value is
3427 /// returned.
3428 #[deprecated(since = "1.28.0", note = "renamed to `overflowing_round_ties_even`")]
3429 fn overflowing_round_ties_to_even(self) -> (Self, bool) {
3430 self.overflowing_round_ties_even()
3431 }
3432
3433 /// Creates a fixed-point number from another number, panicking on overflow.
3434 ///
3435 /// # Panics
3436 ///
3437 /// Panics if the value does not fit.
3438 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_num`")]
3439 #[track_caller]
3440 fn unwrapped_from_num<Src: ToFixed>(src: Src) -> Self {
3441 Self::strict_from_num(src)
3442 }
3443
3444 /// Converts a fixed-point number to another number, panicking on overflow.
3445 ///
3446 /// # Panics
3447 ///
3448 /// Panics if the value does not fit.
3449 #[deprecated(since = "1.30.0", note = "renamed to `strict_to_num`")]
3450 #[track_caller]
3451 fn unwrapped_to_num<Dst: FromFixed>(self) -> Dst {
3452 self.strict_to_num()
3453 }
3454
3455 /// Parses a string slice containing decimal digits to return a
3456 /// fixed-point number, panicking on overflow.
3457 ///
3458 /// # Panics
3459 ///
3460 /// Panics if the value does not fit or if there is a parsing error.
3461 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_str`")]
3462 #[track_caller]
3463 fn unwrapped_from_str(src: &str) -> Self {
3464 Self::strict_from_str(src)
3465 }
3466
3467 /// Parses a string slice containing binary digits to return a
3468 /// fixed-point number, panicking on overflow.
3469 ///
3470 /// # Panics
3471 ///
3472 /// Panics if the value does not fit or if there is a parsing error.
3473 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_str_binary`")]
3474 #[track_caller]
3475 fn unwrapped_from_str_binary(src: &str) -> Self {
3476 Self::strict_from_str_binary(src)
3477 }
3478
3479 /// Parses a string slice containing octal digits to return a
3480 /// fixed-point number, panicking on overflow.
3481 ///
3482 /// # Panics
3483 ///
3484 /// Panics if the value does not fit or if there is a parsing error.
3485 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_str_octal`")]
3486 #[track_caller]
3487 fn unwrapped_from_str_octal(src: &str) -> Self {
3488 Self::strict_from_str_octal(src)
3489 }
3490
3491 /// Parses a string slice containing hexadecimal digits to return a
3492 /// fixed-point number, panicking on overflow.
3493 ///
3494 /// # Panics
3495 ///
3496 /// Panics if the value does not fit or if there is a parsing error.
3497 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_str_hex`")]
3498 #[track_caller]
3499 fn unwrapped_from_str_hex(src: &str) -> Self {
3500 Self::strict_from_str_hex(src)
3501 }
3502
3503 /// Parses an ASCII-byte slice containing decimal digits to return a
3504 /// fixed-point number, panicking on overflow.
3505 ///
3506 /// # Panics
3507 ///
3508 /// Panics if the value does not fit or if there is a parsing error.
3509 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_ascii`")]
3510 #[track_caller]
3511 fn unwrapped_from_ascii(src: &[u8]) -> Self {
3512 Self::strict_from_ascii(src)
3513 }
3514
3515 /// Parses an ASCII-byte slice containing binary digits to return a
3516 /// fixed-point number, panicking on overflow.
3517 ///
3518 /// # Panics
3519 ///
3520 /// Panics if the value does not fit or if there is a parsing error.
3521 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_ascii_binary`")]
3522 #[track_caller]
3523 fn unwrapped_from_ascii_binary(src: &[u8]) -> Self {
3524 Self::strict_from_ascii_binary(src)
3525 }
3526
3527 /// Parses an ASCII-byte slice containing octal digits to return a
3528 /// fixed-point number, panicking on overflow.
3529 ///
3530 /// # Panics
3531 ///
3532 /// Panics if the value does not fit or if there is a parsing error.
3533 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_ascii_octal`")]
3534 #[track_caller]
3535 fn unwrapped_from_ascii_octal(src: &[u8]) -> Self {
3536 Self::strict_from_ascii_octal(src)
3537 }
3538
3539 /// Parses an ASCII-byte slice containing hexadecimal digits to return a
3540 /// fixed-point number, panicking on overflow.
3541 ///
3542 /// # Panics
3543 ///
3544 /// Panics if the value does not fit or if there is a parsing error.
3545 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_ascii_hex`")]
3546 #[track_caller]
3547 fn unwrapped_from_ascii_hex(src: &[u8]) -> Self {
3548 Self::strict_from_ascii_hex(src)
3549 }
3550
3551 /// Strict ceil. Rounds to the next integer towards +∞,
3552 /// panicking on overflow.
3553 ///
3554 /// # Panics
3555 ///
3556 /// Panics if the result does not fit.
3557 #[deprecated(since = "1.30.0", note = "renamed to `strict_ceil`")]
3558 #[track_caller]
3559 #[must_use]
3560 fn unwrapped_ceil(self) -> Self {
3561 self.strict_ceil()
3562 }
3563
3564 /// Strict floor. Rounds to the next integer towards −∞,
3565 /// panicking on overflow.
3566 ///
3567 /// # Panics
3568 ///
3569 /// Panics if the result does not fit.
3570 #[deprecated(since = "1.30.0", note = "renamed to `strict_floor`")]
3571 #[track_caller]
3572 #[must_use]
3573 fn unwrapped_floor(self) -> Self {
3574 self.strict_floor()
3575 }
3576
3577 /// Strict round. Rounds to the next integer to the nearest,
3578 /// with ties rounded away from zero, and panicking on overflow.
3579 ///
3580 /// # Panics
3581 ///
3582 /// Panics if the result does not fit.
3583 #[deprecated(since = "1.30.0", note = "renamed to `strict_round`")]
3584 #[track_caller]
3585 #[must_use]
3586 fn unwrapped_round(self) -> Self {
3587 self.strict_round()
3588 }
3589
3590 /// Strict round. Rounds to the next integer to the nearest, with ties
3591 /// rounded to even, and panicking on overflow.
3592 ///
3593 /// # Panics
3594 ///
3595 /// Panics if the result does not fit.
3596 #[deprecated(since = "1.30.0", note = "renamed to `strict_round_ties_even`")]
3597 #[track_caller]
3598 #[must_use]
3599 fn unwrapped_round_ties_even(self) -> Self {
3600 self.strict_round_ties_even()
3601 }
3602
3603 /// Strict negation. Returns the negated value, panicking on overflow.
3604 ///
3605 /// # Panics
3606 ///
3607 /// Panics if the result does not fit.
3608 #[deprecated(since = "1.30.0", note = "renamed to `strict_neg`")]
3609 #[track_caller]
3610 #[must_use]
3611 fn unwrapped_neg(self) -> Self {
3612 self.strict_neg()
3613 }
3614
3615 /// Strict addition. Returns the sum, panicking on overflow.
3616 ///
3617 /// # Panics
3618 ///
3619 /// Panics if the result does not fit.
3620 #[deprecated(since = "1.30.0", note = "renamed to `strict_add`")]
3621 #[track_caller]
3622 #[must_use = "this returns the result of the operation, without modifying the original"]
3623 fn unwrapped_add(self, rhs: Self) -> Self {
3624 self.strict_add(rhs)
3625 }
3626
3627 /// Strict subtraction. Returns the difference, panicking on overflow.
3628 ///
3629 /// # Panics
3630 ///
3631 /// Panics if the result does not fit.
3632 #[deprecated(since = "1.30.0", note = "renamed to `strict_sub`")]
3633 #[track_caller]
3634 #[must_use = "this returns the result of the operation, without modifying the original"]
3635 fn unwrapped_sub(self, rhs: Self) -> Self {
3636 self.strict_sub(rhs)
3637 }
3638
3639 /// Strict multiplication. Returns the product, panicking on overflow.
3640 ///
3641 /// # Panics
3642 ///
3643 /// Panics if the result does not fit.
3644 #[deprecated(since = "1.30.0", note = "renamed to `strict_mul`")]
3645 #[track_caller]
3646 #[must_use = "this returns the result of the operation, without modifying the original"]
3647 fn unwrapped_mul(self, rhs: Self) -> Self {
3648 self.strict_mul(rhs)
3649 }
3650
3651 /// Strict division. Returns the quotient, panicking on overflow.
3652 ///
3653 /// # Panics
3654 ///
3655 /// Panics if the divisor is zero or if the result does not fit.
3656 #[deprecated(since = "1.30.0", note = "renamed to `strict_div`")]
3657 #[track_caller]
3658 #[must_use = "this returns the result of the operation, without modifying the original"]
3659 fn unwrapped_div(self, rhs: Self) -> Self {
3660 self.strict_div(rhs)
3661 }
3662
3663 /// Strict remainder. Returns the quotient, panicking if the divisor is zero.
3664 ///
3665 /// # Panics
3666 ///
3667 /// Panics if the divisor is zero.
3668 #[deprecated(since = "1.30.0", note = "renamed to `strict_rem`")]
3669 #[track_caller]
3670 #[must_use = "this returns the result of the operation, without modifying the original"]
3671 fn unwrapped_rem(self, rhs: Self) -> Self {
3672 self.strict_rem(rhs)
3673 }
3674
3675 /// Strict reciprocal. Returns reciprocal, panicking on overflow.
3676 ///
3677 /// # Panics
3678 ///
3679 /// Panics if `self` is zero or on overflow.
3680 #[deprecated(since = "1.30.0", note = "renamed to `strict_recip`")]
3681 #[track_caller]
3682 #[must_use]
3683 fn unwrapped_recip(self) -> Self {
3684 self.strict_recip()
3685 }
3686
3687 /// Strict next multiple of `other`. Returns the next multiple, panicking
3688 /// on overflow.
3689 ///
3690 /// # Panics
3691 ///
3692 /// Panics if `other` is zero or on overflow.
3693 #[deprecated(since = "1.30.0", note = "renamed to `strict_next_multiple_of`")]
3694 #[track_caller]
3695 #[must_use]
3696 fn unwrapped_next_multiple_of(self, other: Self) -> Self {
3697 self.strict_next_multiple_of(other)
3698 }
3699
3700 /// Strict multiply and add. Returns `self` × `mul` + `add`, panicking on overflow.
3701 ///
3702 /// # Panics
3703 ///
3704 /// Panics if the result does not fit.
3705 #[deprecated(since = "1.30.0", note = "renamed to `strict_mul_add`")]
3706 #[track_caller]
3707 #[must_use = "this returns the result of the operation, without modifying the original"]
3708 fn unwrapped_mul_add(self, mul: Self, add: Self) -> Self {
3709 self.strict_mul_add(mul, add)
3710 }
3711
3712 /// Adds `self` to the product `a` × `b`, panicking on overflow.
3713 ///
3714 /// # Panics
3715 ///
3716 /// Panics if the result does not fit.
3717 #[deprecated(since = "1.30.0", note = "renamed to `strict_add_prod`")]
3718 #[track_caller]
3719 #[must_use]
3720 fn unwrapped_add_prod(self, a: Self, b: Self) -> Self {
3721 self.strict_add_prod(a, b)
3722 }
3723
3724 /// Strict multiply and accumulate. Adds (`a` × `b`) to `self`, panicking on overflow.
3725 ///
3726 /// # Panics
3727 ///
3728 /// Panics if the result does not fit.
3729 #[deprecated(since = "1.30.0", note = "renamed to `strict_mul_acc`")]
3730 #[track_caller]
3731 fn unwrapped_mul_acc(&mut self, a: Self, b: Self) {
3732 self.strict_mul_acc(a, b)
3733 }
3734
3735 /// Strict Euclidean division. Returns the quotient, panicking on overflow.
3736 ///
3737 /// # Panics
3738 ///
3739 /// Panics if the divisor is zero or if the result does not fit.
3740 #[deprecated(since = "1.30.0", note = "renamed to `strict_div_euclid`")]
3741 #[track_caller]
3742 #[must_use = "this returns the result of the operation, without modifying the original"]
3743 fn unwrapped_div_euclid(self, rhs: Self) -> Self {
3744 self.strict_div_euclid(rhs)
3745 }
3746
3747 /// Strict remainder for Euclidean division. Returns the
3748 /// remainder, panicking if the divisor is zero.
3749 ///
3750 /// # Panics
3751 ///
3752 /// Panics if the divisor is zero.
3753 #[deprecated(since = "1.30.0", note = "renamed to `strict_rem_euclid`")]
3754 #[track_caller]
3755 #[must_use = "this returns the result of the operation, without modifying the original"]
3756 fn unwrapped_rem_euclid(self, rhs: Self) -> Self {
3757 self.strict_rem_euclid(rhs)
3758 }
3759
3760 /// Strict multiplication by an integer. Returns the product, panicking on overflow.
3761 ///
3762 /// # Panics
3763 ///
3764 /// Panics if the result does not fit.
3765 #[deprecated(since = "1.30.0", note = "renamed to `strict_mul_int`")]
3766 #[track_caller]
3767 #[must_use = "this returns the result of the operation, without modifying the original"]
3768 fn unwrapped_mul_int(self, rhs: Self::Bits) -> Self {
3769 self.strict_mul_int(rhs)
3770 }
3771
3772 /// Strict division by an integer. Returns the quotient, panicking on overflow.
3773 ///
3774 /// # Panics
3775 ///
3776 /// Panics if the divisor is zero or if the result does not fit.
3777 #[deprecated(since = "1.30.0", note = "renamed to `strict_div_int`")]
3778 #[track_caller]
3779 #[must_use = "this returns the result of the operation, without modifying the original"]
3780 fn unwrapped_div_int(self, rhs: Self::Bits) -> Self {
3781 self.strict_div_int(rhs)
3782 }
3783
3784 /// Strict remainder for division by an integer. Returns the
3785 /// remainder, panicking if the divisor is zero.
3786 ///
3787 /// # Panics
3788 ///
3789 /// Panics if the divisor is zero.
3790 #[deprecated(since = "1.30.0", note = "renamed to `strict_rem_int`")]
3791 #[track_caller]
3792 #[must_use = "this returns the result of the operation, without modifying the original"]
3793 fn unwrapped_rem_int(self, rhs: Self::Bits) -> Self {
3794 self.strict_rem_int(rhs)
3795 }
3796
3797 /// Strict Euclidean division by an integer. Returns the
3798 /// quotient, panicking on overflow.
3799 ///
3800 /// # Panics
3801 ///
3802 /// Panics if the divisor is zero or if the result does not fit.
3803 #[deprecated(since = "1.30.0", note = "renamed to `strict_div_euclid_int`")]
3804 #[track_caller]
3805 #[must_use = "this returns the result of the operation, without modifying the original"]
3806 fn unwrapped_div_euclid_int(self, rhs: Self::Bits) -> Self {
3807 self.strict_div_euclid_int(rhs)
3808 }
3809
3810 /// Strict remainder for Euclidean division by an integer.
3811 /// Returns the remainder, panicking on overflow.
3812 ///
3813 /// # Panics
3814 ///
3815 /// Panics if the divisor is zero or if the result does not fit.
3816 #[deprecated(since = "1.30.0", note = "renamed to `strict_rem_euclid_int`")]
3817 #[track_caller]
3818 #[must_use = "this returns the result of the operation, without modifying the original"]
3819 fn unwrapped_rem_euclid_int(self, rhs: Self::Bits) -> Self {
3820 self.strict_rem_euclid_int(rhs)
3821 }
3822
3823 /// Strict shift left. Panics if `rhs` ≥ the number of bits.
3824 ///
3825 /// # Panics
3826 ///
3827 /// Panics if `rhs` ≥ the number of bits.
3828 #[deprecated(since = "1.30.0", note = "renamed to `strict_shl`")]
3829 #[track_caller]
3830 #[must_use = "this returns the result of the operation, without modifying the original"]
3831 fn unwrapped_shl(self, rhs: u32) -> Self {
3832 self.strict_shl(rhs)
3833 }
3834
3835 /// Strict shift right. Panics if `rhs` ≥ the number of bits.
3836 ///
3837 /// # Panics
3838 ///
3839 /// Panics if `rhs` ≥ the number of bits.
3840 #[deprecated(since = "1.30.0", note = "renamed to `strict_shr`")]
3841 #[track_caller]
3842 #[must_use = "this returns the result of the operation, without modifying the original"]
3843 fn unwrapped_shr(self, rhs: u32) -> Self {
3844 self.strict_shr(rhs)
3845 }
3846
3847 /// Strict distance. Returns the distance from `self` to `other`,
3848 /// panicking on overflow.
3849 ///
3850 /// # Panics
3851 ///
3852 /// Panics if the result does not fit.
3853 ///
3854 /// See also
3855 /// <code>FixedI32::[unwrapped\_dist][FixedI32::unwrapped_dist]</code> and
3856 /// <code>FixedU32::[unwrapped\_dist][FixedU32::unwrapped_dist]</code>.
3857 #[deprecated(since = "1.30.0", note = "renamed to `strict_dist`")]
3858 #[track_caller]
3859 #[must_use = "this returns the result of the operation, without modifying the original"]
3860 fn unwrapped_dist(self, other: Self) -> Self {
3861 self.strict_dist(other)
3862 }
3863
3864 /// Compute the hypotenuse of a right triange, panicking on overflow.
3865 ///
3866 /// # Panics
3867 ///
3868 /// Panics if the result does not fit.
3869 #[deprecated(since = "1.30.0", note = "renamed to `strict_hypot`")]
3870 #[track_caller]
3871 #[must_use = "this returns the result of the operation, without modifying the original"]
3872 fn unwrapped_hypot(self, other: Self) -> Self {
3873 self.strict_hypot(other)
3874 }
3875
3876 /// Returns the square root, panicking if the number is negative or on overflow.
3877 ///
3878 /// # Panics
3879 ///
3880 /// Panics if the number is negative or on overflow.
3881 #[deprecated(since = "1.30.0", note = "renamed to `strict_sqrt`")]
3882 fn unwrapped_sqrt(self) -> Self {
3883 self.strict_sqrt()
3884 }
3885
3886 /// Linear interpolation between `start` and `end`, panicking on overflow.
3887 ///
3888 /// # Panics
3889 ///
3890 /// Panics if the result does not fit.
3891 #[deprecated(since = "1.30.0", note = "renamed to `strict_lerp`")]
3892 #[track_caller]
3893 #[must_use]
3894 fn unwrapped_lerp(self, start: Self, end: Self) -> Self {
3895 self.strict_lerp(start, end)
3896 }
3897
3898 /// Inverse linear interpolation between `start` and `end`, panicking on overflow.
3899 ///
3900 /// # Panics
3901 ///
3902 /// Panics when `start` = `end` or when the results overflows.
3903 #[deprecated(since = "1.30.0", note = "renamed to `strict_inv_lerp`")]
3904 #[track_caller]
3905 #[must_use]
3906 fn unwrapped_inv_lerp(self, start: Self, end: Self) -> Self {
3907 self.strict_inv_lerp(start, end)
3908 }
3909}
3910
3911/// This trait provides methods common to all signed fixed-point numbers.
3912///
3913/// Methods common to all fixed-point numbers including unsigned
3914/// fixed-point numbers are provided by the [`Fixed`] supertrait.
3915///
3916/// This trait is sealed and cannot be implemented for more types; it
3917/// is implemented for [`FixedI8`], [`FixedI16`], [`FixedI32`],
3918/// [`FixedI64`], and [`FixedI128`].
3919pub trait FixedSigned: Fixed
3920where
3921 Self: Neg<Output = Self>,
3922{
3923 /// Negative one if the fixed-point number can represent it, otherwise
3924 /// [`None`].
3925 const TRY_NEG_ONE: Option<Self>;
3926
3927 /// Returns the number of bits required to represent the value.
3928 ///
3929 /// See also <code>FixedI32::[signed\_bits][FixedI32::signed_bits]</code>.
3930 fn signed_bits(self) -> u32;
3931
3932 /// Returns [`true`] if the number is > 0.
3933 ///
3934 /// See also <code>FixedI32::[is\_positive][FixedI32::is_positive]</code>.
3935 fn is_positive(self) -> bool;
3936
3937 /// Returns [`true`] if the number is < 0.
3938 ///
3939 /// See also <code>FixedI32::[is\_negative][FixedI32::is_negative]</code>.
3940 fn is_negative(self) -> bool;
3941
3942 /// Returns the absolute value.
3943 ///
3944 /// See also <code>FixedI32::[abs][FixedI32::abs]</code>.
3945 #[must_use]
3946 fn abs(self) -> Self;
3947
3948 /// Returns the absolute value using an unsigned type without any
3949 /// wrapping or panicking.
3950 ///
3951 /// See also <code>FixedI32::[unsigned\_abs][FixedI32::unsigned_abs]</code>.
3952 fn unsigned_abs(self) -> Self::Unsigned;
3953
3954 /// Returns the distance from `self` to `other` using an unsigned type
3955 /// without any wrapping or panicking.
3956 ///
3957 /// See also
3958 /// <code>FixedI32::[unsigned\_dist][FixedI32::unsigned_dist]</code>.
3959 fn unsigned_dist(self, other: Self) -> Self::Unsigned;
3960
3961 /// Returns a number representing the sign of `self`.
3962 ///
3963 /// See also <code>FixedI32::[signum][FixedI32::signum]</code>.
3964 ///
3965 /// # Panics
3966 ///
3967 /// When debug assertions are enabled, this method panics
3968 /// * if the value is positive and the fixed-point number has
3969 /// zero or one integer bits such that it cannot hold the
3970 /// value 1.
3971 /// * if the value is negative and the fixed-point number has
3972 /// zero integer bits, such that it cannot hold the value −1.
3973 ///
3974 /// When debug assertions are not enabled, the wrapped value can
3975 /// be returned in those cases, but it is not considered a
3976 /// breaking change if in the future it panics; using this method
3977 /// when 1 and −1 cannot be represented is almost certainly a bug.
3978 #[must_use]
3979 fn signum(self) -> Self;
3980
3981 /// Addition with an unsigned fixed-point number.
3982 ///
3983 /// See also <code>FixedI32::[add\_unsigned][FixedI32::add_unsigned]</code>.
3984 #[must_use]
3985 fn add_unsigned(self, rhs: Self::Unsigned) -> Self;
3986
3987 /// Subtraction with an unsigned fixed-point number.
3988 ///
3989 /// See also <code>FixedI32::[sub\_unsigned][FixedI32::sub_unsigned]</code>.
3990 #[must_use]
3991 fn sub_unsigned(self, rhs: Self::Unsigned) -> Self;
3992
3993 /// Checked absolute value. Returns the absolute value, or [`None`] on overflow.
3994 ///
3995 /// Overflow can only occur when trying to find the absolute value of the minimum value.
3996 ///
3997 /// See also <code>FixedI32::[checked\_abs][FixedI32::checked_abs]</code>.
3998 fn checked_abs(self) -> Option<Self>;
3999
4000 /// Checked signum. Returns a number representing the sign of
4001 /// `self`, or [`None`] on overflow.
4002 ///
4003 /// Overflow can only occur
4004 /// * if the value is positive and the fixed-point number has zero
4005 /// or one integer bits such that it cannot hold the value 1.
4006 /// * if the value is negative and the fixed-point number has zero
4007 /// integer bits, such that it cannot hold the value −1.
4008 ///
4009 /// See also
4010 /// <code>FixedI32::[checked\_signum][FixedI32::checked_signum]</code>.
4011 fn checked_signum(self) -> Option<Self>;
4012
4013 /// Checked addition with an unsigned fixed-point number. Returns the sum,
4014 /// or [`None`] on overflow.
4015 ///
4016 /// See also
4017 /// <code>FixedI32::[checked\_add\_unsigned][FixedI32::checked_add_unsigned]</code>.
4018 #[must_use]
4019 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
4020
4021 /// Checked subtraction with an unsigned fixed-point number. Returns the
4022 /// difference, or [`None`] on overflow.
4023 ///
4024 /// See also <code>FixedI32::[checked\_sub\_unsigned][FixedI32::checked_sub_unsigned]</code>.
4025 #[must_use]
4026 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
4027
4028 /// Saturating absolute value. Returns the absolute value, saturating on overflow.
4029 ///
4030 /// Overflow can only occur when trying to find the absolute value of the minimum value.
4031 ///
4032 /// See also
4033 /// <code>FixedI32::[saturating\_abs][FixedI32::saturating_abs]</code>.
4034 #[must_use]
4035 fn saturating_abs(self) -> Self;
4036
4037 /// Saturating signum. Returns a number representing the sign of
4038 /// `self`, saturating on overflow.
4039 ///
4040 /// Overflow can only occur
4041 /// * if the value is positive and the fixed-point number has zero
4042 /// or one integer bits such that it cannot hold the value 1.
4043 /// * if the value is negative and the fixed-point number has zero
4044 /// integer bits, such that it cannot hold the value −1.
4045 ///
4046 /// See also
4047 /// <code>FixedI32::[saturating\_signum][FixedI32::saturating_signum]</code>.
4048 #[must_use]
4049 fn saturating_signum(self) -> Self;
4050
4051 /// Saturating addition with an unsigned fixed-point number. Returns the
4052 /// sum, saturating on overflow.
4053 ///
4054 /// See also
4055 /// <code>FixedI32::[saturating\_add\_unsigned][FixedI32::saturating_add_unsigned]</code>.
4056 #[must_use]
4057 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
4058
4059 /// Saturating subtraction with an unsigned fixed-point number. Returns the
4060 /// difference, saturating on overflow.
4061 ///
4062 /// See also
4063 /// <code>FixedI32::[saturating\_sub\_unsigned][FixedI32::saturating_sub_unsigned]</code>.
4064 #[must_use]
4065 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
4066
4067 /// Wrapping absolute value. Returns the absolute value, wrapping on overflow.
4068 ///
4069 /// Overflow can only occur when trying to find the absolute value of the minimum value.
4070 ///
4071 /// See also <code>FixedI32::[wrapping\_abs][FixedI32::wrapping_abs]</code>.
4072 #[must_use]
4073 fn wrapping_abs(self) -> Self;
4074
4075 /// Wrapping signum. Returns a number representing the sign of
4076 /// `self`, wrapping on overflow.
4077 ///
4078 /// Overflow can only occur
4079 /// * if the value is positive and the fixed-point number has zero
4080 /// or one integer bits such that it cannot hold the value 1.
4081 /// * if the value is negative and the fixed-point number has zero
4082 /// integer bits, such that it cannot hold the value −1.
4083 ///
4084 /// See also
4085 /// <code>FixedI32::[wrapping\_signum][FixedI32::wrapping_signum]</code>.
4086 #[must_use]
4087 fn wrapping_signum(self) -> Self;
4088
4089 /// Wrapping addition with an unsigned fixed-point number. Returns the sum,
4090 /// wrapping on overflow.
4091 ///
4092 /// See also
4093 /// <code>FixedI32::[wrapping\_add\_unsigned][FixedI32::wrapping_add_unsigned]</code>.
4094 #[must_use]
4095 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
4096
4097 /// Wrapping subtraction with an unsigned fixed-point number. Returns the
4098 /// difference, wrapping on overflow.
4099 ///
4100 /// See also
4101 /// <code>FixedI32::[wrapping\_sub\_unsigned][FixedI32::wrapping_sub_unsigned]</code>.
4102 #[must_use]
4103 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
4104
4105 /// Strict absolute value. Returns the absolute value, panicking on overflow.
4106 ///
4107 /// Overflow can only occur when trying to find the absolute value of the minimum value.
4108 ///
4109 /// See also
4110 /// <code>FixedI32::[strict\_abs][FixedI32::strict_abs]</code>.
4111 ///
4112 /// # Panics
4113 ///
4114 /// Panics if the result does not fit.
4115 #[track_caller]
4116 #[must_use]
4117 fn strict_abs(self) -> Self;
4118
4119 /// Strict signum. Returns a number representing the sign of
4120 /// `self`, panicking on overflow.
4121 ///
4122 /// Overflow can only occur
4123 /// * if the value is positive and the fixed-point number has zero
4124 /// or one integer bits such that it cannot hold the value 1.
4125 /// * if the value is negative and the fixed-point number has zero
4126 /// integer bits, such that it cannot hold the value −1.
4127 ///
4128 /// See also
4129 /// <code>FixedI32::[strict\_signum][FixedI32::strict_signum]</code>.
4130 ///
4131 /// # Panics
4132 ///
4133 /// Panics if the result does not fit.
4134 #[track_caller]
4135 #[must_use]
4136 fn strict_signum(self) -> Self;
4137
4138 /// Strict addition with an unsigned fixed-point number. Returns the sum,
4139 /// panicking on overflow.
4140 ///
4141 /// See also
4142 /// <code>FixedI32::[strict\_add\_unsigned][FixedI32::strict_add_unsigned]</code>.
4143 ///
4144 /// # Panics
4145 ///
4146 /// Panics if the result does not fit.
4147 #[track_caller]
4148 #[must_use]
4149 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
4150
4151 /// Strict subtraction with an unsigned fixed-point number. Returns the
4152 /// difference, panicking on overflow.
4153 ///
4154 /// See also
4155 /// <code>FixedI32::[strict\_sub\_unsigned][FixedI32::strict_sub_unsigned]</code>.
4156 ///
4157 /// # Panics
4158 ///
4159 /// Panics if the result does not fit.
4160 #[track_caller]
4161 #[must_use]
4162 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
4163
4164 /// Overflowing absolute value.
4165 ///
4166 /// Returns a [tuple] of the fixed-point number and a [`bool`],
4167 /// indicating whether an overflow has occurred. On overflow, the
4168 /// wrapped value is returned.
4169 ///
4170 /// See also
4171 /// <code>FixedI32::[overflowing\_abs][FixedI32::overflowing_abs]</code>.
4172 fn overflowing_abs(self) -> (Self, bool);
4173
4174 /// Overflowing signum.
4175 ///
4176 /// Returns a [tuple] of the signum and a [`bool`], indicating
4177 /// whether an overflow has occurred. On overflow, the wrapped
4178 /// value is returned.
4179 ///
4180 /// Overflow can only occur
4181 /// * if the value is positive and the fixed-point number has zero
4182 /// or one integer bits such that it cannot hold the value 1.
4183 /// * if the value is negative and the fixed-point number has zero
4184 /// integer bits, such that it cannot hold the value −1.
4185 ///
4186 /// See also
4187 /// <code>FixedI32::[overflowing\_signum][FixedI32::overflowing_signum]</code>.
4188 fn overflowing_signum(self) -> (Self, bool);
4189
4190 /// Overflowing addition with an unsigned fixed-point number.
4191 ///
4192 /// Returns a [tuple] of the sum and a [`bool`], indicating whether an
4193 /// overflow has occurred. On overflow, the wrapped value is returned.
4194 ///
4195 /// See also
4196 /// <code>FixedI32::[overflowing\_add\_unsigned][FixedI32::overflowing_add_unsigned]</code>.
4197 #[must_use]
4198 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
4199
4200 /// Overflowing subtraction with an unsigned fixed-point number.
4201 ///
4202 /// Returns a [tuple] of the difference and a [`bool`], indicating whether
4203 /// an overflow has occurred. On overflow, the wrapped value is returned.
4204 ///
4205 /// See also
4206 /// <code>FixedI32::[overflowing\_sub\_unsigned][FixedI32::overflowing_sub_unsigned]</code>.
4207 #[must_use]
4208 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
4209
4210 /// Unchecked negation. Computes −`self`, assuming overflow cannot
4211 /// occur.
4212 ///
4213 /// # Safety
4214 ///
4215 /// This results in undefined behavior when
4216 /// `self` = [`MIN`][Fixed::MIN], that is when
4217 /// [`checked_neg`][Fixed::checked_neg] would return [`None`].
4218 unsafe fn unchecked_neg(self) -> Self;
4219
4220 /// Strict absolute value. Returns the absolute value, panicking on overflow.
4221 ///
4222 /// # Panics
4223 ///
4224 /// Panics if the result does not fit.
4225 #[deprecated(since = "1.30.0", note = "renamed to `strict_abs`")]
4226 #[track_caller]
4227 #[must_use]
4228 fn unwrapped_abs(self) -> Self {
4229 self.strict_abs()
4230 }
4231
4232 /// Strict signum. Returns a number representing the sign of
4233 /// `self`, panicking on overflow.
4234 ///
4235 /// # Panics
4236 ///
4237 /// Panics if the result does not fit.
4238 #[deprecated(since = "1.30.0", note = "renamed to `strict_signum`")]
4239 #[track_caller]
4240 #[must_use]
4241 fn unwrapped_signum(self) -> Self {
4242 self.strict_signum()
4243 }
4244
4245 /// Strict addition with an unsigned fixed-point number. Returns the sum,
4246 /// panicking on overflow.
4247 ///
4248 /// # Panics
4249 ///
4250 /// Panics if the result does not fit.
4251 #[deprecated(since = "1.30.0", note = "renamed to `strict_add_unsigned`")]
4252 #[track_caller]
4253 #[must_use]
4254 fn unwrapped_add_unsigned(self, rhs: Self::Unsigned) -> Self {
4255 self.strict_add_unsigned(rhs)
4256 }
4257
4258 /// Strict subtraction with an unsigned fixed-point number. Returns the
4259 /// difference, panicking on overflow.
4260 ///
4261 /// # Panics
4262 ///
4263 /// Panics if the result does not fit.
4264 #[deprecated(since = "1.30.0", note = "renamed to `strict_sub_unsigned`")]
4265 #[track_caller]
4266 #[must_use]
4267 fn unwrapped_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
4268 self.strict_sub_unsigned(rhs)
4269 }
4270}
4271
4272/// This trait provides methods common to all unsigned fixed-point numbers.
4273///
4274/// Methods common to all fixed-point numbers including signed
4275/// fixed-point numbers are provided by the [`Fixed`] supertrait.
4276///
4277/// This trait is sealed and cannot be implemented for more types; it
4278/// is implemented for [`FixedU8`], [`FixedU16`], [`FixedU32`],
4279/// [`FixedU64`], and [`FixedU128`].
4280pub trait FixedUnsigned: Fixed
4281where
4282 Self: Div<<Self as Fixed>::NonZeroBits, Output = Self>,
4283 Self: DivAssign<<Self as Fixed>::NonZeroBits>,
4284{
4285 /// Returns the number of bits required to represent the value.
4286 ///
4287 /// See also
4288 /// <code>FixedU32::[significant\_bits][FixedU32::significant_bits]</code>.
4289 fn significant_bits(self) -> u32;
4290
4291 /// Returns [`true`] if the fixed-point number is
4292 /// 2<sup><i>k</i></sup> for some integer <i>k</i>.
4293 ///
4294 /// See also
4295 /// <code>FixedU32::[is\_power\_of\_two][FixedU32::is_power_of_two]</code>.
4296 fn is_power_of_two(self) -> bool;
4297
4298 /// Returns the highest one in the binary representation, or zero
4299 /// if `self` is zero.
4300 ///
4301 /// See also <code>FixedU32::[highest\_one][FixedU32::highest_one]</code>.
4302 #[must_use]
4303 fn highest_one(self) -> Self;
4304
4305 /// Returns the smallest power of two that is ≥ `self`.
4306 ///
4307 /// See also
4308 /// <code>FixedU32::[next\_power\_of\_two][FixedU32::next_power_of_two]</code>.
4309 #[must_use]
4310 fn next_power_of_two(self) -> Self;
4311
4312 /// Addition with an signed fixed-point number.
4313 ///
4314 /// See also <code>FixedU32::[add\_signed][FixedU32::add_signed]</code>.
4315 #[must_use]
4316 fn add_signed(self, rhs: Self::Signed) -> Self;
4317
4318 /// Subtraction with an signed fixed-point number.
4319 ///
4320 /// See also <code>FixedU32::[sub\_signed][FixedU32::sub_signed]</code>.
4321 #[must_use]
4322 fn sub_signed(self, rhs: Self::Signed) -> Self;
4323
4324 /// Returns the smallest power of two that is ≥ `self`, or [`None`] if the
4325 /// next power of two is too large to represent.
4326 ///
4327 /// See also
4328 /// <code>FixedU32::[checked\_next\_power\_of\_two][FixedU32::checked_next_power_of_two]</code>.
4329 fn checked_next_power_of_two(self) -> Option<Self>;
4330
4331 /// Checked addition with an signed fixed-point number. Returns the sum,
4332 /// or [`None`] on overflow.
4333 ///
4334 /// See also
4335 /// <code>FixedU32::[checked\_add\_signed][FixedU32::checked_add_signed]</code>.
4336 #[must_use]
4337 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
4338
4339 /// Checked subtraction with an signed fixed-point number. Returns the
4340 /// difference, or [`None`] on overflow.
4341 ///
4342 /// See also <code>FixedU32::[checked\_sub\_signed][FixedU32::checked_sub_signed]</code>.
4343 #[must_use]
4344 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
4345
4346 /// Saturating addition with an signed fixed-point number. Returns the
4347 /// sum, saturating on overflow.
4348 ///
4349 /// See also
4350 /// <code>FixedU32::[saturating\_add\_signed][FixedU32::saturating_add_signed]</code>.
4351 #[must_use]
4352 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
4353
4354 /// Saturating subtraction with an signed fixed-point number. Returns the
4355 /// difference, saturating on overflow.
4356 ///
4357 /// See also
4358 /// <code>FixedU32::[saturating\_sub\_signed][FixedU32::saturating_sub_signed]</code>.
4359 #[must_use]
4360 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
4361
4362 /// Returns the smallest power of two that is ≥ `self`, wrapping
4363 /// to 0 if the next power of two is too large to represent.
4364 ///
4365 /// See also
4366 /// <code>FixedU32::[wrapping\_next\_power\_of\_two][FixedU32::wrapping_next_power_of_two]</code>.
4367 #[must_use]
4368 fn wrapping_next_power_of_two(self) -> Self;
4369
4370 /// Wrapping addition with an signed fixed-point number. Returns the sum,
4371 /// wrapping on overflow.
4372 ///
4373 /// See also
4374 /// <code>FixedU32::[wrapping\_add\_signed][FixedU32::wrapping_add_signed]</code>.
4375 #[must_use]
4376 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
4377
4378 /// Wrapping subtraction with an signed fixed-point number. Returns the
4379 /// difference, wrapping on overflow.
4380 ///
4381 /// See also
4382 /// <code>FixedU32::[wrapping\_sub\_signed][FixedU32::wrapping_sub_signed]</code>.
4383 #[must_use]
4384 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
4385
4386 /// Returns the smallest power of two that is ≥ `self`, panicking
4387 /// if the next power of two is too large to represent.
4388 ///
4389 /// See also
4390 /// <code>FixedU32::[strict\_next\_power\_of\_two][FixedU32::strict_next_power_of_two]</code>.
4391 ///
4392 /// # Panics
4393 ///
4394 /// Panics if the result does not fit.
4395 #[track_caller]
4396 #[must_use]
4397 fn strict_next_power_of_two(self) -> Self;
4398
4399 /// Strict addition with an signed fixed-point number. Returns the sum,
4400 /// panicking on overflow.
4401 ///
4402 /// See also
4403 /// <code>FixedU32::[strict\_add\_signed][FixedU32::strict_add_signed]</code>.
4404 ///
4405 /// # Panics
4406 ///
4407 /// Panics if the result does not fit.
4408 #[track_caller]
4409 #[must_use]
4410 fn strict_add_signed(self, rhs: Self::Signed) -> Self;
4411
4412 /// Strict subtraction with an signed fixed-point number. Returns the
4413 /// difference, panicking on overflow.
4414 ///
4415 /// See also
4416 /// <code>FixedU32::[strict\_sub\_signed][FixedU32::strict_sub_signed]</code>.
4417 ///
4418 /// # Panics
4419 ///
4420 /// Panics if the result does not fit.
4421 #[track_caller]
4422 #[must_use]
4423 fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
4424
4425 /// Overflowing addition with an signed fixed-point number.
4426 ///
4427 /// Returns a [tuple] of the sum and a [`bool`], indicating whether an
4428 /// overflow has occurred. On overflow, the wrapped value is returned.
4429 ///
4430 /// See also
4431 /// <code>FixedU32::[overflowing\_add\_signed][FixedU32::overflowing_add_signed]</code>.
4432 #[must_use]
4433 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
4434
4435 /// Overflowing subtraction with an signed fixed-point number.
4436 ///
4437 /// Returns a [tuple] of the difference and a [`bool`], indicating whether
4438 /// an overflow has occurred. On overflow, the wrapped value is returned.
4439 ///
4440 /// See also
4441 /// <code>FixedU32::[overflowing\_sub\_signed][FixedU32::overflowing_sub_signed]</code>.
4442 #[must_use]
4443 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
4444
4445 /// Returns the smallest power of two that is ≥ `self`, panicking
4446 /// if the next power of two is too large to represent.
4447 ///
4448 /// # Panics
4449 ///
4450 /// Panics if the result does not fit.
4451 #[deprecated(since = "1.30.0", note = "renamed to `strict_next_power_of_two`")]
4452 #[track_caller]
4453 #[must_use]
4454 fn unwrapped_next_power_of_two(self) -> Self {
4455 self.strict_next_power_of_two()
4456 }
4457
4458 /// Strict addition with an signed fixed-point number. Returns the sum,
4459 /// panicking on overflow.
4460 ///
4461 /// # Panics
4462 ///
4463 /// Panics if the result does not fit.
4464 #[deprecated(since = "1.30.0", note = "renamed to `strict_add_signed`")]
4465 #[track_caller]
4466 #[must_use]
4467 fn unwrapped_add_signed(self, rhs: Self::Signed) -> Self {
4468 self.strict_add_signed(rhs)
4469 }
4470
4471 /// Strict subtraction with an signed fixed-point number. Returns the
4472 /// difference, panicking on overflow.
4473 ///
4474 /// # Panics
4475 ///
4476 /// Panics if the result does not fit.
4477 #[deprecated(since = "1.30.0", note = "renamed to `strict_sub_signed`")]
4478 #[track_caller]
4479 #[must_use]
4480 fn unwrapped_sub_signed(self, rhs: Self::Signed) -> Self {
4481 self.strict_sub_signed(rhs)
4482 }
4483}
4484
4485/// This trait provides lossless conversions that might be fallible.
4486///
4487/// This trait is implemented for conversions between integer
4488/// primitives, floating-point primitives and fixed-point numbers.
4489///
4490/// # Examples
4491///
4492/// ```rust
4493/// use fixed::traits::LosslessTryFrom;
4494/// use fixed::types::{I24F8, I4F12};
4495/// // original is 0x000001.23, lossless is 0x1.230
4496/// let original = I24F8::from_bits(0x0000_0123);
4497/// let lossless = I4F12::lossless_try_from(original);
4498/// assert_eq!(lossless, Some(I4F12::from_bits(0x1230)));
4499/// // too_large is 0x000012.34, 0x12.340 does not fit in I4F12
4500/// let too_large = I24F8::from_bits(0x0000_1234);
4501/// let overflow = I4F12::lossless_try_from(too_large);
4502/// assert_eq!(overflow, None);
4503/// ```
4504pub trait LosslessTryFrom<Src>: Sized {
4505 /// Performs the conversion.
4506 fn lossless_try_from(src: Src) -> Option<Self>;
4507}
4508
4509/// This trait provides lossless conversions that might be fallible.
4510/// This is the reciprocal of [`LosslessTryFrom`].
4511///
4512/// Usually [`LosslessTryFrom`] should be implemented instead of this
4513/// trait; there is a blanket implementation which provides this trait
4514/// when [`LosslessTryFrom`] is implemented (similar to [`Into`] and
4515/// [`From`]).
4516///
4517/// # Examples
4518///
4519/// ```rust
4520/// use fixed::traits::LosslessTryInto;
4521/// use fixed::types::{I24F8, I4F12};
4522/// // original is 0x000001.23, lossless is 0x1.230
4523/// let original = I24F8::from_bits(0x0000_0123);
4524/// let lossless: Option<I4F12> = original.lossless_try_into();
4525/// assert_eq!(lossless, Some(I4F12::from_bits(0x1230)));
4526/// // too_large is 0x000012.34, 0x12.340 does not fit in I4F12
4527/// let too_large = I24F8::from_bits(0x0000_1234);
4528/// let overflow: Option<I4F12> = too_large.lossless_try_into();
4529/// assert_eq!(overflow, None);
4530/// ```
4531pub trait LosslessTryInto<Dst> {
4532 /// Performs the conversion.
4533 fn lossless_try_into(self) -> Option<Dst>;
4534}
4535
4536impl<Src, Dst> LosslessTryInto<Dst> for Src
4537where
4538 Dst: LosslessTryFrom<Src>,
4539{
4540 fn lossless_try_into(self) -> Option<Dst> {
4541 Dst::lossless_try_from(self)
4542 }
4543}
4544
4545/// This trait provides infallible conversions that might be lossy.
4546///
4547/// This trait is implemented for conversions between integer
4548/// primitives, floating-point primitives and fixed-point numbers.
4549///
4550/// # Examples
4551///
4552/// ```rust
4553/// use fixed::traits::LossyFrom;
4554/// use fixed::types::{I12F4, I8F24};
4555/// // original is 0x12.345678, lossy is 0x012.3
4556/// let original = I8F24::from_bits(0x1234_5678);
4557/// let lossy = I12F4::lossy_from(original);
4558/// assert_eq!(lossy, I12F4::from_bits(0x0123));
4559/// ```
4560pub trait LossyFrom<Src> {
4561 /// Performs the conversion.
4562 fn lossy_from(src: Src) -> Self;
4563}
4564
4565/// This trait provides infallible conversions that might be lossy.
4566/// This is the reciprocal of [`LossyFrom`].
4567///
4568/// Usually [`LossyFrom`] should be implemented instead of this trait;
4569/// there is a blanket implementation which provides this trait when
4570/// [`LossyFrom`] is implemented (similar to [`Into`] and [`From`]).
4571///
4572/// # Examples
4573///
4574/// ```rust
4575/// use fixed::traits::LossyInto;
4576/// use fixed::types::{I12F4, I8F24};
4577/// // original is 0x12.345678, lossy is 0x012.3
4578/// let original = I8F24::from_bits(0x1234_5678);
4579/// let lossy: I12F4 = original.lossy_into();
4580/// assert_eq!(lossy, I12F4::from_bits(0x0123));
4581/// ```
4582pub trait LossyInto<Dst> {
4583 /// Performs the conversion.
4584 fn lossy_into(self) -> Dst;
4585}
4586
4587impl<Src, Dst> LossyInto<Dst> for Src
4588where
4589 Dst: LossyFrom<Src>,
4590{
4591 fn lossy_into(self) -> Dst {
4592 Dst::lossy_from(self)
4593 }
4594}
4595
4596/// This trait provides checked conversions from fixed-point numbers.
4597///
4598/// This trait is implemented for conversions between integer
4599/// primitives, floating-point primitives and fixed-point numbers.
4600///
4601/// # Examples
4602///
4603/// ```rust
4604/// use fixed::traits::FromFixed;
4605/// use fixed::types::U8F8;
4606/// // 0x87.65
4607/// let f = U8F8::from_bits(0x8765);
4608/// assert_eq!(f32::from_fixed(f), f32::from(0x8765u16) / 256.0);
4609/// assert_eq!(i32::checked_from_fixed(f), Some(0x87));
4610/// assert_eq!(u8::saturating_from_fixed(f), 0x87);
4611/// // no fit
4612/// assert_eq!(i8::checked_from_fixed(f), None);
4613/// assert_eq!(i8::saturating_from_fixed(f), i8::MAX);
4614/// assert_eq!(i8::wrapping_from_fixed(f), 0x87u8 as i8);
4615/// assert_eq!(i8::overflowing_from_fixed(f), (0x87u8 as i8, true));
4616/// ```
4617pub trait FromFixed {
4618 /// Converts from a fixed-point number.
4619 ///
4620 /// Any extra fractional bits are discarded, which rounds towards −∞.
4621 ///
4622 /// # Panics
4623 ///
4624 /// When debug assertions are enabled, panics if the value does
4625 /// not fit. When debug assertions are not enabled, the wrapped
4626 /// value can be returned, but it is not considered a breaking
4627 /// change if in the future it panics; if wrapping is required use
4628 /// [`wrapping_from_fixed`] instead.
4629 ///
4630 /// [`wrapping_from_fixed`]: FromFixed::wrapping_from_fixed
4631 fn from_fixed<F: Fixed>(src: F) -> Self;
4632
4633 /// Converts from a fixed-point number if it fits, otherwise returns [`None`].
4634 ///
4635 /// Any extra fractional bits are discarded, which rounds towards −∞.
4636 fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
4637 where
4638 Self: Sized;
4639
4640 /// Converts from a fixed-point number, saturating if it does not fit.
4641 ///
4642 /// Any extra fractional bits are discarded, which rounds towards −∞.
4643 fn saturating_from_fixed<F: Fixed>(src: F) -> Self;
4644
4645 /// Converts from a fixed-point number, wrapping if it does not fit.
4646 ///
4647 /// Any extra fractional bits are discarded, which rounds towards −∞.
4648 fn wrapping_from_fixed<F: Fixed>(src: F) -> Self;
4649
4650 /// Converts from a fixed-point number.
4651 ///
4652 /// Returns a [tuple] of the value and a [`bool`] indicating whether
4653 /// an overflow has occurred. On overflow, the wrapped value is
4654 /// returned.
4655 ///
4656 /// Any extra fractional bits are discarded, which rounds towards −∞.
4657 fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
4658 where
4659 Self: Sized;
4660
4661 /// Converts from a fixed-point number, panicking if the value
4662 /// does not fit.
4663 ///
4664 /// Any extra fractional bits are discarded, which rounds towards −∞.
4665 ///
4666 /// # Panics
4667 ///
4668 /// Panics if the value does not fit, even when debug assertions
4669 /// are not enabled.
4670 #[inline]
4671 #[track_caller]
4672 fn strict_from_fixed<F: Fixed>(src: F) -> Self
4673 where
4674 Self: Sized,
4675 {
4676 match Self::overflowing_from_fixed(src) {
4677 (val, false) => val,
4678 (_, true) => panic!("overflow"),
4679 }
4680 }
4681
4682 /// Converts from a fixed-point number, panicking if the value
4683 /// does not fit.
4684 ///
4685 /// Any extra fractional bits are discarded, which rounds towards −∞.
4686 ///
4687 /// # Panics
4688 ///
4689 /// Panics if the value does not fit, even when debug assertions
4690 /// are not enabled.
4691 #[deprecated(since = "1.30.0", note = "renamed to `strict_from_fixed`")]
4692 #[inline]
4693 #[track_caller]
4694 fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self
4695 where
4696 Self: Sized,
4697 {
4698 Self::strict_from_fixed(src)
4699 }
4700}
4701
4702/// This trait provides checked conversions to fixed-point numbers.
4703///
4704/// This trait is implemented for conversions between integer
4705/// primitives, floating-point primitives and fixed-point numbers.
4706///
4707/// # Examples
4708///
4709/// ```rust
4710/// use fixed::traits::ToFixed;
4711/// use fixed::types::{U8F8, U16F16};
4712/// let f: U8F8 = 13.5f32.to_fixed();
4713/// assert_eq!(f, U8F8::from_bits((13 << 8) | (1 << 7)));
4714/// // 0x1234.5678 is too large and can be wrapped to 0x34.56
4715/// let too_large = U16F16::from_bits(0x1234_5678);
4716/// let checked: Option<U8F8> = too_large.checked_to_num();
4717/// assert_eq!(checked, None);
4718/// let saturating: U8F8 = too_large.saturating_to_num();
4719/// assert_eq!(saturating, U8F8::MAX);
4720/// let wrapping: U8F8 = too_large.wrapping_to_num();
4721/// assert_eq!(wrapping, U8F8::from_bits(0x3456));
4722/// let overflowing: (U8F8, bool) = too_large.overflowing_to_num();
4723/// assert_eq!(overflowing, (U8F8::from_bits(0x3456), true));
4724/// ```
4725pub trait ToFixed {
4726 /// Converts to a fixed-point number.
4727 ///
4728 /// Any extra fractional bits are discarded, which rounds towards −∞.
4729 ///
4730 /// # Panics
4731 ///
4732 /// Panics if `self` is a floating-point number that is not [finite].
4733 ///
4734 /// When debug assertions are enabled, also panics if the value
4735 /// does not fit. When debug assertions are not enabled, the
4736 /// wrapped value can be returned, but it is not considered a
4737 /// breaking change if in the future it panics; if wrapping is
4738 /// required use [`wrapping_to_fixed`] instead.
4739 ///
4740 /// [`wrapping_to_fixed`]: ToFixed::wrapping_to_fixed
4741 /// [finite]: f64::is_finite
4742 fn to_fixed<F: Fixed>(self) -> F;
4743
4744 /// Converts to a fixed-point number if it fits, otherwise returns [`None`].
4745 ///
4746 /// Any extra fractional bits are discarded, which rounds towards −∞.
4747 fn checked_to_fixed<F: Fixed>(self) -> Option<F>;
4748
4749 /// Converts to a fixed-point number, saturating if it does not fit.
4750 ///
4751 /// Any extra fractional bits are discarded, which rounds towards −∞.
4752 ///
4753 /// # Panics
4754 ///
4755 /// Panics if `self` is a floating-point number that is [NaN].
4756 ///
4757 /// [NaN]: f64::is_nan
4758 fn saturating_to_fixed<F: Fixed>(self) -> F;
4759
4760 /// Converts to a fixed-point number, wrapping if it does not fit.
4761 ///
4762 /// Any extra fractional bits are discarded, which rounds towards −∞.
4763 ///
4764 /// # Panics
4765 ///
4766 /// Panics if `self` is a floating-point number that is not [finite].
4767 ///
4768 /// [finite]: f64::is_finite
4769 fn wrapping_to_fixed<F: Fixed>(self) -> F;
4770
4771 /// Converts to a fixed-point number.
4772 ///
4773 /// Returns a [tuple] of the fixed-point number and a [`bool`]
4774 /// indicating whether an overflow has occurred. On overflow, the
4775 /// wrapped value is returned.
4776 ///
4777 /// Any extra fractional bits are discarded, which rounds towards −∞.
4778 ///
4779 /// # Panics
4780 ///
4781 /// Panics if `self` is a floating-point number that is not [finite].
4782 ///
4783 /// [finite]: f64::is_finite
4784 fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool);
4785
4786 /// Converts to a fixed-point number, panicking if it does not fit.
4787 ///
4788 /// Any extra fractional bits are discarded, which rounds towards −∞.
4789 ///
4790 /// # Panics
4791 ///
4792 /// Panics if `self` is a floating-point number that is not
4793 /// [finite] or if the value does not fit, even if debug
4794 /// assertions are not enabled.
4795 ///
4796 /// [finite]: f64::is_finite
4797 #[inline]
4798 #[track_caller]
4799 fn strict_to_fixed<F: Fixed>(self) -> F
4800 where
4801 Self: Sized,
4802 {
4803 match self.overflowing_to_fixed() {
4804 (val, false) => val,
4805 (_, true) => panic!("overflow"),
4806 }
4807 }
4808
4809 /// Converts to a fixed-point number, panicking if it does not fit.
4810 ///
4811 /// Any extra fractional bits are discarded, which rounds towards −∞.
4812 ///
4813 /// # Panics
4814 ///
4815 /// Panics if `self` is a floating-point number that is not
4816 /// [finite] or if the value does not fit, even if debug
4817 /// assertions are not enabled.
4818 ///
4819 /// [finite]: f64::is_finite
4820 #[deprecated(since = "1.30.0", note = "renamed to `strict_to_fixed`")]
4821 #[inline]
4822 #[track_caller]
4823 fn unwrapped_to_fixed<F: Fixed>(self) -> F
4824 where
4825 Self: Sized,
4826 {
4827 self.strict_to_fixed()
4828 }
4829}
4830
4831/// This trait provides a way to convert a number to/from an equivalent
4832/// fixed-point number.
4833///
4834/// Implementations are provided for the signed integer primitives [`i8`],
4835/// [`i16`], [`i32`], [`i64`] and [`i128`], which have equivalent fixed-point
4836/// types [`I8F0`], [`I16F0`], [`I32F0`], [`I64F0`] and [`I128F0`]. Similar
4837/// implementations are provided for the unsigned integer primitives [`u8`],
4838/// [`u16`], [`u32`], [`u64`] and [`u128`].
4839///
4840/// # Examples
4841///
4842/// An [`i32`] can be treated as an [`I32F0`].
4843///
4844/// ```rust
4845/// use fixed::traits::{Fixed, FixedEquiv};
4846///
4847/// fn next_up<F: Fixed>(f: &mut F) {
4848/// *f += F::DELTA;
4849/// }
4850///
4851/// let mut i = 12i32;
4852/// // next_up is called with &mut i converted to &mut I32F0
4853/// next_up(i.as_fixed_equiv_mut());
4854/// assert_eq!(i, 13);
4855/// ```
4856///
4857/// Simlarly, an [`I32F0`] can be treated as an [`i32`].
4858///
4859/// ```rust
4860/// use fixed::traits::FixedEquiv;
4861/// use fixed::types::I32F0;
4862///
4863/// fn increase_by_5(i: &mut i32) {
4864/// *i += 5;
4865/// }
4866///
4867/// let mut f = I32F0::from_num(12);
4868/// // increase_by_5 is called with &mut f converted to &mut i32
4869/// increase_by_5(i32::mut_from_fixed_equiv(&mut f));
4870/// assert_eq!(f, 17);
4871/// ```
4872///
4873/// [`I8F0`]: crate::types::I8F0
4874/// [`I16F0`]: crate::types::I16F0
4875/// [`I32F0`]: crate::types::I32F0
4876/// [`I64F0`]: crate::types::I64F0
4877/// [`I128F0`]: crate::types::I128F0
4878pub trait FixedEquiv {
4879 /// The equivalent fixed-point type.
4880 type Equiv: Fixed;
4881
4882 /// Converts an owned value to the equivalent fixed-point type.
4883 fn to_fixed_equiv(self) -> Self::Equiv;
4884
4885 /// Converts a reference into a reference to the equivalent fixed-point
4886 /// type.
4887 fn as_fixed_equiv(&self) -> &Self::Equiv;
4888
4889 /// Converts a mutable reference into a mutable reference to the equivalent
4890 /// fixed-point type.
4891 fn as_fixed_equiv_mut(&mut self) -> &mut Self::Equiv;
4892
4893 /// Converts an owned equivalent fixed-point type to this type.
4894 fn from_fixed_equiv(f: Self::Equiv) -> Self;
4895
4896 /// Converts a reference to the equivalent fixed-point type into a reference
4897 /// to this type.
4898 fn ref_from_fixed_equiv(f: &Self::Equiv) -> &Self;
4899
4900 /// Converts a mutable reference to the equivalent fixed-point type into a
4901 /// mutable reference to this type.
4902 fn mut_from_fixed_equiv(f: &mut Self::Equiv) -> &mut Self;
4903}
4904
4905macro_rules! trait_delegate {
4906 (fn $method:ident($($param:ident: $Param:ty),*$(,)?) -> $Ret:ty) => {
4907 #[inline]
4908 fn $method($($param: $Param),*) -> $Ret {
4909 Self::$method($($param),*)
4910 }
4911 };
4912 (fn $method:ident(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4913 #[inline]
4914 fn $method(self $(, $param: $Param)*) -> $Ret {
4915 self.$method($($param),*)
4916 }
4917 };
4918 (unsafe fn $method:ident(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4919 #[inline]
4920 unsafe fn $method(self $(, $param: $Param)*) -> $Ret {
4921 unsafe { self.$method($($param),*) }
4922 }
4923 };
4924 (fn $method:ident(&mut self $(, $param:ident: $Param:ty)*) $(-> $Ret:ty)*) => {
4925 #[inline]
4926 fn $method(&mut self $(, $param: $Param)*) $(-> $Ret)* {
4927 self.$method($($param),*)
4928 }
4929 };
4930 (fn $method:ident<$Gen:ident: $Trait:ident>($($param:ident: $Param:ty),*) -> $Ret:ty) => {
4931 #[inline]
4932 fn $method<$Gen: $Trait>($($param: $Param),*) -> $Ret {
4933 Self::$method($($param),*)
4934 }
4935 };
4936 (fn $method:ident<$Gen:ident: $Trait:ident>(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => {
4937 #[inline]
4938 fn $method<$Gen: $Trait>(self $(, $param: $Param)*) -> $Ret {
4939 self.$method($($param),*)
4940 }
4941 };
4942}
4943
4944macro_rules! impl_fixed {
4945 (
4946 $Fixed:ident, $IFixed:ident, $UFixed:ident, $LeEqU:ident, $Bits:ident, $Signedness:ident
4947 ) => {
4948 impl<Frac: $LeEqU> FixedOptionalArbitrary for $Fixed<Frac> {}
4949 impl<Frac: $LeEqU> FixedOptionalBorsh for $Fixed<Frac> {}
4950 impl<Frac: $LeEqU> FixedOptionalNum for $Fixed<Frac> {}
4951 impl<Frac: $LeEqU> FixedOptionalSerde for $Fixed<Frac> {}
4952 impl<Frac: $LeEqU> FixedOptionalNightlyFloat for $Fixed<Frac> {}
4953 impl<Frac: $LeEqU> FixedOptionalFeatures for $Fixed<Frac> {}
4954
4955 impl<Frac: $LeEqU> Fixed for $Fixed<Frac> {
4956 type Bits = $Bits;
4957 type NonZeroBits = NonZero<$Bits>;
4958 type Bytes = [u8; size_of::<$Bits>()];
4959 type Frac = Frac;
4960 type Signed = $IFixed<Frac>;
4961 type Unsigned = $UFixed<Frac>;
4962 const ZERO: Self = Self::ZERO;
4963 const TRY_ONE: Option<Self> = Self::TRY_ONE;
4964 const DELTA: Self = Self::DELTA;
4965 const MIN: Self = Self::MIN;
4966 const MAX: Self = Self::MAX;
4967 const IS_SIGNED: bool = Self::IS_SIGNED;
4968 const INT_NBITS: u32 = Self::INT_NBITS;
4969 const FRAC_NBITS: u32 = Self::FRAC_NBITS;
4970 trait_delegate! { fn from_bits(bits: Self::Bits) -> Self }
4971 trait_delegate! { fn to_bits(self) -> Self::Bits }
4972 trait_delegate! { fn from_be(fixed: Self) -> Self }
4973 trait_delegate! { fn from_le(fixed: Self) -> Self }
4974 trait_delegate! { fn to_be(self) -> Self }
4975 trait_delegate! { fn to_le(self) -> Self }
4976 trait_delegate! { fn swap_bytes(self) -> Self }
4977 trait_delegate! { fn from_be_bytes(bits: Self::Bytes) -> Self }
4978 trait_delegate! { fn from_le_bytes(bits: Self::Bytes) -> Self }
4979 trait_delegate! { fn from_ne_bytes(bits: Self::Bytes) -> Self }
4980 trait_delegate! { fn to_be_bytes(self) -> Self::Bytes }
4981 trait_delegate! { fn to_le_bytes(self) -> Self::Bytes }
4982 trait_delegate! { fn to_ne_bytes(self) -> Self::Bytes }
4983 trait_delegate! { fn from_num<Src: ToFixed>(src: Src) -> Self }
4984 trait_delegate! { fn to_num<Dst: FromFixed>(self) -> Dst }
4985 trait_delegate! { fn checked_from_num<Src: ToFixed>(val: Src) -> Option<Self> }
4986 trait_delegate! { fn checked_to_num<Dst: FromFixed>(self) -> Option<Dst> }
4987 trait_delegate! { fn saturating_from_num<Src: ToFixed>(val: Src) -> Self }
4988 trait_delegate! { fn saturating_to_num<Dst: FromFixed>(self) -> Dst }
4989 trait_delegate! { fn wrapping_from_num<Src: ToFixed>(val: Src) -> Self }
4990 trait_delegate! { fn wrapping_to_num<Dst: FromFixed>(self) -> Dst }
4991 trait_delegate! { fn strict_from_num<Src: ToFixed>(val: Src) -> Self }
4992 trait_delegate! { fn strict_to_num<Dst: FromFixed>(self) -> Dst }
4993 trait_delegate! { fn overflowing_from_num<Src: ToFixed>(val: Src) -> (Self, bool) }
4994 trait_delegate! { fn overflowing_to_num<Dst: FromFixed>(self) -> (Dst, bool) }
4995 trait_delegate! { fn from_str_binary(src: &str) -> Result<Self, ParseFixedError> }
4996 trait_delegate! { fn from_str_octal(src: &str) -> Result<Self, ParseFixedError> }
4997 trait_delegate! { fn from_str_hex(src: &str) -> Result<Self, ParseFixedError> }
4998 trait_delegate! { fn from_ascii(src: &[u8]) -> Result<Self, ParseFixedError> }
4999 trait_delegate! { fn from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError> }
5000 trait_delegate! { fn from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError> }
5001 trait_delegate! { fn from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError> }
5002 trait_delegate! {
5003 fn saturating_from_str(src: &str) -> Result<Self, ParseFixedError>
5004 }
5005 trait_delegate! {
5006 fn saturating_from_str_binary(src: &str) -> Result<Self, ParseFixedError>
5007 }
5008 trait_delegate! {
5009 fn saturating_from_str_octal(src: &str) -> Result<Self, ParseFixedError>
5010 }
5011 trait_delegate! {
5012 fn saturating_from_str_hex(src: &str) -> Result<Self, ParseFixedError>
5013 }
5014 trait_delegate! {
5015 fn saturating_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>
5016 }
5017 trait_delegate! {
5018 fn saturating_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>
5019 }
5020 trait_delegate! {
5021 fn saturating_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>
5022 }
5023 trait_delegate! {
5024 fn saturating_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>
5025 }
5026 trait_delegate! {
5027 fn wrapping_from_str(src: &str) -> Result<Self, ParseFixedError>
5028 }
5029 trait_delegate! {
5030 fn wrapping_from_str_binary(src: &str) -> Result<Self, ParseFixedError>
5031 }
5032 trait_delegate! {
5033 fn wrapping_from_str_octal(src: &str) -> Result<Self, ParseFixedError>
5034 }
5035 trait_delegate! {
5036 fn wrapping_from_str_hex(src: &str) -> Result<Self, ParseFixedError>
5037 }
5038 trait_delegate! {
5039 fn wrapping_from_ascii(src: &[u8]) -> Result<Self, ParseFixedError>
5040 }
5041 trait_delegate! {
5042 fn wrapping_from_ascii_binary(src: &[u8]) -> Result<Self, ParseFixedError>
5043 }
5044 trait_delegate! {
5045 fn wrapping_from_ascii_octal(src: &[u8]) -> Result<Self, ParseFixedError>
5046 }
5047 trait_delegate! {
5048 fn wrapping_from_ascii_hex(src: &[u8]) -> Result<Self, ParseFixedError>
5049 }
5050 trait_delegate! { fn strict_from_str(src: &str) -> Self }
5051 trait_delegate! { fn strict_from_str_binary(src: &str) -> Self }
5052 trait_delegate! { fn strict_from_str_octal(src: &str) -> Self }
5053 trait_delegate! { fn strict_from_str_hex(src: &str) -> Self }
5054 trait_delegate! { fn strict_from_ascii(src: &[u8]) -> Self }
5055 trait_delegate! { fn strict_from_ascii_binary(src: &[u8]) -> Self }
5056 trait_delegate! { fn strict_from_ascii_octal(src: &[u8]) -> Self }
5057 trait_delegate! { fn strict_from_ascii_hex(src: &[u8]) -> Self }
5058 trait_delegate! {
5059 fn overflowing_from_str(src: &str) -> Result<(Self, bool), ParseFixedError>
5060 }
5061 trait_delegate! {
5062 fn overflowing_from_str_binary(src: &str) -> Result<(Self, bool), ParseFixedError>
5063 }
5064 trait_delegate! {
5065 fn overflowing_from_str_octal(src: &str) -> Result<(Self, bool), ParseFixedError>
5066 }
5067 trait_delegate! {
5068 fn overflowing_from_str_hex(src: &str) -> Result<(Self, bool), ParseFixedError>
5069 }
5070 trait_delegate! {
5071 fn overflowing_from_ascii(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
5072 }
5073 trait_delegate! {
5074 fn overflowing_from_ascii_binary(
5075 src: &[u8],
5076 ) -> Result<(Self, bool), ParseFixedError>
5077 }
5078 trait_delegate! {
5079 fn overflowing_from_ascii_octal(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
5080 }
5081 trait_delegate! {
5082 fn overflowing_from_ascii_hex(src: &[u8]) -> Result<(Self, bool), ParseFixedError>
5083 }
5084 trait_delegate! { fn int(self) -> Self }
5085 trait_delegate! { fn frac(self) -> Self }
5086 trait_delegate! { fn ceil(self) -> Self }
5087 trait_delegate! { fn floor(self) -> Self }
5088 trait_delegate! { fn round_to_zero(self) -> Self }
5089 trait_delegate! { fn round(self) -> Self }
5090 trait_delegate! { fn round_ties_even(self) -> Self }
5091 trait_delegate! { fn checked_ceil(self) -> Option<Self> }
5092 trait_delegate! { fn checked_floor(self) -> Option<Self> }
5093 trait_delegate! { fn checked_round(self) -> Option<Self> }
5094 trait_delegate! { fn checked_round_ties_even(self) -> Option<Self> }
5095 trait_delegate! { fn saturating_ceil(self) -> Self }
5096 trait_delegate! { fn saturating_floor(self) -> Self }
5097 trait_delegate! { fn saturating_round(self) -> Self }
5098 trait_delegate! { fn saturating_round_ties_even(self) -> Self }
5099 trait_delegate! { fn wrapping_ceil(self) -> Self }
5100 trait_delegate! { fn wrapping_floor(self) -> Self }
5101 trait_delegate! { fn wrapping_round(self) -> Self }
5102 trait_delegate! { fn wrapping_round_ties_even(self) -> Self }
5103 trait_delegate! { fn strict_ceil(self) -> Self }
5104 trait_delegate! { fn strict_floor(self) -> Self }
5105 trait_delegate! { fn strict_round(self) -> Self }
5106 trait_delegate! { fn strict_round_ties_even(self) -> Self }
5107 trait_delegate! { fn overflowing_ceil(self) -> (Self, bool) }
5108 trait_delegate! { fn overflowing_floor(self) -> (Self, bool) }
5109 trait_delegate! { fn overflowing_round(self) -> (Self, bool) }
5110 trait_delegate! { fn overflowing_round_ties_even(self) -> (Self, bool) }
5111 trait_delegate! { fn count_ones(self) -> u32 }
5112 trait_delegate! { fn count_zeros(self) -> u32 }
5113 trait_delegate! { fn leading_ones(self) -> u32 }
5114 trait_delegate! { fn leading_zeros(self) -> u32 }
5115 trait_delegate! { fn trailing_ones(self) -> u32 }
5116 trait_delegate! { fn trailing_zeros(self) -> u32 }
5117 trait_delegate! { fn int_log2(self) -> i32 }
5118 trait_delegate! { fn int_log10(self) -> i32 }
5119 trait_delegate! { fn int_log(self, base: u32) -> i32 }
5120 trait_delegate! { fn checked_int_log2(self) -> Option<i32> }
5121 trait_delegate! { fn checked_int_log10(self) -> Option<i32> }
5122 trait_delegate! { fn checked_int_log(self, base: u32) -> Option<i32> }
5123 trait_delegate! { fn reverse_bits(self) -> Self }
5124 trait_delegate! { fn rotate_left(self, n: u32) -> Self }
5125 trait_delegate! { fn rotate_right(self, n: u32) -> Self }
5126 trait_delegate! { fn is_zero(self) -> bool }
5127 trait_delegate! { fn dist(self, other: Self) -> Self }
5128 trait_delegate! { fn abs_diff(self, other: Self) -> Self::Unsigned }
5129 trait_delegate! { fn mean(self, other: Self) -> Self }
5130 trait_delegate! { fn hypot(self, other: Self) -> Self }
5131 trait_delegate! { fn recip(self) -> Self }
5132 trait_delegate! { fn next_multiple_of(self, other: Self) -> Self }
5133 trait_delegate! { fn mul_add(self, mul: Self, add: Self) -> Self }
5134 trait_delegate! { fn add_prod(self, a: Self, b: Self) -> Self }
5135 trait_delegate! { fn mul_acc(&mut self, a: Self, b: Self) }
5136 trait_delegate! { fn div_euclid(self, rhs: Self) -> Self }
5137 trait_delegate! { fn rem_euclid(self, rhs: Self) -> Self }
5138 trait_delegate! { fn div_euclid_int(self, rhs: Self::Bits) -> Self }
5139 trait_delegate! { fn rem_euclid_int(self, rhs: Self::Bits) -> Self }
5140 trait_delegate! { fn unbounded_shl(self, rhs: u32) -> Self }
5141 trait_delegate! { fn unbounded_shr(self, rhs: u32) -> Self }
5142 trait_delegate! { fn sqrt(self) -> Self }
5143 trait_delegate! { fn lerp(self, start: Self, end: Self) -> Self }
5144 trait_delegate! { fn inv_lerp(self, start: Self, end: Self) -> Self }
5145 trait_delegate! { fn checked_neg(self) -> Option<Self> }
5146 trait_delegate! { fn checked_add(self, rhs: Self) -> Option<Self> }
5147 trait_delegate! { fn checked_sub(self, rhs: Self) -> Option<Self> }
5148 trait_delegate! { fn checked_mul(self, rhs: Self) -> Option<Self> }
5149 trait_delegate! { fn checked_div(self, rhs: Self) -> Option<Self> }
5150 trait_delegate! { fn checked_rem(self, rhs: Self) -> Option<Self> }
5151 trait_delegate! { fn checked_recip(self) -> Option<Self> }
5152 trait_delegate! { fn checked_next_multiple_of(self, other: Self) -> Option<Self> }
5153 trait_delegate! { fn checked_mul_add(self, mul: Self, add: Self) -> Option<Self> }
5154 trait_delegate! { fn checked_add_prod(self, a: Self, b: Self) -> Option<Self> }
5155 trait_delegate! { fn checked_mul_acc(&mut self, a: Self, b: Self) -> Option<()> }
5156 trait_delegate! { fn checked_div_euclid(self, rhs: Self) -> Option<Self> }
5157 trait_delegate! { fn checked_rem_euclid(self, rhs: Self) -> Option<Self> }
5158 trait_delegate! { fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self> }
5159 trait_delegate! { fn checked_div_int(self, rhs: Self::Bits) -> Option<Self> }
5160 trait_delegate! { fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self> }
5161 trait_delegate! { fn checked_div_euclid_int(self, rhs: Self::Bits) -> Option<Self> }
5162 trait_delegate! { fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self> }
5163 trait_delegate! { fn checked_shl(self, rhs: u32) -> Option<Self> }
5164 trait_delegate! { fn checked_shr(self, rhs: u32) -> Option<Self> }
5165 trait_delegate! { fn checked_dist(self, other: Self) -> Option<Self> }
5166 trait_delegate! { fn checked_hypot(self, other: Self) -> Option<Self> }
5167 trait_delegate! { fn checked_sqrt(self) -> Option<Self> }
5168 trait_delegate! { fn checked_lerp(self, start: Self, end: Self) -> Option<Self> }
5169 trait_delegate! { fn checked_inv_lerp(self, start: Self, end: Self) -> Option<Self> }
5170 trait_delegate! { fn saturating_neg(self) -> Self }
5171 trait_delegate! { fn saturating_add(self, rhs: Self) -> Self }
5172 trait_delegate! { fn saturating_sub(self, rhs: Self) -> Self }
5173 trait_delegate! { fn saturating_mul(self, rhs: Self) -> Self }
5174 trait_delegate! { fn saturating_div(self, rhs: Self) -> Self }
5175 trait_delegate! { fn saturating_recip(self) -> Self }
5176 trait_delegate! { fn saturating_next_multiple_of(self, other: Self) -> Self }
5177 trait_delegate! { fn saturating_mul_add(self, mul: Self, add: Self) -> Self }
5178 trait_delegate! { fn saturating_add_prod(self, a: Self, b: Self) -> Self }
5179 trait_delegate! { fn saturating_mul_acc(&mut self, a: Self, b: Self) }
5180 trait_delegate! { fn saturating_div_euclid(self, rhs: Self) -> Self }
5181 trait_delegate! { fn saturating_mul_int(self, rhs: Self::Bits) -> Self }
5182 trait_delegate! { fn saturating_div_int(self, rhs: Self::Bits) -> Self }
5183 trait_delegate! { fn saturating_div_euclid_int(self, rhs: Self::Bits) -> Self }
5184 trait_delegate! { fn saturating_rem_euclid_int(self, rhs: Self::Bits) -> Self }
5185 trait_delegate! { fn saturating_dist(self, other: Self) -> Self }
5186 trait_delegate! { fn saturating_hypot(self, other: Self) -> Self }
5187 trait_delegate! { fn saturating_sqrt(self) -> Self }
5188 trait_delegate! { fn saturating_lerp(self, start: Self, end: Self) -> Self }
5189 trait_delegate! { fn saturating_inv_lerp(self, start: Self, end: Self) -> Self }
5190 trait_delegate! { fn wrapping_neg(self) -> Self }
5191 trait_delegate! { fn wrapping_add(self, rhs: Self) -> Self }
5192 trait_delegate! { fn wrapping_sub(self, rhs: Self) -> Self }
5193 trait_delegate! { fn wrapping_mul(self, rhs: Self) -> Self }
5194 trait_delegate! { fn wrapping_div(self, rhs: Self) -> Self }
5195 trait_delegate! { fn wrapping_recip(self) -> Self }
5196 trait_delegate! { fn wrapping_next_multiple_of(self, other: Self) -> Self }
5197 trait_delegate! { fn wrapping_mul_add(self, mul: Self, add: Self) -> Self }
5198 trait_delegate! { fn wrapping_add_prod(self, a: Self, b: Self) -> Self }
5199 trait_delegate! { fn wrapping_mul_acc(&mut self, a: Self, b: Self) }
5200 trait_delegate! { fn wrapping_div_euclid(self, rhs: Self) -> Self }
5201 trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self }
5202 trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self }
5203 trait_delegate! { fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self }
5204 trait_delegate! { fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self }
5205 trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self }
5206 trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self }
5207 trait_delegate! { fn wrapping_dist(self, other: Self) -> Self }
5208 trait_delegate! { fn wrapping_hypot(self, other: Self) -> Self }
5209 trait_delegate! { fn wrapping_sqrt(self) -> Self }
5210 trait_delegate! { fn wrapping_lerp(self, start: Self, end: Self) -> Self }
5211 trait_delegate! { fn wrapping_inv_lerp(self, start: Self, end: Self) -> Self }
5212 trait_delegate! { fn strict_neg(self) -> Self }
5213 trait_delegate! { fn strict_add(self, rhs: Self) -> Self }
5214 trait_delegate! { fn strict_sub(self, rhs: Self) -> Self }
5215 trait_delegate! { fn strict_mul(self, rhs: Self) -> Self }
5216 trait_delegate! { fn strict_div(self, rhs: Self) -> Self }
5217 trait_delegate! { fn strict_rem(self, rhs: Self) -> Self }
5218 trait_delegate! { fn strict_recip(self) -> Self }
5219 trait_delegate! { fn strict_next_multiple_of(self, other: Self) -> Self }
5220 trait_delegate! { fn strict_mul_add(self, mul: Self, add: Self) -> Self }
5221 trait_delegate! { fn strict_add_prod(self, a: Self, b: Self) -> Self }
5222 trait_delegate! { fn strict_mul_acc(&mut self, a: Self, b: Self) }
5223 trait_delegate! { fn strict_div_euclid(self, rhs: Self) -> Self }
5224 trait_delegate! { fn strict_rem_euclid(self, rhs: Self) -> Self }
5225 trait_delegate! { fn strict_mul_int(self, rhs: Self::Bits) -> Self }
5226 trait_delegate! { fn strict_div_int(self, rhs: Self::Bits) -> Self }
5227 trait_delegate! { fn strict_rem_int(self, rhs: Self::Bits) -> Self }
5228 trait_delegate! { fn strict_div_euclid_int(self, rhs: Self::Bits) -> Self }
5229 trait_delegate! { fn strict_rem_euclid_int(self, rhs: Self::Bits) -> Self }
5230 trait_delegate! { fn strict_shl(self, rhs: u32) -> Self }
5231 trait_delegate! { fn strict_shr(self, rhs: u32) -> Self }
5232 trait_delegate! { fn strict_dist(self, other: Self) -> Self }
5233 trait_delegate! { fn strict_hypot(self, other: Self) -> Self }
5234 trait_delegate! { fn strict_sqrt(self) -> Self }
5235 trait_delegate! { fn strict_lerp(self, start: Self, end: Self) -> Self }
5236 trait_delegate! { fn strict_inv_lerp(self, start: Self, end: Self) -> Self }
5237 trait_delegate! { fn overflowing_neg(self) -> (Self, bool) }
5238 trait_delegate! { fn overflowing_add(self, rhs: Self) -> (Self, bool) }
5239 trait_delegate! { fn overflowing_sub(self, rhs: Self) -> (Self, bool) }
5240 trait_delegate! { fn overflowing_mul(self, rhs: Self) -> (Self, bool) }
5241 trait_delegate! { fn overflowing_div(self, rhs: Self) -> (Self, bool) }
5242 trait_delegate! { fn overflowing_recip(self) -> (Self, bool) }
5243 trait_delegate! { fn overflowing_next_multiple_of(self, other: Self) -> (Self, bool) }
5244 trait_delegate! { fn overflowing_mul_add(self, mul: Self, add: Self) -> (Self, bool) }
5245 trait_delegate! { fn overflowing_add_prod(self, a: Self, b: Self) -> (Self, bool) }
5246 trait_delegate! { fn overflowing_mul_acc(&mut self, a: Self, b: Self) -> bool }
5247 trait_delegate! { fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) }
5248 trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) }
5249 trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) }
5250 trait_delegate! { fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
5251 trait_delegate! { fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
5252 trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) }
5253 trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) }
5254 trait_delegate! { fn overflowing_dist(self, other: Self) -> (Self, bool) }
5255 trait_delegate! { fn overflowing_hypot(self, other: Self) -> (Self, bool) }
5256 trait_delegate! { fn overflowing_sqrt(self) -> (Self, bool) }
5257 trait_delegate! { fn overflowing_lerp(self, start: Self, end: Self) -> (Self, bool) }
5258 trait_delegate! {
5259 fn overflowing_inv_lerp(self, start: Self, end: Self) -> (Self, bool)
5260 }
5261 trait_delegate! { unsafe fn unchecked_add(self, rhs: Self) -> Self }
5262 trait_delegate! { unsafe fn unchecked_sub(self, rhs: Self) -> Self }
5263 trait_delegate! { unsafe fn unchecked_shl(self, rhs: u32) -> Self }
5264 trait_delegate! { unsafe fn unchecked_shr(self, rhs: u32) -> Self }
5265 trait_delegate! { unsafe fn unchecked_mul_int(self, rhs: Self::Bits) -> Self }
5266 }
5267
5268 impl<Frac: $LeEqU> FromFixed for $Fixed<Frac> {
5269 /// Converts a fixed-point number.
5270 ///
5271 /// Any extra fractional bits are discarded, which rounds towards −∞.
5272 ///
5273 /// # Panics
5274 ///
5275 /// When debug assertions are enabled, panics if the value
5276 /// does not fit. When debug assertions are not enabled,
5277 /// the wrapped value can be returned, but it is not
5278 /// considered a breaking change if in the future it
5279 /// panics; if wrapping is required use
5280 /// [`wrapping_from_fixed`] instead.
5281 ///
5282 /// [`wrapping_from_fixed`]: FromFixed::wrapping_from_fixed
5283 #[inline]
5284 fn from_fixed<F: Fixed>(src: F) -> Self {
5285 let (wrapped, overflow) = FromFixed::overflowing_from_fixed(src);
5286 debug_assert!(!overflow, "{} overflows", src);
5287 wrapped
5288 }
5289
5290 /// Converts a fixed-point number if it fits, otherwise returns [`None`].
5291 ///
5292 /// Any extra fractional bits are discarded, which rounds towards −∞.
5293 #[inline]
5294 fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self> {
5295 match FromFixed::overflowing_from_fixed(src) {
5296 (_, true) => None,
5297 (wrapped, false) => Some(wrapped),
5298 }
5299 }
5300
5301 /// Converts a fixed-point number, saturating if it does not fit.
5302 ///
5303 /// Any extra fractional bits are discarded, which rounds towards −∞.
5304 #[inline]
5305 fn saturating_from_fixed<F: Fixed>(src: F) -> Self {
5306 let conv = src.to_fixed_helper(Private, Self::FRAC_NBITS, Self::INT_NBITS);
5307 if conv.overflow {
5308 return if src < 0 { Self::MIN } else { Self::MAX };
5309 }
5310 let bits = if_signed_unsigned!(
5311 $Signedness,
5312 match conv.bits {
5313 Widest::Unsigned(bits) => {
5314 if (bits as $Bits) < 0 {
5315 return Self::MAX;
5316 }
5317 bits as $Bits
5318 }
5319 Widest::Negative(bits) => bits as $Bits,
5320 },
5321 match conv.bits {
5322 Widest::Unsigned(bits) => bits as $Bits,
5323 Widest::Negative(_) => {
5324 return Self::MIN;
5325 }
5326 },
5327 );
5328 Self::from_bits(bits)
5329 }
5330
5331 /// Converts a fixed-point number, wrapping if it does not fit.
5332 ///
5333 /// Any extra fractional bits are discarded, which rounds towards −∞.
5334 #[inline]
5335 fn wrapping_from_fixed<F: Fixed>(src: F) -> Self {
5336 let (wrapped, _) = FromFixed::overflowing_from_fixed(src);
5337 wrapped
5338 }
5339
5340 /// Converts a fixed-point number.
5341 ///
5342 /// Returns a [tuple] of the value and a [`bool`]
5343 /// indicating whether an overflow has occurred. On
5344 /// overflow, the wrapped value is returned.
5345 ///
5346 /// Any extra fractional bits are discarded, which rounds towards −∞.
5347 #[inline]
5348 fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool) {
5349 let conv = src.to_fixed_helper(Private, Self::FRAC_NBITS, Self::INT_NBITS);
5350 let mut new_overflow = false;
5351 let bits = if_signed_unsigned!(
5352 $Signedness,
5353 match conv.bits {
5354 Widest::Unsigned(bits) => {
5355 if (bits as $Bits) < 0 {
5356 new_overflow = true;
5357 }
5358 bits as $Bits
5359 }
5360 Widest::Negative(bits) => bits as $Bits,
5361 },
5362 match conv.bits {
5363 Widest::Unsigned(bits) => bits as $Bits,
5364 Widest::Negative(bits) => {
5365 new_overflow = true;
5366 bits as $Bits
5367 }
5368 },
5369 );
5370 (Self::from_bits(bits), conv.overflow || new_overflow)
5371 }
5372 }
5373
5374 impl<Frac: $LeEqU> ToFixed for $Fixed<Frac> {
5375 /// Converts a fixed-point number.
5376 ///
5377 /// Any extra fractional bits are discarded, which rounds towards −∞.
5378 ///
5379 /// # Panics
5380 ///
5381 /// When debug assertions are enabled, panics if the value
5382 /// does not fit. When debug assertions are not enabled,
5383 /// the wrapped value can be returned, but it is not
5384 /// considered a breaking change if in the future it
5385 /// panics; if wrapping is required use
5386 /// [`wrapping_to_fixed`] instead.
5387 ///
5388 /// [`wrapping_to_fixed`]: ToFixed::wrapping_to_fixed
5389 #[inline]
5390 fn to_fixed<F: Fixed>(self) -> F {
5391 FromFixed::from_fixed(self)
5392 }
5393
5394 /// Converts a fixed-point number if it fits, otherwise returns [`None`].
5395 ///
5396 /// Any extra fractional bits are discarded, which rounds towards −∞.
5397 #[inline]
5398 fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
5399 FromFixed::checked_from_fixed(self)
5400 }
5401
5402 /// Converts a fixed-point number, saturating if it does not fit.
5403 ///
5404 /// Any extra fractional bits are discarded, which rounds towards −∞.
5405 #[inline]
5406 fn saturating_to_fixed<F: Fixed>(self) -> F {
5407 FromFixed::saturating_from_fixed(self)
5408 }
5409
5410 /// Converts a fixed-point number, wrapping if it does not fit.
5411 ///
5412 /// Any extra fractional bits are discarded, which rounds towards −∞.
5413 #[inline]
5414 fn wrapping_to_fixed<F: Fixed>(self) -> F {
5415 FromFixed::wrapping_from_fixed(self)
5416 }
5417
5418 /// Converts a fixed-point number.
5419 ///
5420 /// Returns a [tuple] of the value and a [`bool`]
5421 /// indicating whether an overflow has occurred. On
5422 /// overflow, the wrapped value is returned.
5423 ///
5424 /// Any extra fractional bits are discarded, which rounds towards −∞.
5425 #[inline]
5426 fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
5427 FromFixed::overflowing_from_fixed(self)
5428 }
5429
5430 /// Converts a fixed-point number, panicking if it does not fit.
5431 ///
5432 /// Any extra fractional bits are discarded, which rounds towards −∞.
5433 ///
5434 /// # Panics
5435 ///
5436 /// Panics if the value does not fit, even when debug
5437 /// assertions are not enabled.
5438 #[inline]
5439 fn strict_to_fixed<F: Fixed>(self) -> F {
5440 FromFixed::strict_from_fixed(self)
5441 }
5442 }
5443
5444 if_signed! {
5445 $Signedness;
5446 impl<Frac: $LeEqU> FixedSigned for $Fixed<Frac> {
5447 const TRY_NEG_ONE: Option<Self> = Self::TRY_NEG_ONE;
5448 trait_delegate! { fn signed_bits(self) -> u32 }
5449 trait_delegate! { fn is_positive(self) -> bool }
5450 trait_delegate! { fn is_negative(self) -> bool }
5451 trait_delegate! { fn abs(self) -> Self }
5452 trait_delegate! { fn unsigned_abs(self) -> Self::Unsigned }
5453 trait_delegate! { fn unsigned_dist(self, other: Self) -> Self::Unsigned }
5454 trait_delegate! { fn signum(self) -> Self }
5455 trait_delegate! { fn add_unsigned(self, rhs: Self::Unsigned) -> Self }
5456 trait_delegate! { fn sub_unsigned(self, rhs: Self::Unsigned) -> Self }
5457 trait_delegate! { fn checked_abs(self) -> Option<Self> }
5458 trait_delegate! { fn checked_signum(self) -> Option<Self> }
5459 trait_delegate! {
5460 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>
5461 }
5462 trait_delegate! {
5463 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>
5464 }
5465 trait_delegate! { fn saturating_abs(self) -> Self }
5466 trait_delegate! { fn saturating_signum(self) -> Self }
5467 trait_delegate! { fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self }
5468 trait_delegate! { fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
5469 trait_delegate! { fn wrapping_abs(self) -> Self }
5470 trait_delegate! { fn wrapping_signum(self) -> Self }
5471 trait_delegate! { fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self }
5472 trait_delegate! { fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
5473 trait_delegate! { fn strict_abs(self) -> Self }
5474 trait_delegate! { fn strict_signum(self) -> Self }
5475 trait_delegate! { fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self }
5476 trait_delegate! { fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self }
5477 trait_delegate! { fn overflowing_abs(self) -> (Self, bool) }
5478 trait_delegate! { fn overflowing_signum(self) -> (Self, bool) }
5479 trait_delegate! {
5480 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)
5481 }
5482 trait_delegate! {
5483 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)
5484 }
5485 trait_delegate! { unsafe fn unchecked_neg(self) -> Self }
5486 }
5487 }
5488
5489 if_unsigned! {
5490 $Signedness;
5491 impl<Frac: $LeEqU> FixedUnsigned for $Fixed<Frac> {
5492 trait_delegate! { fn significant_bits(self) -> u32 }
5493 trait_delegate! { fn is_power_of_two(self) -> bool }
5494 trait_delegate! { fn highest_one(self) -> Self }
5495 trait_delegate! { fn next_power_of_two(self) -> Self }
5496 trait_delegate! { fn add_signed(self, rhs: Self::Signed) -> Self }
5497 trait_delegate! { fn sub_signed(self, rhs: Self::Signed) -> Self }
5498 trait_delegate! { fn checked_next_power_of_two(self) -> Option<Self> }
5499 trait_delegate! { fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> }
5500 trait_delegate! { fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self> }
5501 trait_delegate! { fn saturating_add_signed(self, rhs: Self::Signed) -> Self }
5502 trait_delegate! { fn saturating_sub_signed(self, rhs: Self::Signed) -> Self }
5503 trait_delegate! { fn wrapping_next_power_of_two(self) -> Self }
5504 trait_delegate! { fn wrapping_add_signed(self, rhs: Self::Signed) -> Self }
5505 trait_delegate! { fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self }
5506 trait_delegate! { fn strict_next_power_of_two(self) -> Self }
5507 trait_delegate! { fn strict_add_signed(self, rhs: Self::Signed) -> Self }
5508 trait_delegate! { fn strict_sub_signed(self, rhs: Self::Signed) -> Self }
5509 trait_delegate! {
5510 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool)
5511 }
5512 trait_delegate! {
5513 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool)
5514 }
5515 }
5516 }
5517 };
5518}
5519
5520impl_fixed! { FixedI8, FixedI8, FixedU8, LeEqU8, i8, Signed }
5521impl_fixed! { FixedI16, FixedI16, FixedU16, LeEqU16, i16, Signed }
5522impl_fixed! { FixedI32, FixedI32, FixedU32, LeEqU32, i32, Signed }
5523impl_fixed! { FixedI64, FixedI64, FixedU64, LeEqU64, i64, Signed }
5524impl_fixed! { FixedI128, FixedI128, FixedU128, LeEqU128, i128, Signed }
5525impl_fixed! { FixedU8, FixedI8, FixedU8, LeEqU8, u8, Unsigned }
5526impl_fixed! { FixedU16, FixedI16, FixedU16, LeEqU16, u16, Unsigned }
5527impl_fixed! { FixedU32, FixedI32, FixedU32, LeEqU32, u32, Unsigned }
5528impl_fixed! { FixedU64, FixedI64, FixedU64, LeEqU64, u64, Unsigned }
5529impl_fixed! { FixedU128, FixedI128, FixedU128, LeEqU128, u128, Unsigned }