DoubleDouble

Struct DoubleDouble 

Source
pub struct DoubleDouble(f64, f64);

Tuple Fields§

§0: f64§1: f64

Implementations§

Source§

impl DoubleDouble

Source

fn renormalize(a0: f64, a1: f64, a2: f64) -> Self

Examples found in repository?
crates/competitive/src/num/double_double.rs (line 74)
71    fn add(self, rhs: f64) -> Self::Output {
72        let (t0, e) = two_sum(self.0, rhs);
73        let (t1, t2) = two_sum(self.1, e);
74        Self::renormalize(t0, t1, t2)
75    }
76}
77
78impl Add<DoubleDouble> for DoubleDouble {
79    type Output = Self;
80    fn add(self, rhs: Self) -> Self::Output {
81        let (t0, e) = two_sum(self.0, rhs.0);
82        let (t1, t2) = three_two_sum(self.1, rhs.1, e);
83        Self::renormalize(t0, t1, t2)
84    }
85}
86
87impl Sub for DoubleDouble {
88    type Output = Self;
89    fn sub(self, rhs: Self) -> Self::Output {
90        self + -rhs
91    }
92}
93
94impl Neg for DoubleDouble {
95    type Output = Self;
96    fn neg(self) -> Self::Output {
97        Self(-self.0, -self.1)
98    }
99}
100
101impl Mul<f64> for DoubleDouble {
102    type Output = Self;
103    fn mul(self, rhs: f64) -> Self::Output {
104        let (t0, e0) = two_prod(self.0, rhs);
105        let p1 = self.1 * rhs;
106        let (t1, t2) = two_sum(p1, e0);
107        Self::renormalize(t0, t1, t2)
108    }
109}
110
111impl Mul<DoubleDouble> for DoubleDouble {
112    type Output = Self;
113    fn mul(self, rhs: Self) -> Self::Output {
114        let (t0, q00) = two_prod(self.0, rhs.0);
115        let (p01, q01) = two_prod(self.0, rhs.1);
116        let (p10, q10) = two_prod(self.1, rhs.0);
117        let p11 = self.1 * rhs.1;
118        let (t1, e1) = three_two_sum(q00, p01, p10);
119        let t2 = e1 + q01 + q10 + p11;
120        Self::renormalize(t0, t1, t2)
121    }
122}
123
124impl Div<DoubleDouble> for DoubleDouble {
125    type Output = Self;
126    fn div(self, rhs: Self) -> Self::Output {
127        let q0 = self.0 / rhs.0;
128        let r = self - rhs * q0;
129        let q1 = r.0 / rhs.0;
130        let r = r - rhs * q1;
131        let q2 = r.0 / rhs.0;
132        Self::renormalize(q0, q1, q2)
133    }
134}
135
136impl From<DoubleDouble> for f64 {
137    fn from(x: DoubleDouble) -> f64 {
138        x.1 + x.0
139    }
140}
141
142impl From<DoubleDouble> for i64 {
143    fn from(mut x: DoubleDouble) -> i64 {
144        let is_neg = x.0.is_sign_negative();
145        if is_neg {
146            x = -x;
147        }
148        let mut i = 0i64;
149        for k in (1..64).rev() {
150            let t = (k as f64).exp2();
151            if x.0 >= t {
152                x = x + -t;
153                i += 1 << k;
154            }
155        }
156        i += x.0.round() as i64;
157        if is_neg {
158            i = -i;
159        }
160        i
161    }
162}
163
164impl From<f64> for DoubleDouble {
165    fn from(x: f64) -> Self {
166        Self(x, 0.)
167    }
168}
169
170impl Display for DoubleDouble {
171    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172        write!(f, "{}", Decimal::from(self.0) + Decimal::from(self.1))
173    }
174}
175
176#[derive(Debug, Clone)]
177pub enum ParseDoubleDoubleError {
178    ParseFloatError(ParseFloatError),
179    ParseDecimalError(super::decimal::convert::ParseDecimalError),
180}
181
182impl From<ParseFloatError> for ParseDoubleDoubleError {
183    fn from(e: ParseFloatError) -> Self {
184        Self::ParseFloatError(e)
185    }
186}
187
188impl From<super::decimal::convert::ParseDecimalError> for ParseDoubleDoubleError {
189    fn from(e: super::decimal::convert::ParseDecimalError) -> Self {
190        Self::ParseDecimalError(e)
191    }
192}
193
194impl FromStr for DoubleDouble {
195    type Err = ParseDoubleDoubleError;
196    fn from_str(s: &str) -> Result<Self, Self::Err> {
197        let f0: f64 = s.parse()?;
198        let d1 = Decimal::from_str(s)? - Decimal::from(f0);
199        let f1: f64 = d1.to_string().parse()?;
200        Ok(Self::renormalize(f0, f1, 0.))
201    }
Source§

impl DoubleDouble

Source

pub fn sqrt(self) -> Self

Source

pub fn abs(self) -> Self

Source

fn div2(self, rhs: f64) -> Self

Examples found in repository?
crates/competitive/src/num/double_double.rs (line 268)
263    pub fn sqrt(self) -> Self {
264        if self.is_zero() {
265            return Self::from(0.);
266        }
267        let x = Self::from(1. / self.0.sqrt());
268        let x = x + x * (Self::from(1.) - self * x * x).div2(2.);
269        let x = x + x * (Self::from(1.) - self * x * x).div2(2.);
270        let x = x + x * (Self::from(1.) - self * x * x).div2(2.);
271        x * self
272    }

Trait Implementations§

Source§

impl Add<f64> for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f64) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Bounded for DoubleDouble

