pub struct NumberRef<'a> { /* private fields */ }Expand description
Borrows or copies a numeric value without depending on a runtime value enum.
Values are constructed through From implementations. The internal
representation is intentionally private, so callers cannot create marker
or otherwise non-numeric states.
§Type Parameters
'a- Lifetime of a borrowed arbitrary-precision numeric value.
§Examples
use qubit_datatype::NumberRef;
let number = NumberRef::from(42_i64);
assert!(number.is_integer());use std::cmp::Ordering;
use qubit_datatype::NumberRef;
use qubit_datatype::NumericComparisonPolicy;
let integer = NumberRef::from((1_u64 << 53) + 1);
let float = NumberRef::from((1_u64 << 53) as f64);
assert_eq!(integer.compare(float, NumericComparisonPolicy::Exact), Some(Ordering::Greater));
assert_eq!(integer.compare(float, NumericComparisonPolicy::Approximate), Some(Ordering::Equal));
assert_eq!(integer.compare(NumberRef::from(f64::NAN), NumericComparisonPolicy::Exact), None);use qubit_datatype::NumberRef;
let _ = NumberRef::Int8(1);Implementations§
Source§impl<'a> NumberRef<'a>
impl<'a> NumberRef<'a>
Sourcepub fn is_integer(self) -> bool
pub fn is_integer(self) -> bool
Reports whether this value is an integer representation.
§Returns
true for fixed-width integer values.
Sourcepub fn is_float(self) -> bool
pub fn is_float(self) -> bool
Reports whether this value is a primitive floating-point value.
§Returns
true for f32 and f64 values.
Sourcepub fn is_big_integer(self) -> bool
pub fn is_big_integer(self) -> bool
Reports whether this value is an arbitrary-precision integer.
§Returns
true for BigInt values when the big-integer feature is enabled;
otherwise, false.
Sourcepub fn is_big_decimal(self) -> bool
pub fn is_big_decimal(self) -> bool
Reports whether this value is an arbitrary-precision decimal.
§Returns
true for BigDecimal values when the big-decimal feature is
enabled; otherwise, false.
Sourcepub fn is_infinite(self) -> bool
pub fn is_infinite(self) -> bool
Reports whether this value is an infinite primitive float.
§Returns
true for positive or negative infinity; otherwise, false.
Sourcepub fn is_finite(self) -> bool
pub fn is_finite(self) -> bool
Reports whether this value is finite.
§Returns
true for all integers, arbitrary-precision values, and finite
primitive floats; otherwise, false.
Sourcepub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Reports whether this value is mathematically zero.
§Returns
true for integer zero and both positive and negative floating-point
zero; otherwise, false.
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Reports whether this value is greater than zero.
§Returns
true for positive finite or infinite values; zero, negative values,
and NaN return false.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Reports whether this value is less than zero.
§Returns
true for negative finite or infinite values; zero, positive values,
and NaN return false.
Sourcepub fn compare(
self,
right: NumberRef<'_>,
policy: NumericComparisonPolicy,
) -> Option<Ordering>
pub fn compare( self, right: NumberRef<'_>, policy: NumericComparisonPolicy, ) -> Option<Ordering>
Compares this value with right under an explicit numeric policy.
Fixed-width comparisons do not require heap allocation. Some paths
involving BigInt or BigDecimal may clone coefficients or construct
exact rational or decimal values, with cost depending on the operand
representations. Direct comparisons between some large-number
representations avoid that work. Conversion limits and session budgets
do not apply to this independent comparison; constrain untrusted number
representations at the input boundary when their size must be bounded.
See the type-level examples for exact, approximate, and NaN comparisons. Approximate comparison is not transitive across mixed representations; do not use it for sorting or ordered keys.
§Parameters
right- Right numeric operand.policy- Exact or approximate comparison policy.
§Returns
The numeric ordering, or None when either operand is NaN.