Trait rug::ops::AddFromRound
source · pub trait AddFromRound<Lhs = Self> {
type Round;
type Ordering;
// Required method
fn add_from_round(&mut self, lhs: Lhs, round: Self::Round) -> Self::Ordering;
}
Expand description
Compound addition and assignment to the rhs operand with a specified rounding method.
Examples
use core::cmp::Ordering;
use rug::float::Round;
use rug::ops::{AddAssignRound, AddFromRound};
use rug::Float;
struct F(f64);
impl AddFromRound<f64> for F {
type Round = Round;
type Ordering = Ordering;
fn add_from_round(&mut self, lhs: f64, round: Round) -> Ordering {
let mut f = Float::with_val(53, lhs);
let dir = f.add_assign_round(self.0, round);
self.0 = f.to_f64();
dir
}
}
let mut f = F(5.0);
let dir = f.add_from_round(3.0, Round::Nearest);
// 3.0 + 5.0 = 8.0
assert_eq!(f.0, 8.0);
assert_eq!(dir, Ordering::Equal);
Required Associated Types§
Required Methods§
sourcefn add_from_round(&mut self, lhs: Lhs, round: Self::Round) -> Self::Ordering
fn add_from_round(&mut self, lhs: Lhs, round: Self::Round) -> Self::Ordering
Performs the addition.
Examples
use core::cmp::Ordering;
use rug::float::Round;
use rug::ops::AddFromRound;
use rug::Float;
// only four significant bits
let mut f = Float::with_val(4, -0.3);
let dir = f.add_from_round(-3, Round::Nearest);
// -3.3 rounded up to -3.25
assert_eq!(f, -3.25);
assert_eq!(dir, Ordering::Greater);