Source§

fn maximum() -> Self

Source§

fn minimum() -> Self

Source§

fn is_maximum(&self) -> bool

Source§

fn is_minimum(&self) -> bool

Source§

fn set_maximum(&mut self)

Source§

fn set_minimum(&mut self)

Source§

impl Clone for DoubleDouble

Source§

fn clone(&self) -> DoubleDouble

Returns a duplicate 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 Debug for DoubleDouble

Source§

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

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

impl Default for DoubleDouble

Source§

fn default() -> DoubleDouble

Returns the “default value” for a type. Read more
Source§

impl Display for DoubleDouble

Source§

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

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

impl Div for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl From<DoubleDouble> for f64

Source§

fn from(x: DoubleDouble) -> f64

Converts to this type from the input type.
Source§

impl From<DoubleDouble> for i64

Source§

fn from(x: DoubleDouble) -> i64

Converts to this type from the input type.
Source§

impl From<f64> for DoubleDouble

Source§

fn from(x: f64) -> Self

Converts to this type from the input type.
Source§

impl FromStr for DoubleDouble

Source§

type Err = ParseDoubleDoubleError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl IterScan for DoubleDouble

Source§

type Output = DoubleDouble

Source§

fn scan<'a, I: Iterator<Item = &'a str>>(iter: &mut I) -> Option<Self::Output>

Source§

impl Mul<f64> for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl One for DoubleDouble

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

Source§

impl Ord for DoubleDouble

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for DoubleDouble

Source§

fn eq(&self, other: &DoubleDouble) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for DoubleDouble

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for DoubleDouble

Source§

type Output = DoubleDouble

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Zero for DoubleDouble

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool
where Self: PartialEq,

Source§

fn set_zero(&mut self)

Source§

impl Copy for DoubleDouble

Source§

impl Eq for DoubleDouble

Source§

impl StructuralPartialEq for DoubleDouble

Auto Trait Implementations§

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> AsTotalOrd for T
where T: PartialOrd,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PartialOrdExt for T
where T: PartialOrd,

Source§

fn chmin(&mut self, other: T)

Source§

fn chmax(&mut self, other: T)

Source§

fn minmax(self, other: T) -> (T, T)

Source§

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

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.