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

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_native("1.234")?;
let b = DBig::from_str_native("6.789")?;
assert_eq!(context.add(&a.repr(), &b.repr()), Inexact(DBig::from_str_native("8.0")?, NoOp));

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_native("1.234")?;
let b = DBig::from_str_native("6.789")?;
assert_eq!(
    context.sub(&a.repr(), &b.repr()),
    Inexact(DBig::from_str_native("-5.6")?, SubOne)
);

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_native("-12")?));
assert_eq!(
    context.convert_int::<10>(5678.into()),
    Inexact(DBig::from_str_native("5.7e3")?, AddOne)
);

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_native("-1.234")?;
let b = DBig::from_str_native("6.789")?;
assert_eq!(context.div(&a.repr(), &b.repr()), Inexact(DBig::from_str_native("-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.

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.

Raise the floating point number to an floating point power under this context.

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.

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

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

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_native("1.234")?;
assert_eq!(context.ln(&a.repr()), Inexact(DBig::from_str_native("0.21")?, NoOp));

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_native("0.1234")?;
assert_eq!(context.ln_1p(&a.repr()), Inexact(DBig::from_str_native("0.12")?, AddOne));

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_native("-1.234")?;
let b = DBig::from_str_native("6.789")?;
assert_eq!(
    context.mul(&a.repr(), &b.repr()),
    Inexact(DBig::from_str_native("-8.4")?, SubOne)
);

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_native("-1.234")?;
assert_eq!(context.square(&a.repr()), Inexact(DBig::from_str_native("1.5")?, NoOp));

Create a float operation context with the given precision limit.

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

Get the precision limited from the context

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_native("1.23")?;
assert_eq!(context.sqrt(&a.repr()), Inexact(DBig::from_str_native("1.1")?, NoOp));
Panics

Panics if the precision is unlimited.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.