Number

Enum Number 

Source
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§

§

Integer(i64)

§

Float(f64)

§

BigInteger(Box<BigInt>)

§

Rational(Box<Ratio<BigInt>>)

Implementations§

Source§

impl Number

Source

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

Source

pub fn integer(value: i64) -> Number

Create an integer number

§Examples
use mathhook_core::Number;

let num = Number::integer(42);
Source

pub fn float(value: f64) -> Number

Create a float number

§Examples
use mathhook_core::Number;

let num = Number::float(3.14);
Source

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);
Source

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());
Source

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());
Source

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

§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§

type Output = Result<Number, MathError>

The resulting type after applying the + operator.
Source§

fn add(self, other: Number) -> Result<Number, MathError>

Performs the + operation. Read more
Source§

impl Clone for Number

Source§

fn clone(&self) -> Number

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Number

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Number

Source§

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 Display for Number

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

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§

type Output = Result<Number, MathError>

The resulting type after applying the / operator.
Source§

fn div(self, other: Number) -> Result<Number, MathError>

Performs the / operation. Read more
Source§

impl From<f64> for Number

Source§

fn from(value: f64) -> Number

Converts to this type from the input type.
Source§

impl From<i32> for Number

Source§

fn from(value: i32) -> Number

Converts to this type from the input type.
Source§

impl From<i64> for Number

Source§

fn from(value: i64) -> Number

Converts to this type from the input type.
Source§

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§

type Output = Result<Number, MathError>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Number) -> Result<Number, MathError>

Performs the * operation. Read more
Source§

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§

type Output = Result<Number, MathError>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Result<Number, MathError>

Performs the unary - operation. Read more
Source§

impl PartialEq for Number

Source§

fn eq(&self, other: &Number) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Number

Source§

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

§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));
Source§

type Output = Result<Number, MathError>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Number) -> Result<Number, MathError>

Performs the - operation. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,