Skip to main content

primitive_float_positive_difference_rational

Function primitive_float_positive_difference_rational 

Source
pub fn primitive_float_positive_difference_rational<T>(x: T, y: &Rational) -> T
where Float: From<T> + PartialOrd<T>, for<'a> T: ExactFrom<&'a Float> + PrimitiveFloat,
Expand description

Computes the positive difference of a primitive float and a Rational — $x-y$ if $x>y$, and $+0.0$ otherwise — correctly rounding the result to the nearest value.

The comparison and the difference are both exact: the Rational operand is never rounded before use, so the correct branch is always chosen and the winning difference is correctly rounded.

§Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is y.significant_bits().

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::positive_difference::*;
use malachite_q::Rational;

let d = primitive_float_positive_difference_rational(3.0, &Rational::from_signeds(1, 3));
assert_eq!(NiceFloat(d), NiceFloat(2.6666666666666665));