Skip to content

Commit

Permalink
Merge pull request #125 from cuviper/consts
Browse files Browse the repository at this point in the history
Add const ZERO/ONE/I and impl ConstZero/ConstOne
  • Loading branch information
cuviper committed May 7, 2024
2 parents f66164b + 2045f2d commit 612900a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -24,7 +24,7 @@ optional = true
version = "1"

[dependencies.num-traits]
version = "0.2.11"
version = "0.2.18"
default-features = false
features = ["i128"]

Expand Down
27 changes: 25 additions & 2 deletions src/lib.rs
Expand Up @@ -30,7 +30,7 @@ use core::str::FromStr;
#[cfg(feature = "std")]
use std::error::Error;

use num_traits::{Inv, MulAdd, Num, One, Pow, Signed, Zero};
use num_traits::{ConstOne, ConstZero, Inv, MulAdd, Num, One, Pow, Signed, Zero};

use num_traits::float::FloatCore;
#[cfg(any(feature = "std", feature = "libm"))]
Expand Down Expand Up @@ -104,7 +104,9 @@ impl<T> Complex<T> {
}

impl<T: Clone + Num> Complex<T> {
/// Returns imaginary unit
/// Returns the imaginary unit.
///
/// See also [`Complex::I`].
#[inline]
pub fn i() -> Self {
Self::new(T::zero(), T::one())
Expand Down Expand Up @@ -1146,6 +1148,15 @@ impl<T: Clone + Num> Rem<T> for Complex<T> {
real_arithmetic!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64);

// constants
impl<T: ConstZero> Complex<T> {
/// A constant `Complex` 0.
pub const ZERO: Self = Self::new(T::ZERO, T::ZERO);
}

impl<T: Clone + Num + ConstZero> ConstZero for Complex<T> {
const ZERO: Self = Self::ZERO;
}

impl<T: Clone + Num> Zero for Complex<T> {
#[inline]
fn zero() -> Self {
Expand All @@ -1164,6 +1175,18 @@ impl<T: Clone + Num> Zero for Complex<T> {
}
}

impl<T: ConstOne + ConstZero> Complex<T> {
/// A constant `Complex` 1.
pub const ONE: Self = Self::new(T::ONE, T::ZERO);

/// A constant `Complex` _i_, the imaginary unit.
pub const I: Self = Self::new(T::ZERO, T::ONE);
}

impl<T: Clone + Num + ConstOne + ConstZero> ConstOne for Complex<T> {
const ONE: Self = Self::ONE;
}

impl<T: Clone + Num> One for Complex<T> {
#[inline]
fn one() -> Self {
Expand Down

0 comments on commit 612900a

Please sign in to comment.