pub struct Context<RoundingMode: Round> { /* private fields */ }Expand description
The context containing runtime information for the floating point number and its operations.
The context currently consists of a precision limit and a rounding mode. All the operation
associated with the context will be precise to the full precision (|error| < 1 ulp).
The rounding result returned from the functions tells additional error information, see
the rounding mode module for details.
§Precision
The precision limit determine the number of significant digits in the float number.
For binary operations, the result will have the higher one between the precisions of two operands.
If the precision is set to 0, then the precision is unlimited during operations. Be cautious to use unlimited precision because it can leads to very huge significands. Unlimited precision is forbidden for some operations where the result is always inexact.
§Rounding Mode
The rounding mode determines the rounding behavior of the float operations.
See the rounding mode module for built-in rounding modes. Users can implement custom rounding mode by implementing the Round trait, but this is discouraged since in the future we might restrict the rounding modes to be chosen from the the built-in modes.
For binary operations, the two oprands must have the same rounding mode.
Implementations§
Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn add<const B: Word>(
&self,
lhs: &Repr<B>,
rhs: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn add<const B: Word>( &self, lhs: &Repr<B>, rhs: &Repr<B>, ) -> Rounded<FBig<R, B>>
Add two floating point numbers under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("1.234")?;
let b = DBig::from_str("6.789")?;
assert_eq!(context.add(&a.repr(), &b.repr()), Inexact(DBig::from_str("8.0")?, NoOp));Sourcepub fn sub<const B: Word>(
&self,
lhs: &Repr<B>,
rhs: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn sub<const B: Word>( &self, lhs: &Repr<B>, rhs: &Repr<B>, ) -> Rounded<FBig<R, B>>
Subtract two floating point numbers under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("1.234")?;
let b = DBig::from_str("6.789")?;
assert_eq!(
context.sub(&a.repr(), &b.repr()),
Inexact(DBig::from_str("-5.6")?, SubOne)
);Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn convert_int<const B: Word>(&self, n: IBig) -> Rounded<FBig<R, B>>
pub fn convert_int<const B: Word>(&self, n: IBig) -> Rounded<FBig<R, B>>
Convert an IBig instance to a FBig instance with precision and rounding given by the context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
assert_eq!(context.convert_int::<10>((-12).into()), Exact(DBig::from_str("-12")?));
assert_eq!(
context.convert_int::<10>(5678.into()),
Inexact(DBig::from_str("5.7e3")?, AddOne)
);Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn div<const B: Word>(
&self,
lhs: &Repr<B>,
rhs: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn div<const B: Word>( &self, lhs: &Repr<B>, rhs: &Repr<B>, ) -> Rounded<FBig<R, B>>
Divide two floating point numbers under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("-1.234")?;
let b = DBig::from_str("6.789")?;
assert_eq!(context.div(&a.repr(), &b.repr()), Inexact(DBig::from_str("-0.18")?, NoOp));§Euclidean Division
To do euclidean division on the float numbers (get an integer quotient and remainder, equivalent to C99’s
fmod and remquo), please use the methods provided by traits DivEuclid, RemEuclid and DivRemEuclid.
Sourcepub fn rem<const B: Word>(
&self,
lhs: &Repr<B>,
rhs: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn rem<const B: Word>( &self, lhs: &Repr<B>, rhs: &Repr<B>, ) -> Rounded<FBig<R, B>>
Calculate the remainder of ⌈lhs / rhs⌋.
The remainder is calculated as r = lhs - ⌈lhs / rhs⌋ * rhs, the division rounds to the nearest and ties to away.
So if n = (lhs / rhs).round(), then lhs == n * rhs + r (given enough precision).
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(3);
let a = DBig::from_str("6.789")?;
let b = DBig::from_str("-1.234")?;
assert_eq!(context.rem(&a.repr(), &b.repr()), Exact(DBig::from_str("-0.615")?));Sourcepub fn inv<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn inv<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
Compute the multiplicative inverse of an FBig
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("-1.234")?;
assert_eq!(context.inv(&a.repr()), Inexact(DBig::from_str("-0.81")?, NoOp));Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn powi<const B: Word>(
&self,
base: &Repr<B>,
exp: IBig,
) -> Rounded<FBig<R, B>>
pub fn powi<const B: Word>( &self, base: &Repr<B>, exp: IBig, ) -> Rounded<FBig<R, B>>
Raise the floating point number to an integer power under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str_native("-1.234")?;
assert_eq!(context.powi(&a.repr(), 10.into()), Inexact(DBig::from_str_native("8.2")?, AddOne));§Panics
Panics if the precision is unlimited and the exponent is negative. In this case, the exact result is likely to have infinite digits.
Sourcepub fn powf<const B: Word>(
&self,
base: &Repr<B>,
exp: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn powf<const B: Word>( &self, base: &Repr<B>, exp: &Repr<B>, ) -> Rounded<FBig<R, B>>
Raise the floating point number to an floating point power under this context.
Note that this method will not rely on FBig::powi even if the exp is actually an integer.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let x = DBig::from_str_native("1.23")?;
let y = DBig::from_str_native("-4.56")?;
assert_eq!(context.powf(&x.repr(), &y.repr()), Inexact(DBig::from_str_native("0.39")?, AddOne));§Panics
Panics if the precision is unlimited.
Sourcepub fn exp<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn exp<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the exponential function (eˣ) on the floating point number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str_native("-1.234")?;
assert_eq!(context.exp(&a.repr()), Inexact(DBig::from_str_native("0.29")?, NoOp));Sourcepub fn exp_m1<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn exp_m1<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the exponential minus one function (eˣ-1) on the floating point number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str_native("-0.1234")?;
assert_eq!(context.exp_m1(&a.repr()), Inexact(DBig::from_str_native("-0.12")?, SubOne));Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn ln<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn ln<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the natural logarithm function (log(x)) on the float number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("1.234")?;
assert_eq!(context.ln(&a.repr()), Inexact(DBig::from_str("0.21")?, NoOp));Sourcepub fn ln_1p<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn ln_1p<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the natural logarithm function (log(x+1)) on the float number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("0.1234")?;
assert_eq!(context.ln_1p(&a.repr()), Inexact(DBig::from_str("0.12")?, AddOne));Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn pi<const B: Word>(&self) -> Rounded<FBig<R, B>>
pub fn pi<const B: Word>(&self) -> Rounded<FBig<R, B>>
Calculate π using the Chudnovsky algorithm with binary splitting.
The Chudnovsky algorithm is one of the most efficient methods for high-precision π calculation, providing ~14.18 decimal digits per term.
§Methodology
We use Binary Splitting to evaluate the series. This technique transforms the linear-time summation into a recursive tree evaluation. By combining terms into large products, it allows the library to leverage fast multiplication algorithms (like Toom-3 or FFT) as the numbers grow, leading to significant performance gains over simple iterative summation.
// TODO: consider adding a static cache for π at common precisions.
Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn sin<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
pub fn sin<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
Calculate the sine of the floating point representation.
Sourcepub fn cos<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
pub fn cos<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
Calculate the cosine of the floating point representation.
Sourcepub fn sin_cos<const B: Word>(&self, x: &Repr<B>) -> (FpResult<B>, FpResult<B>)
pub fn sin_cos<const B: Word>(&self, x: &Repr<B>) -> (FpResult<B>, FpResult<B>)
Calculate both the sine and cosine of the floating point representation.
This is more efficient than calling sin and cos separately.
Sourcepub fn tan<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
pub fn tan<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
Calculate the tangent of the floating point representation.
§Note
Near odd multiples of π/2, the result returns Infinite.
Sourcepub fn asin<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
pub fn asin<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
Calculate the arcsine of the floating point representation.
§Methodology
Uses the identity: asin(x) = atan(x / sqrt(1 - x^2))
Returns NaN if |x| > 1.
Sourcepub fn acos<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
pub fn acos<const B: Word>(&self, x: &Repr<B>) -> FpResult<B>
Calculate the arccosine of the floating point representation.
§Methodology
Uses the identity: acos(x) = pi/2 - asin(x).
Higher precision is used internally to avoid catastrophic cancellation near x ≈ 1.
Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn mul<const B: Word>(
&self,
lhs: &Repr<B>,
rhs: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn mul<const B: Word>( &self, lhs: &Repr<B>, rhs: &Repr<B>, ) -> Rounded<FBig<R, B>>
Multiply two floating point numbers under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("-1.234")?;
let b = DBig::from_str("6.789")?;
assert_eq!(
context.mul(&a.repr(), &b.repr()),
Inexact(DBig::from_str("-8.4")?, SubOne)
);Sourcepub fn sqr<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn sqr<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the square of the floating point number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("-1.234")?;
assert_eq!(context.sqr(&a.repr()), Inexact(DBig::from_str("1.5")?, NoOp));Sourcepub fn cubic<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn cubic<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the cubic of the floating point number under this context.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("-1.234")?;
assert_eq!(context.cubic(&a.repr()), Inexact(DBig::from_str("-1.9")?, SubOne));Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub const fn new(precision: usize) -> Self
pub const fn new(precision: usize) -> Self
Create a float operation context with the given precision limit.
Sourcepub const fn max(lhs: Self, rhs: Self) -> Self
pub const fn max(lhs: Self, rhs: Self) -> Self
Create a float operation context with the higher precision from the two context inputs.
§Examples
use dashu_float::{Context, round::mode::Zero};
let ctxt1 = Context::<Zero>::new(2);
let ctxt2 = Context::<Zero>::new(5);
assert_eq!(Context::max(ctxt1, ctxt2).precision(), 5);Source§impl<R: Round> Context<R>
impl<R: Round> Context<R>
Sourcepub fn sqrt<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn sqrt<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the square root of the floating point number.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("1.23")?;
assert_eq!(context.sqrt(&a.repr()), Inexact(DBig::from_str("1.1")?, NoOp));§Panics
Panics if the precision is unlimited.
Sourcepub fn cbrt<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
pub fn cbrt<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>
Calculate the cubic root of the floating point number.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("8")?;
assert_eq!(context.cbrt(&a.repr()), Exact(DBig::from_str("2")?));§Panics
Panics if the precision is unlimited.
Sourcepub fn nth_root<const B: Word>(
&self,
n: usize,
x: &Repr<B>,
) -> Rounded<FBig<R, B>>
pub fn nth_root<const B: Word>( &self, n: usize, x: &Repr<B>, ) -> Rounded<FBig<R, B>>
Calculate the nth root of the floating point number.
§Examples
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let context = Context::<HalfAway>::new(2);
let a = DBig::from_str("27")?;
assert_eq!(context.nth_root(3, &a.repr()), Exact(DBig::from_str("3")?));§Panics
Panics if n is zero, if the precision is unlimited, or if n is even and x is negative.
Trait Implementations§
impl<RoundingMode: Copy + Round> Copy for Context<RoundingMode>
Auto Trait Implementations§
impl<RoundingMode> Freeze for Context<RoundingMode>
impl<RoundingMode> RefUnwindSafe for Context<RoundingMode>where
RoundingMode: RefUnwindSafe,
impl<RoundingMode> Send for Context<RoundingMode>where
RoundingMode: Send,
impl<RoundingMode> Sync for Context<RoundingMode>where
RoundingMode: Sync,
impl<RoundingMode> Unpin for Context<RoundingMode>where
RoundingMode: Unpin,
impl<RoundingMode> UnsafeUnpin for Context<RoundingMode>
impl<RoundingMode> UnwindSafe for Context<RoundingMode>where
RoundingMode: UnwindSafe,
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
&self to an expression for Diesel’s query builder. Read moreSource§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read more