pub enum Number {
Integer(i64),
Float(f64),
BigInteger(Box<BigInt>),
Rational(Box<Ratio<BigInt>>),
}Expand description
Unified number type supporting integers, rationals, and floats
Variants§
Implementations§
Source§impl Number
impl Number
Sourcepub fn pow(&self, exponent: &Number) -> Result<Number, MathError>
pub fn pow(&self, exponent: &Number) -> Result<Number, MathError>
Power operation with overflow checking
Computes self raised to the power of exponent. For integer bases and positive integer exponents, uses checked arithmetic with promotion to BigInt on overflow. For other cases, converts to float and checks for infinity/NaN.
§Arguments
exponent- The exponent to raise self to
§Examples
use mathhook_core::Number;
let base = Number::integer(2);
let exp = Number::integer(3);
let result = base.pow(&exp).unwrap();
assert_eq!(result, Number::integer(8));§Errors
Returns MathError::NumericOverflow if:
- Float power results in infinity or NaN
- Exponentiation produces non-representable result
Source§impl Number
impl Number
Sourcepub fn rational(value: Ratio<BigInt>) -> Number
pub fn rational(value: Ratio<BigInt>) -> Number
Create a rational number
§Examples
use mathhook_core::Number;
use num_rational::BigRational;
use num_bigint::BigInt;
let rational = BigRational::new(BigInt::from(3), BigInt::from(4));
let num = Number::rational(rational);Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Check if the number is zero
§Examples
use mathhook_core::Number;
let zero = Number::integer(0);
assert!(zero.is_zero());Sourcepub fn is_one(&self) -> bool
pub fn is_one(&self) -> bool
Check if the number is one
§Examples
use mathhook_core::Number;
let one = Number::integer(1);
assert!(one.is_one());Sourcepub fn is_negative_one(&self) -> bool
pub fn is_negative_one(&self) -> bool
Check if the number is negative one
§Examples
use mathhook_core::Number;
let neg_one = Number::integer(-1);
assert!(neg_one.is_negative_one());Trait Implementations§
Source§impl Add for Number
Addition with overflow checking and promotion to BigInt
impl Add for Number
Addition with overflow checking and promotion to BigInt
§Examples
use mathhook_core::Number;
let a = Number::integer(5);
let b = Number::integer(3);
let result = (a + b).unwrap();
assert_eq!(result, Number::integer(8));Source§impl<'de> Deserialize<'de> for Number
impl<'de> Deserialize<'de> for Number
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Number, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Number, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Div for Number
Division with division by zero check and automatic promotion to rational
impl Div for Number
Division with division by zero check and automatic promotion to rational
§Examples
use mathhook_core::Number;
let a = Number::integer(10);
let b = Number::integer(2);
let result = (a / b).unwrap();
assert_eq!(result, Number::integer(5));Source§impl Mul for Number
Multiplication with overflow checking and promotion to BigInt
impl Mul for Number
Multiplication with overflow checking and promotion to BigInt
§Examples
use mathhook_core::Number;
let a = Number::integer(6);
let b = Number::integer(7);
let result = (a * b).unwrap();
assert_eq!(result, Number::integer(42));Source§impl Neg for Number
Negation with overflow checking
impl Neg for Number
Negation with overflow checking
§Examples
use mathhook_core::Number;
let a = Number::integer(5);
let result = (-a).unwrap();
assert_eq!(result, Number::integer(-5));Source§impl Serialize for Number
impl Serialize for Number
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
Source§impl Sub for Number
Subtraction with overflow checking and promotion to BigInt
impl Sub for Number
Subtraction with overflow checking and promotion to BigInt
§Examples
use mathhook_core::Number;
let a = Number::integer(10);
let b = Number::integer(3);
let result = (a - b).unwrap();
assert_eq!(result, Number::integer(7));impl StructuralPartialEq for Number
Auto Trait Implementations§
impl Freeze for Number
impl RefUnwindSafe for Number
impl Send for Number
impl Sync for Number
impl Unpin for Number
impl UnwindSafe for Number
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more