Struct dashu_float::Context
source · 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 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.
Trait Implementations§
impl<RoundingMode: Copy + Round> Copy for Context<RoundingMode>
Auto Trait Implementations§
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> UnwindSafe for Context<RoundingMode>where
RoundingMode: UnwindSafe,
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
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
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