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>

source

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

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>

source

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>

source

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.

source

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")?));
source

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>

source

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.

source

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.

source

pub fn exp<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>>

Calculate the exponential function () 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));
source

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>

source

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

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>

source

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

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

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>

source

pub const fn new(precision: usize) -> Self

Create a float operation context with the given precision limit.

source

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

pub const fn precision(&self) -> usize

Get the precision limited from the context

source§

impl<R: Round> Context<R>

source

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§

source§

impl<RoundingMode: Clone + Round> Clone for Context<RoundingMode>

source§

fn clone(&self) -> Context<RoundingMode>

Returns a copy 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<R: Round> Debug for Context<R>

source§

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

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

impl<R: Round> Zeroize for Context<R>

source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
source§

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> 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> 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> IntoSql for T

source§

fn into_sql<T>(self) -> Self::Expression
where Self: AsExpression<T> + Sized,

Convert self to an expression for Diesel’s query builder. Read more
source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression

Convert &self to an expression for Diesel’s query builder. Read more
source§

impl<T> IntoSql for T

source§

fn into_sql<T>(self) -> Self::Expression

Convert self to an expression for Diesel’s query builder. Read more
source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression

Convert &self to an expression for Diesel’s query builder. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V