Struct malachite_float::ComparableFloatRef
source · pub struct ComparableFloatRef<'a>(pub &'a Float);
Expand description
ComparableFloatRef
is a wrapper around a Float
, taking the Float
be reference.
See the ComparableFloat
documentation for details.
Tuple Fields§
§0: &'a Float
Methods from Deref<Target = Float>§
sourcepub fn abs_negative_zero_ref(&self) -> Float
pub fn abs_negative_zero_ref(&self) -> Float
If self
is negative zero, returns positive zero; otherwise, returns self
, taking self
by reference.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
};
use malachite_float::{ComparableFloat, Float};
assert_eq!(
ComparableFloat(Float::NAN.abs_negative_zero_ref()),
ComparableFloat(Float::NAN)
);
assert_eq!(Float::INFINITY.abs_negative_zero_ref(), Float::INFINITY);
assert_eq!(
Float::NEGATIVE_INFINITY.abs_negative_zero_ref(),
Float::NEGATIVE_INFINITY
);
assert_eq!(
ComparableFloat(Float::ZERO.abs_negative_zero_ref()),
ComparableFloat(Float::ZERO)
);
assert_eq!(
ComparableFloat(Float::NEGATIVE_ZERO.abs_negative_zero_ref()),
ComparableFloat(Float::ZERO)
);
assert_eq!(Float::ONE.abs_negative_zero_ref(), Float::ONE);
assert_eq!(
Float::NEGATIVE_ONE.abs_negative_zero_ref(),
Float::NEGATIVE_ONE
);
sourcepub fn add_prec_round_ref_val(
&self,
other: Float,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_prec_round_ref_val( &self, other: Float, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Adds two Float
s, rounding the result to the specified precision and with the specified
rounding mode. The first Float
is taken by reference and the second by value. An
Ordering
is also returned, indicating whether the rounded sum is less than, equal to, or
greater than the exact sum. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\infty,-\infty,p,m)=f(-\infty,\infty,p,m)= \text{NaN}$
- $f(\infty,x,p,m)=f(x,\infty,p,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p,m)=f(x,-\infty,p,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,p,m)=0.0$
- $f(-0.0,-0.0,p,m)=-0.0$
- $f(0.0,-0.0,p,m)=f(-0.0,0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,-0.0,p,m)=f(-0.0,0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,-x,p,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,-x,p,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::add_prec_ref_val
instead.
If you know that your target precision is the maximum of the precisions of the two inputs,
consider using Float::add_round_ref_val
instead. If both of these things are true,
consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact addition.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_prec_round_val_ref(&Float::from(E), 5, Floor);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_val(Float::from(E), 5, Ceiling);
assert_eq!(sum.to_string(), "6.0");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_prec_round_ref_val(Float::from(E), 5, Nearest);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_val(Float::from(E), 20, Floor);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_val(Float::from(E), 20, Ceiling);
assert_eq!(sum.to_string(), "5.85988");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_prec_round_ref_val(Float::from(E), 20, Nearest);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
sourcepub fn add_prec_round_ref_ref(
&self,
other: &Float,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_prec_round_ref_ref( &self, other: &Float, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Adds two Float
s, rounding the result to the specified precision and with the specified
rounding mode. Both Float
s are taken by reference. An Ordering
is also returned,
indicating whether the rounded sum is less than, equal to, or greater than the exact sum.
Although NaN
s are not comparable to any Float
, whenever this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\infty,-\infty,p,m)=f(-\infty,\infty,p,m)= \text{NaN}$
- $f(\infty,x,p,m)=f(x,\infty,p,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p,m)=f(x,-\infty,p,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,p,m)=0.0$
- $f(-0.0,-0.0,p,m)=-0.0$
- $f(0.0,-0.0,p,m)=f(-0.0,0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,-0.0,p,m)=f(-0.0,0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,-x,p,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,-x,p,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::add_prec_ref_ref
instead.
If you know that your target precision is the maximum of the precisions of the two inputs,
consider using Float::add_round_ref_ref
instead. If both of these things are true,
consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact addition.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 5, Floor);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
assert_eq!(sum.to_string(), "6.0");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 5, Nearest);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 20, Floor);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
assert_eq!(sum.to_string(), "5.85988");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_prec_round_ref_ref(&Float::from(E), 20, Nearest);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
sourcepub fn add_prec_ref_val(&self, other: Float, prec: u64) -> (Float, Ordering)
pub fn add_prec_ref_val(&self, other: Float, prec: u64) -> (Float, Ordering)
Adds two Float
s, rounding the result to the nearest value of the specified precision.
The first Float
is taken by reference and the second by value. An Ordering
is also
returned, indicating whether the rounded sum is less than, equal to, or greater than the
exact sum. Although NaN
s are not comparable to any Float
, whenever this function
returns a NaN
it also returns Equal
.
If the sum is equidistant from two Float
s with the specified precision, the Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a description of
the Nearest
rounding mode.
$$ f(x,y,p) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\infty,-\infty,p)=f(-\infty,\infty,p)=\text{NaN}$
- $f(\infty,x,p)=f(x,\infty,p)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p)=f(x,-\infty,p)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,p)=0.0$
- $f(-0.0,-0.0,p)=-0.0$
- $f(0.0,-0.0,p)=f(-0.0,0.0,p)=0.0$
- $f(x,-x,p)=0.0$ if $x$ is finite and nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::add_prec_round_ref_val
instead. If you know that your target precision is the
maximum of the precisions of the two inputs, consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Examples
use core::f64::consts::{E, PI};
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = (&Float::from(PI)).add_prec_ref_val(Float::from(E), 5);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = (&Float::from(PI)).add_prec_ref_val(Float::from(E), 20);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
sourcepub fn add_prec_ref_ref(&self, other: &Float, prec: u64) -> (Float, Ordering)
pub fn add_prec_ref_ref(&self, other: &Float, prec: u64) -> (Float, Ordering)
Adds two Float
s, rounding the result to the nearest value of the specified precision.
Both Float
s are taken by reference. An Ordering
is also returned, indicating whether
the rounded sum is less than, equal to, or greater than the exact sum. Although NaN
s are
not comparable to any Float
, whenever this function returns a NaN
it also returns
Equal
.
If the sum is equidistant from two Float
s with the specified precision, the Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a description of
the Nearest
rounding mode.
$$ f(x,y,p) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\infty,-\infty,p)=f(-\infty,\infty,p)=\text{NaN}$
- $f(\infty,x,p)=f(x,\infty,p)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p)=f(x,-\infty,p)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,p)=0.0$
- $f(-0.0,-0.0,p)=-0.0$
- $f(0.0,-0.0,p)=f(-0.0,0.0,p)=0.0$
- $f(x,-x,p)=0.0$ if $x$ is finite and nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::add_prec_round_ref_ref
instead. If you know that your target precision is the
maximum of the precisions of the two inputs, consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Examples
use core::f64::consts::{E, PI};
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = (&Float::from(PI)).add_prec_ref_ref(&Float::from(E), 5);
assert_eq!(sum.to_string(), "5.8");
assert_eq!(o, Less);
let (sum, o) = (&Float::from(PI)).add_prec_ref_ref(&Float::from(E), 20);
assert_eq!(sum.to_string(), "5.85987");
assert_eq!(o, Less);
sourcepub fn add_round_ref_val(
&self,
other: Float,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_round_ref_val( &self, other: Float, rm: RoundingMode ) -> (Float, Ordering)
Adds two Float
s, rounding the result with the specified rounding mode. The first
Float
is taken by reference and the second by value. An Ordering
is also returned,
indicating whether the rounded sum is less than, equal to, or greater than the exact sum.
Although NaN
s are not comparable to any Float
, whenever this function returns a NaN
it also returns Equal
.
The precision of the output is the maximum of the precision of the inputs. See
RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
If the output has a precision, it is the maximum of the precisions of the inputs.
Special cases:
- $f(\text{NaN},x,m)=f(x,\text{NaN},m)=f(\infty,-\infty,m)=f(-\infty,\infty,m)= \text{NaN}$
- $f(\infty,x,m)=f(x,\infty,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=f(x,-\infty,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,m)=0.0$
- $f(-0.0,-0.0,m)=-0.0$
- $f(0.0,-0.0,m)=f(-0.0,0.0,m)=0.0$ if $m$ is not
Floor
- $f(0.0,-0.0,m)=f(-0.0,0.0,m)=-0.0$ if $m$ is
Floor
- $f(0.0,x,m)=f(x,0.0,m)=f(-0.0,x,m)=f(x,-0.0,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,-x,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,-x,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to specify an output precision, consider using Float::add_prec_round_ref_val
instead. If you know you’ll be using the Nearest
rounding mode, consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(m)$
where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits())
, and $m$ is self.significant_bits()
.
§Panics
Panics if rm
is Exact
but the maximum precision of the inputs is not high enough to
represent the output.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = (&Float::from(PI)).add_round_ref_val(Float::from(E), Floor);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
let (sum, o) = (&Float::from(PI)).add_round_ref_val(Float::from(E), Ceiling);
assert_eq!(sum.to_string(), "5.859874482048839");
assert_eq!(o, Greater);
let (sum, o) = (&Float::from(PI)).add_round_ref_val(Float::from(E), Nearest);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
sourcepub fn add_round_ref_ref(
&self,
other: &Float,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_round_ref_ref( &self, other: &Float, rm: RoundingMode ) -> (Float, Ordering)
Adds two Float
s, rounding the result with the specified rounding mode. Both Float
s
are taken by reference. An Ordering
is also returned, indicating whether the rounded sum
is less than, equal to, or greater than the exact sum. Although NaN
s are not comparable to
any Float
, whenever this function returns a NaN
it also returns Equal
.
The precision of the output is the maximum of the precision of the inputs. See
RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
If the output has a precision, it is the maximum of the precisions of the inputs.
Special cases:
- $f(\text{NaN},x,m)=f(x,\text{NaN},m)=f(\infty,-\infty,m)=f(-\infty,\infty,m)= \text{NaN}$
- $f(\infty,x,m)=f(x,\infty,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=f(x,-\infty,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0.0,m)=0.0$
- $f(-0.0,-0.0,m)=-0.0$
- $f(0.0,-0.0,m)=f(-0.0,0.0,m)=0.0$ if $m$ is not
Floor
- $f(0.0,-0.0,m)=f(-0.0,0.0,m)=-0.0$ if $m$ is
Floor
- $f(0.0,x,m)=f(x,0.0,m)=f(-0.0,x,m)=f(x,-0.0,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,-x,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,-x,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to specify an output precision, consider using Float::add_prec_round_ref_ref
instead. If you know you’ll be using the Nearest
rounding mode, consider using +
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the maximum precision of the inputs is not high enough to
represent the output.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_round_ref_ref(&Float::from(E), Floor);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_round_ref_ref(&Float::from(E), Ceiling);
assert_eq!(sum.to_string(), "5.859874482048839");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_round_ref_ref(&Float::from(E), Nearest);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
sourcepub fn add_rational_prec_round_ref_val(
&self,
other: Rational,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_rational_prec_round_ref_val( &self, other: Rational, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result to the specified precision and with
the specified rounding mode. The Float
is taken by reference and the Rational
by
value. An Ordering
is also returned, indicating whether the rounded sum is less than,
equal to, or greater than the exact sum. Although NaN
s are not comparable to any
Float
, whenever this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=\text{NaN}$
- $f(\infty,x,p,m)=\infty$
- $f(-\infty,x,p,m)=-\infty$
- $f(0.0,0,p,m)=0.0$
- $f(-0.0,0,p,m)=-0.0$
- $f(x,-x,p,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,-x,p,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::add_rational_prec_ref_val
instead. If you know that your target precision is the precision of the Float
input,
consider using Float::add_rational_round_ref_val
instead. If both of these things are
true, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact addition.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Floor,
);
assert_eq!(sum.to_string(), "3.4");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Ceiling,
);
assert_eq!(sum.to_string(), "3.5");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Nearest,
);
assert_eq!(sum.to_string(), "3.5");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Floor,
);
assert_eq!(sum.to_string(), "3.474922");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Ceiling,
);
assert_eq!(sum.to_string(), "3.474926");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Nearest,
);
assert_eq!(sum.to_string(), "3.474926");
assert_eq!(o, Greater);
sourcepub fn add_rational_prec_round_ref_ref(
&self,
other: &Rational,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_rational_prec_round_ref_ref( &self, other: &Rational, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result to the specified precision and with
the specified rounding mode. The Float
and the Rational
are both taken by reference.
An Ordering
is also returned, indicating whether the rounded sum is less than, equal to,
or greater than the exact sum. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=\text{NaN}$
- $f(\infty,x,p,m)=\infty$
- $f(-\infty,x,p,m)=-\infty$
- $f(0.0,0,p,m)=0.0$
- $f(-0.0,0,p,m)=-0.0$
- $f(x,-x,p,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,-x,p,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::add_rational_prec_ref_ref
instead. If you know that your target precision is the precision of the Float
input,
consider using Float::add_rational_round_ref_ref
instead. If both of these things are
true, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact addition.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Floor,
);
assert_eq!(sum.to_string(), "3.4");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Ceiling,
);
assert_eq!(sum.to_string(), "3.5");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Nearest,
);
assert_eq!(sum.to_string(), "3.5");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Floor,
);
assert_eq!(sum.to_string(), "3.474922");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Ceiling,
);
assert_eq!(sum.to_string(), "3.474926");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Nearest,
);
assert_eq!(sum.to_string(), "3.474926");
assert_eq!(o, Greater);
sourcepub fn add_rational_prec_ref_val(
&self,
other: Rational,
prec: u64
) -> (Float, Ordering)
pub fn add_rational_prec_ref_val( &self, other: Rational, prec: u64 ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result to the nearest value of the
specified precision. The Float
is taken by reference and the Rational
by value. An
Ordering
is also returned, indicating whether the rounded sum is less than, equal to, or
greater than the exact sum. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
If the sum is equidistant from two Float
s with the specified precision, the Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a description of
the Nearest
rounding mode.
$$ f(x,y,p) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$
- $f(-\infty,x,p)=-\infty$
- $f(0.0,0,p)=0.0$
- $f(-0.0,0,p)=-0.0$
- $f(x,-x,p)=0.0$ if $x$ is nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::add_rational_prec_round_ref_val
instead. If you know that your target precision
is the precision of the Float
input, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Examples
use core::f64::consts::PI;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_rational_prec_ref_val(Rational::exact_from(1.5), 5);
assert_eq!(sum.to_string(), "4.8");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_ref_val(Rational::exact_from(1.5), 20);
assert_eq!(sum.to_string(), "4.641594");
assert_eq!(o, Greater);
sourcepub fn add_rational_prec_ref_ref(
&self,
other: &Rational,
prec: u64
) -> (Float, Ordering)
pub fn add_rational_prec_ref_ref( &self, other: &Rational, prec: u64 ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result to the nearest value of the
specified precision. The Float
and the Rational
are both are taken by reference. An
Ordering
is also returned, indicating whether the rounded sum is less than, equal to, or
greater than the exact sum. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
If the sum is equidistant from two Float
s with the specified precision, the Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a description of
the Nearest
rounding mode.
$$ f(x,y,p) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$
- $f(-\infty,x,p)=-\infty$
- $f(0.0,0,p)=0.0$
- $f(-0.0,0,p)=-0.0$
- $f(x,-x,p)=0.0$ if $x$ is nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::add_rational_prec_round_ref_ref
instead. If you know that your target precision
is the precision of the Float
input, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Examples
use core::f64::consts::PI;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).add_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
assert_eq!(sum.to_string(), "4.8");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).add_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
assert_eq!(sum.to_string(), "4.641594");
assert_eq!(o, Greater);
sourcepub fn add_rational_round_ref_val(
&self,
other: Rational,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_rational_round_ref_val( &self, other: Rational, rm: RoundingMode ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result with the specified rounding mode.
The Float
is taken by reference and the Float
by value. An Ordering
is also
returned, indicating whether the rounded sum is less than, equal to, or greater than the
exact sum. Although NaN
s are not comparable to any Float
, whenever this function
returns a NaN
it also returns Equal
.
The precision of the output is the precision of the Float
input. See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$, where $p$ is the precision of the inputFloat
. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$, where $p$ is the precision of the inputFloat
.
If the output has a precision, it is the precision of the Float
input.
Special cases:
- $f(\text{NaN},x,m)=\text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0,m)=0.0$
- $f(-0.0,0,m)=-0.0$
- $f(0.0,x,m)=f(x,0,m)=f(-0.0,x,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,-x,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,-x,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you want to specify an output precision, consider using
Float::add_rational_prec_round_ref_val
instead. If you know you’ll be using the
Nearest
rounding mode, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the precision of the Float
input is not high enough to
represent the output.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) =
Float::from(PI).add_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
assert_eq!(sum.to_string(), "3.4749259869231262");
assert_eq!(o, Less);
let (sum, o) =
Float::from(PI).add_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
assert_eq!(sum.to_string(), "3.4749259869231266");
assert_eq!(o, Greater);
let (sum, o) =
Float::from(PI).add_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
assert_eq!(sum.to_string(), "3.4749259869231266");
assert_eq!(o, Greater);
sourcepub fn add_rational_round_ref_ref(
&self,
other: &Rational,
rm: RoundingMode
) -> (Float, Ordering)
pub fn add_rational_round_ref_ref( &self, other: &Rational, rm: RoundingMode ) -> (Float, Ordering)
Adds a Float
and a Rational
, rounding the result with the specified rounding mode.
The Float
and the Rational
are both are taken by reference. An Ordering
is also
returned, indicating whether the rounded sum is less than, equal to, or greater than the
exact sum. Although NaN
s are not comparable to any Float
, whenever this function
returns a NaN
it also returns Equal
.
The precision of the output is the precision of the Float
input. See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x+y+\epsilon. $$
- If $x+y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x+y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p+1}$, where $p$ is the precision of the inputFloat
. - If $x+y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x+y|\rfloor-p}$, where $p$ is the precision of the inputFloat
.
If the output has a precision, it is the precision of the Float
input.
Special cases:
- $f(\text{NaN},x,m)=\text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0,m)=0.0$
- $f(-0.0,0,m)=-0.0$
- $f(0.0,x,m)=f(x,0,m)=f(-0.0,x,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,-x,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,-x,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you want to specify an output precision, consider using
Float::add_rational_prec_round_ref_ref
instead. If you know you’ll be using the
Nearest
rounding mode, consider using +
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the precision of the Float
input is not high enough to
represent the output.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) =
Float::from(PI).add_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
assert_eq!(sum.to_string(), "3.4749259869231262");
assert_eq!(o, Less);
let (sum, o) =
Float::from(PI).add_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
assert_eq!(sum.to_string(), "3.4749259869231266");
assert_eq!(o, Greater);
let (sum, o) =
Float::from(PI).add_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
assert_eq!(sum.to_string(), "3.4749259869231266");
assert_eq!(o, Greater);
sourcepub fn sub_prec_round_ref_val(
&self,
other: Float,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_prec_round_ref_val( &self, other: Float, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Subtracts two Float
s, rounding the result to the specified precision and with the
specified rounding mode. The first Float
is taken by reference and the second by value.
An Ordering
is also returned, indicating whether the rounded difference is less than,
equal to, or greater than the exact difference. Although NaN
s are not comparable to any
Float
, whenever this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\infty,\infty,p,m)=f(-\infty,-\infty,p,m)= \text{NaN}$
- $f(\infty,x,p,m)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,p,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p,m)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,p,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,p,m)=0.0$
- $f(-0.0,0.0,p,m)=-0.0$
- $f(0.0,0.0,p,m)=f(-0.0,-0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,p,m)=f(-0.0,-0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,x,p,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,p,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::sub_prec_ref_val
instead.
If you know that your target precision is the maximum of the precisions of the two inputs,
consider using Float::sub_round_ref_val
instead. If both of these things are true,
consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact subtraction.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 5, Floor);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 5, Ceiling);
assert_eq!(sum.to_string(), "0.44");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 5, Nearest);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 20, Floor);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 20, Ceiling);
assert_eq!(sum.to_string(), "0.4233112");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_prec_round_ref_val(Float::from(E), 20, Nearest);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
sourcepub fn sub_prec_round_ref_ref(
&self,
other: &Float,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_prec_round_ref_ref( &self, other: &Float, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Subtracts two Float
s, rounding the result to the specified precision and with the
specified rounding mode. Both Float
s are taken by reference. An Ordering
is also
returned, indicating whether the rounded difference is less than, equal to, or greater than
the exact difference. Although NaN
s are not comparable to any Float
, whenever this
function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\infty,\infty,p,m)=f(-\infty,-\infty,p,m)= \text{NaN}$
- $f(\infty,x,p,m)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,p,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p,m)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,p,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,p,m)=0.0$
- $f(-0.0,0.0,p,m)=-0.0$
- $f(0.0,0.0,p,m)=f(-0.0,-0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,p,m)=f(-0.0,-0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,x,p,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,p,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::sub_prec_ref_ref
instead.
If you know that your target precision is the maximum of the precisions of the two inputs,
consider using Float::sub_round_ref_ref
instead. If both of these things are true,
consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact subtraction.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 5, Floor);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
assert_eq!(sum.to_string(), "0.44");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 5, Nearest);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 20, Floor);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
assert_eq!(sum.to_string(), "0.4233112");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_prec_round_ref_ref(&Float::from(E), 20, Nearest);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
sourcepub fn sub_prec_ref_val(&self, other: Float, prec: u64) -> (Float, Ordering)
pub fn sub_prec_ref_val(&self, other: Float, prec: u64) -> (Float, Ordering)
Subtracts two Float
s, rounding the result to the nearest value of the specified
precision. The first Float
is taken by reference and the second by value. An
Ordering
is also returned, indicating whether the rounded difference is less than, equal
to, or greater than the exact difference. Although NaN
s are not comparable to any
Float
, whenever this function returns a NaN
it also returns Equal
.
If the difference is equidistant from two Float
s with the specified precision, the
Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a
description of the Nearest
rounding mode.
$$ f(x,y,p) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\infty,\infty,p)=f(-\infty,-\infty,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,p)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,p)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,p)=0.0$
- $f(-0.0,0.0,p)=-0.0$
- $f(0.0,0.0,p)=f(-0.0,-0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,p)=f(-0.0,-0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,x,p)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,p)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to use a rounding mode other than Nearest
, consider using
Float::sub_prec_round_ref_val
instead. If you know that your target precision is the
maximum of the precisions of the two inputs, consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Examples
use core::f64::consts::{E, PI};
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_prec_ref_val(Float::from(E), 5);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_ref_val(Float::from(E), 20);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
sourcepub fn sub_prec_ref_ref(&self, other: &Float, prec: u64) -> (Float, Ordering)
pub fn sub_prec_ref_ref(&self, other: &Float, prec: u64) -> (Float, Ordering)
Subtracts two Float
s, rounding the result to the nearest value of the specified
precision. Both Float
s are taken by reference. An Ordering
is also returned,
indicating whether the rounded difference is less than, equal to, or greater than the exact
difference. Although NaN
s are not comparable to any Float
, whenever this function
returns a NaN
it also returns Equal
.
If the difference is equidistant from two Float
s with the specified precision, the
Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a
description of the Nearest
rounding mode.
$$ f(x,y,p) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\infty,\infty,p)=f(-\infty,-\infty,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,p)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,p)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,p)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,p)=0.0$
- $f(-0.0,0.0,p)=-0.0$
- $f(0.0,0.0,p)=f(-0.0,-0.0,p,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,p)=f(-0.0,-0.0,p,m)=-0.0$ if $m$ is
Floor
- $f(x,x,p)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,p)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to use a rounding mode other than Nearest
, consider using
Float::sub_prec_round_ref_ref
instead. If you know that your target precision is the
maximum of the precisions of the two inputs, consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is prec
.
§Examples
use core::f64::consts::{E, PI};
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_prec_ref_ref(&Float::from(E), 5);
assert_eq!(sum.to_string(), "0.42");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_prec_ref_ref(&Float::from(E), 20);
assert_eq!(sum.to_string(), "0.4233108");
assert_eq!(o, Less);
sourcepub fn sub_round_ref_val(
&self,
other: Float,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_round_ref_val( &self, other: Float, rm: RoundingMode ) -> (Float, Ordering)
Subtracts two Float
s, rounding the result with the specified rounding mode. The
Float
is taken by reference and the Rational
by value. An Ordering
is also
returned, indicating whether the rounded difference is less than, equal to, or greater than
the exact difference. Although NaN
s are not comparable to any Float
, whenever this
function returns a NaN
it also returns Equal
.
The precision of the output is the maximum of the precision of the inputs. See
RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
If the output has a precision, it is the maximum of the precisions of the inputs.
Special cases:
- $f(\text{NaN},x,m)=f(x,\text{NaN},m)=f(\infty,\infty,m)=f(-\infty,-\infty,m)= \text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,m)=0.0$
- $f(-0.0,0.0,m)=-0.0$
- $f(0.0,0.0,m)=f(-0.0,-0.0,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,m)=f(-0.0,-0.0,m)=-0.0$ if $m$ is
Floor
- $f(x,x,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to specify an output precision, consider using Float::sub_prec_round_ref_val
instead. If you know you’ll be using the Nearest
rounding mode, consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(m)$
where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits())
, and $m$ is self.significant_bits()
.
§Panics
Panics if rm
is Exact
but the maximum precision of the inputs is not high enough to
represent the output.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_round_ref_val(Float::from(-E), Floor);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_round_ref_val(Float::from(-E), Ceiling);
assert_eq!(sum.to_string(), "5.859874482048839");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_round_ref_val(Float::from(-E), Nearest);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
sourcepub fn sub_round_ref_ref(
&self,
other: &Float,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_round_ref_ref( &self, other: &Float, rm: RoundingMode ) -> (Float, Ordering)
Subtracts two Float
s, rounding the result with the specified rounding mode. Both
Float
s are taken by reference. An Ordering
is also returned, indicating whether the
rounded difference is less than, equal to, or greater than the exact difference. Although
NaN
s are not comparable to any Float
, whenever this function returns a NaN
it also
returns Equal
.
The precision of the output is the maximum of the precision of the inputs. See
RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
If the output has a precision, it is the maximum of the precisions of the inputs.
Special cases:
- $f(\text{NaN},x,m)=f(x,\text{NaN},m)=f(\infty,\infty,m)=f(-\infty,-\infty,m)= \text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $\infty$
- $f(x,-\infty,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $-\infty$
- $f(x,\infty,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,-0.0,m)=0.0$
- $f(-0.0,0.0,m)=-0.0$
- $f(0.0,0.0,m)=f(-0.0,-0.0,m)=0.0$ if $m$ is not
Floor
- $f(0.0,0.0,m)=f(-0.0,-0.0,m)=-0.0$ if $m$ is
Floor
- $f(x,x,m)=0.0$ if $x$ is finite and nonzero and $m$ is not
Floor
- $f(x,x,m)=-0.0$ if $x$ is finite and nonzero and $m$ is
Floor
If you want to specify an output precision, consider using Float::sub_prec_round_ref_ref
instead. If you know you’ll be using the Nearest
rounding mode, consider using -
instead.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the maximum precision of the inputs is not high enough to
represent the output.
§Examples
use core::f64::consts::{E, PI};
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_round_ref_ref(&Float::from(-E), Floor);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_round_ref_ref(&Float::from(-E), Ceiling);
assert_eq!(sum.to_string(), "5.859874482048839");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_round_ref_ref(&Float::from(-E), Nearest);
assert_eq!(sum.to_string(), "5.859874482048838");
assert_eq!(o, Less);
sourcepub fn sub_rational_prec_round_ref_val(
&self,
other: Rational,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_rational_prec_round_ref_val( &self, other: Rational, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result to the specified precision and
with the specified rounding mode. The Float
is taken by reference and the Rational
by value. An Ordering
is also returned, indicating whether the rounded difference is
less than, equal to, or greater than the exact difference. Although NaN
s are not
comparable to any Float
, whenever this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=\text{NaN}$
- $f(\infty,x,p,m)=\infty$
- $f(-\infty,x,p,m)=-\infty$
- $f(0.0,0,p,m)=0.0$
- $f(-0.0,0,p,m)=-0.0$
- $f(x,x,p,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,x,p,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::sub_rational_prec_ref_val
instead. If you know that your target precision is the precision of the Float
input,
consider using Float::sub_rational_round_ref_val
instead. If both of these things are
true, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact subtraction.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Floor,
);
assert_eq!(sum.to_string(), "2.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Ceiling,
);
assert_eq!(sum.to_string(), "2.9");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
5,
Nearest,
);
assert_eq!(sum.to_string(), "2.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Floor,
);
assert_eq!(sum.to_string(), "2.808258");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Ceiling,
);
assert_eq!(sum.to_string(), "2.808262");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_val(
Rational::from_unsigneds(1u8, 3),
20,
Nearest,
);
assert_eq!(sum.to_string(), "2.808258");
assert_eq!(o, Less);
sourcepub fn sub_rational_prec_round_ref_ref(
&self,
other: &Rational,
prec: u64,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_rational_prec_round_ref_ref( &self, other: &Rational, prec: u64, rm: RoundingMode ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result to the specified precision and
with the specified rounding mode. The Float
and the Rational
are both taken by
reference. An Ordering
is also returned, indicating whether the rounded difference is
less than, equal to, or greater than the exact difference. Although NaN
s are not
comparable to any Float
, whenever this function returns a NaN
it also returns Equal
.
See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,p,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p,m)=\text{NaN}$
- $f(\infty,x,p,m)=\infty$
- $f(-\infty,x,p,m)=-\infty$
- $f(0.0,0,p,m)=0.0$
- $f(-0.0,0,p,m)=-0.0$
- $f(x,x,p,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,x,p,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you know you’ll be using Nearest
, consider using Float::sub_rational_prec_ref_ref
instead. If you know that your target precision is the precision of the Float
input,
consider using Float::sub_rational_round_ref_ref
instead. If both of these things are
true, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Panics
Panics if rm
is Exact
but prec
is too small for an exact subtraction.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Floor,
);
assert_eq!(sum.to_string(), "2.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Ceiling,
);
assert_eq!(sum.to_string(), "2.9");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
5,
Nearest,
);
assert_eq!(sum.to_string(), "2.8");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Floor,
);
assert_eq!(sum.to_string(), "2.808258");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Ceiling,
);
assert_eq!(sum.to_string(), "2.808262");
assert_eq!(o, Greater);
let (sum, o) = Float::from(PI).sub_rational_prec_round_ref_ref(
&Rational::from_unsigneds(1u8, 3),
20,
Nearest,
);
assert_eq!(sum.to_string(), "2.808258");
assert_eq!(o, Less);
sourcepub fn sub_rational_prec_ref_val(
&self,
other: Rational,
prec: u64
) -> (Float, Ordering)
pub fn sub_rational_prec_ref_val( &self, other: Rational, prec: u64 ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result to the nearest value of the
specified precision. The Float
is taken by reference and the Rational
by value. An
Ordering
is also returned, indicating whether the rounded difference is less than, equal
to, or greater than the exact difference. Although NaN
s are not comparable to any
Float
, whenever this function returns a NaN
it also returns Equal
.
If the difference is equidistant from two Float
s with the specified precision, the
Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a
description of the Nearest
rounding mode.
$$ f(x,y,p) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$
- $f(-\infty,x,p)=-\infty$
- $f(0.0,0,p)=0.0$
- $f(-0.0,0,p)=-0.0$
- $f(x,x,p)=0.0$ if $x$ is nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::sub_rational_prec_round_ref_val
instead. If you know that your target precision
is the precision of the Float
input, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Examples
use core::f64::consts::PI;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_rational_prec_ref_val(Rational::exact_from(1.5), 5);
assert_eq!(sum.to_string(), "1.62");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_ref_val(Rational::exact_from(1.5), 20);
assert_eq!(sum.to_string(), "1.641592");
assert_eq!(o, Less);
sourcepub fn sub_rational_prec_ref_ref(
&self,
other: &Rational,
prec: u64
) -> (Float, Ordering)
pub fn sub_rational_prec_ref_ref( &self, other: &Rational, prec: u64 ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result to the nearest value of the
specified precision. The Float
and the Rational
are both are taken by reference. An
Ordering
is also returned, indicating whether the rounded difference is less than, equal
to, or greater than the exact difference. Although NaN
s are not comparable to any
Float
, whenever this function returns a NaN
it also returns Equal
.
If the difference is equidistant from two Float
s with the specified precision, the
Float
with fewer 1s in its binary expansion is chosen. See RoundingMode
for a
description of the Nearest
rounding mode.
$$ f(x,y,p) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$.
If the output has a precision, it is prec
.
Special cases:
- $f(\text{NaN},x,p)=\text{NaN}$
- $f(\infty,x,p)=\infty$
- $f(-\infty,x,p)=-\infty$
- $f(0.0,0,p)=0.0$
- $f(-0.0,0,p)=-0.0$
- $f(x,x,p)=0.0$ if $x$ is nonzero
If you want to use a rounding mode other than Nearest
, consider using
Float::sub_rational_prec_round_ref_ref
instead. If you know that your target precision
is the precision of the Float
input, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(other.significant_bits(), prec)
.
§Examples
use core::f64::consts::PI;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) = Float::from(PI).sub_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
assert_eq!(sum.to_string(), "1.62");
assert_eq!(o, Less);
let (sum, o) = Float::from(PI).sub_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
assert_eq!(sum.to_string(), "1.641592");
assert_eq!(o, Less);
sourcepub fn sub_rational_round_ref_val(
&self,
other: Rational,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_rational_round_ref_val( &self, other: Rational, rm: RoundingMode ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result with the specified rounding
mode. The Float
is taken by reference and the Rational
by value. An Ordering
is
also returned, indicating whether the rounded difference is less than, equal to, or greater
than the exact difference. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
The precision of the output is the precision of the Float
input. See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$, where $p$ is the precision of the inputFloat
. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$, where $p$ is the precision of the inputFloat
.
If the output has a precision, it is the precision of the Float
input.
Special cases:
- $f(\text{NaN},x,m)=\text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0,m)=0.0$
- $f(-0.0,0,m)=-0.0$
- $f(x,0,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(0.0,x,m)=f(-0.0,x,m)=-x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,x,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,x,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you want to specify an output precision, consider using
Float::sub_rational_prec_round_ref_val
instead. If you know you’ll be using the
Nearest
rounding mode, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the precision of the Float
input is not high enough to
represent the output.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) =
Float::from(PI).sub_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
assert_eq!(sum.to_string(), "2.8082593202564596");
assert_eq!(o, Less);
let (sum, o) =
Float::from(PI).sub_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
assert_eq!(sum.to_string(), "2.8082593202564601");
assert_eq!(o, Greater);
let (sum, o) =
Float::from(PI).sub_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
assert_eq!(sum.to_string(), "2.8082593202564596");
assert_eq!(o, Less);
sourcepub fn sub_rational_round_ref_ref(
&self,
other: &Rational,
rm: RoundingMode
) -> (Float, Ordering)
pub fn sub_rational_round_ref_ref( &self, other: &Rational, rm: RoundingMode ) -> (Float, Ordering)
Subtracts a Float
by a Rational
, rounding the result with the specified rounding
mode. The Float
and the Rational
are both are taken by reference. An Ordering
is
also returned, indicating whether the rounded difference is less than, equal to, or greater
than the exact difference. Although NaN
s are not comparable to any Float
, whenever
this function returns a NaN
it also returns Equal
.
The precision of the output is the precision of the Float
input. See RoundingMode
for a description of the possible rounding modes.
$$ f(x,y,m) = x-y+\epsilon. $$
- If $x-y$ is infinite, zero, or
NaN
, $\epsilon$ may be ignored or assumed to be 0. - If $x-y$ is finite and nonzero, and $m$ is not
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p+1}$, where $p$ is the precision of the inputFloat
. - If $x-y$ is finite and nonzero, and $m$ is
Nearest
, then $|\epsilon| < 2^{\lfloor\log_2 |x-y|\rfloor-p}$, where $p$ is the precision of the inputFloat
.
If the output has a precision, it is the precision of the Float
input.
Special cases:
- $f(\text{NaN},x,m)=\text{NaN}$
- $f(\infty,x,m)=\infty$ if $x$ is not NaN or $-\infty$
- $f(-\infty,x,m)=-\infty$ if $x$ is not NaN or $\infty$
- $f(0.0,0,m)=0.0$
- $f(-0.0,0,m)=-0.0$
- $f(x,0,m)=x$ if $x$ is not NaN and $x$ is nonzero
- $f(0.0,x,m)=f(-0.0,x,m)=-x$ if $x$ is not NaN and $x$ is nonzero
- $f(x,x,m)=0.0$ if $x$ is nonzero and $m$ is not
Floor
- $f(x,x,m)=-0.0$ if $x$ is nonzero and $m$ is
Floor
If you want to specify an output precision, consider using
Float::sub_rational_prec_round_ref_ref
instead. If you know you’ll be using the
Nearest
rounding mode, consider using -
instead.
§Worst-case complexity
$T(n) = O(n \log n \log\log n)$
$M(n) = O(n \log n)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Panics
Panics if rm
is Exact
but the precision of the Float
input is not high enough to
represent the output.
§Examples
use core::f64::consts::PI;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;
let (sum, o) =
Float::from(PI).sub_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
assert_eq!(sum.to_string(), "2.8082593202564596");
assert_eq!(o, Less);
let (sum, o) =
Float::from(PI).sub_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
assert_eq!(sum.to_string(), "2.8082593202564601");
assert_eq!(o, Greater);
let (sum, o) =
Float::from(PI).sub_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
assert_eq!(sum.to_string(), "2.8082593202564596");
assert_eq!(o, Less);
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Determines whether a Float
is finite.
NaN is not finite.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, One};
use malachite_float::Float;
assert_eq!(Float::NAN.is_finite(), false);
assert_eq!(Float::INFINITY.is_finite(), false);
assert_eq!(Float::ONE.is_finite(), true);
sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Determines whether a Float
is infinite.
NaN is not infinite.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, One};
use malachite_float::Float;
assert_eq!(Float::NAN.is_infinite(), false);
assert_eq!(Float::INFINITY.is_infinite(), true);
assert_eq!(Float::ONE.is_infinite(), false);
sourcepub fn is_positive_zero(&self) -> bool
pub fn is_positive_zero(&self) -> bool
Determines whether a Float
is positive zero.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.is_positive_zero(), false);
assert_eq!(Float::INFINITY.is_positive_zero(), false);
assert_eq!(Float::ONE.is_positive_zero(), false);
assert_eq!(Float::ZERO.is_positive_zero(), true);
assert_eq!(Float::NEGATIVE_ZERO.is_positive_zero(), false);
sourcepub fn is_negative_zero(&self) -> bool
pub fn is_negative_zero(&self) -> bool
Determines whether a Float
is negative zero.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.is_negative_zero(), false);
assert_eq!(Float::INFINITY.is_negative_zero(), false);
assert_eq!(Float::ONE.is_negative_zero(), false);
assert_eq!(Float::ZERO.is_negative_zero(), false);
assert_eq!(Float::NEGATIVE_ZERO.is_negative_zero(), true);
sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Determines whether a Float
is zero (positive or negative).
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.is_zero(), false);
assert_eq!(Float::INFINITY.is_zero(), false);
assert_eq!(Float::ONE.is_zero(), false);
assert_eq!(Float::ZERO.is_zero(), true);
assert_eq!(Float::NEGATIVE_ZERO.is_zero(), true);
sourcepub fn is_normal(&self) -> bool
pub fn is_normal(&self) -> bool
Determines whether a Float
is normal, that is, finite and nonzero.
There is no notion of subnormal Float
s.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.is_normal(), false);
assert_eq!(Float::INFINITY.is_normal(), false);
assert_eq!(Float::ZERO.is_normal(), false);
assert_eq!(Float::NEGATIVE_ZERO.is_normal(), false);
assert_eq!(Float::ONE.is_normal(), true);
sourcepub fn is_sign_positive(&self) -> bool
pub fn is_sign_positive(&self) -> bool
Determines whether a Float
’s sign is positive.
A NaN has no sign, so this function returns false when given a NaN.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
};
use malachite_float::Float;
assert_eq!(Float::NAN.is_sign_positive(), false);
assert_eq!(Float::INFINITY.is_sign_positive(), true);
assert_eq!(Float::NEGATIVE_INFINITY.is_sign_positive(), false);
assert_eq!(Float::ZERO.is_sign_positive(), true);
assert_eq!(Float::NEGATIVE_ZERO.is_sign_positive(), false);
assert_eq!(Float::ONE.is_sign_positive(), true);
assert_eq!(Float::NEGATIVE_ONE.is_sign_positive(), false);
sourcepub fn is_sign_negative(&self) -> bool
pub fn is_sign_negative(&self) -> bool
Determines whether a Float
’s sign is negative.
A NaN has no sign, so this function returns false when given a NaN.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
};
use malachite_float::Float;
assert_eq!(Float::NAN.is_sign_negative(), false);
assert_eq!(Float::INFINITY.is_sign_negative(), false);
assert_eq!(Float::NEGATIVE_INFINITY.is_sign_negative(), true);
assert_eq!(Float::ZERO.is_sign_negative(), false);
assert_eq!(Float::NEGATIVE_ZERO.is_sign_negative(), true);
assert_eq!(Float::ONE.is_sign_negative(), false);
assert_eq!(Float::NEGATIVE_ONE.is_sign_negative(), true);
sourcepub fn classify(&self) -> FpCategory
pub fn classify(&self) -> FpCategory
Classifies a Float
into one of several categories.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
};
use malachite_float::Float;
use std::num::FpCategory;
assert_eq!(Float::NAN.classify(), FpCategory::Nan);
assert_eq!(Float::INFINITY.classify(), FpCategory::Infinite);
assert_eq!(Float::NEGATIVE_INFINITY.classify(), FpCategory::Infinite);
assert_eq!(Float::ZERO.classify(), FpCategory::Zero);
assert_eq!(Float::NEGATIVE_ZERO.classify(), FpCategory::Zero);
assert_eq!(Float::ONE.classify(), FpCategory::Normal);
assert_eq!(Float::NEGATIVE_ONE.classify(), FpCategory::Normal);
sourcepub fn to_non_nan(&self) -> Option<Float>
pub fn to_non_nan(&self) -> Option<Float>
Turns a NaN into a None
and wraps any non-NaN Float
with a Some
. The Float
is
taken by reference.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.to_non_nan(), None);
assert_eq!(Float::INFINITY.to_non_nan(), Some(Float::INFINITY));
assert_eq!(Float::ZERO.to_non_nan(), Some(Float::ZERO));
assert_eq!(
Float::NEGATIVE_ZERO.to_non_nan(),
Some(Float::NEGATIVE_ZERO)
);
assert_eq!(Float::ONE.to_non_nan(), Some(Float::ONE));
sourcepub fn to_finite(&self) -> Option<Float>
pub fn to_finite(&self) -> Option<Float>
Turns any Float
that’s NaN or infinite into a None
and wraps any finite Float
with
a Some
. The Float
is taken by reference.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeZero, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.to_finite(), None);
assert_eq!(Float::INFINITY.to_finite(), None);
assert_eq!(Float::ZERO.to_finite(), Some(Float::ZERO));
assert_eq!(Float::NEGATIVE_ZERO.to_finite(), Some(Float::NEGATIVE_ZERO));
assert_eq!(Float::ONE.to_finite(), Some(Float::ONE));
sourcepub fn complexity(&self) -> u64
pub fn complexity(&self) -> u64
Determines a Float
’s complexity. The complexity is defined as follows:
$$ f(\text{NaN}) = f(\pm\infty) = f(\pm 0.0) = 1, $$
and, if $x$ is finite and nonzero,
$$ f(x) = \max(|\lfloor \log_2 x\rfloor|, p), $$
where $p$ is the precision of $x$.
Informally, the complexity is proportional to the number of characters you would need to
write the Float
out without using exponents.
See also the Float
implementation of SignificantBits
.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{NaN, One};
use malachite_float::Float;
assert_eq!(Float::NAN.complexity(), 1);
assert_eq!(Float::ONE.complexity(), 1);
assert_eq!(Float::one_prec(100).complexity(), 100);
assert_eq!(Float::from(std::f64::consts::PI).complexity(), 53);
assert_eq!(Float::power_of_2(100u64).complexity(), 100);
assert_eq!(Float::power_of_2(-100i64).complexity(), 100);
sourcepub fn to_significand(&self) -> Option<Natural>
pub fn to_significand(&self) -> Option<Natural>
Gets the significand of a Float
, taking the Float
by value.
The significand is the smallest positive integer which is some power of 2 times the
Float
, and whose number of significant bits is a multiple of the limb width. If the
Float
is NaN, infinite, or zero, then None
is returned.
§Worst-case complexity
Constant time and additional memory.
§Examples
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_base::num::arithmetic::traits::PowerOf2;
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_base::num::basic::traits::One;
use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
use malachite_float::Float;
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_nz::natural::Natural;
assert_eq!(Float::NAN.to_significand(), None);
assert_eq!(Float::INFINITY.to_significand(), None);
assert_eq!(Float::ZERO.to_significand(), None);
#[cfg(not(feature = "32_bit_limbs"))]
{
assert_eq!(Float::ONE.to_significand(), Some(Natural::power_of_2(63)));
assert_eq!(
Float::from(std::f64::consts::PI).to_significand().unwrap(),
14488038916154245120u64
);
}
sourcepub fn significand_ref(&self) -> Option<&Natural>
pub fn significand_ref(&self) -> Option<&Natural>
Returns a reference to the significand of a Float
.
The significand is the smallest positive integer which is some power of 2 times the
Float
, and whose number of significant bits is a multiple of the limb width. If the
Float
is NaN, infinite, or zero, then None
is returned.
§Worst-case complexity
Constant time and additional memory.
§Examples
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_base::num::arithmetic::traits::PowerOf2;
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_base::num::basic::traits::One;
use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
use malachite_float::Float;
#[cfg(not(feature = "32_bit_limbs"))]
use malachite_nz::natural::Natural;
assert_eq!(Float::NAN.significand_ref(), None);
assert_eq!(Float::INFINITY.significand_ref(), None);
assert_eq!(Float::ZERO.significand_ref(), None);
#[cfg(not(feature = "32_bit_limbs"))]
{
assert_eq!(
*Float::ONE.significand_ref().unwrap(),
Natural::power_of_2(63)
);
assert_eq!(
*Float::from(std::f64::consts::PI).significand_ref().unwrap(),
14488038916154245120u64
);
}
sourcepub fn get_exponent(&self) -> Option<i64>
pub fn get_exponent(&self) -> Option<i64>
Returns a Float
’s exponent.
$$ f(\text{NaN}) = f(\pm\infty) = f(\pm 0.0) = \text{None}, $$
and, if $x$ is finite and nonzero,
$$ f(x) = \operatorname{Some}(\lfloor \log_2 x \rfloor + 1). $$
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{Infinity, NaN, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.get_exponent(), None);
assert_eq!(Float::INFINITY.get_exponent(), None);
assert_eq!(Float::ZERO.get_exponent(), None);
assert_eq!(Float::ONE.get_exponent(), Some(1));
assert_eq!(Float::from(std::f64::consts::PI).get_exponent(), Some(2));
assert_eq!(Float::power_of_2(100u64).get_exponent(), Some(101));
assert_eq!(Float::power_of_2(-100i64).get_exponent(), Some(-99));
sourcepub fn get_prec(&self) -> Option<u64>
pub fn get_prec(&self) -> Option<u64>
Returns a Float
’s precision. The precision is a positive integer denoting how many of
the Float
’s bits are significant.
Only Float
s that are finite and nonzero have a precision. For other Float
s, None
is returned.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.get_prec(), None);
assert_eq!(Float::INFINITY.get_prec(), None);
assert_eq!(Float::ZERO.get_prec(), None);
assert_eq!(Float::ONE.get_prec(), Some(1));
assert_eq!(Float::one_prec(100).get_prec(), Some(100));
assert_eq!(Float::from(std::f64::consts::PI).get_prec(), Some(53));
sourcepub fn get_min_prec(&self) -> Option<u64>
pub fn get_min_prec(&self) -> Option<u64>
Returns the minimum precision necessary to represent the given Float
’s value.
For example, Float:one_prec(100)
has a precision of 100, but its minimum precision is 1,
because that’s all that’s necessary to represent the value 1.
The minimum precision is always less than or equal to the actual precision.
Only Float
s that are finite and nonzero have a minimum precision. For other Float
s,
None
is returned.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::{Infinity, NaN, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.get_min_prec(), None);
assert_eq!(Float::INFINITY.get_min_prec(), None);
assert_eq!(Float::ZERO.get_min_prec(), None);
assert_eq!(Float::ONE.get_min_prec(), Some(1));
assert_eq!(Float::one_prec(100).get_min_prec(), Some(1));
assert_eq!(Float::from(std::f64::consts::PI).get_min_prec(), Some(50));
sourcepub fn ulp(&self) -> Option<Float>
pub fn ulp(&self) -> Option<Float>
Gets a Float
’s ulp (unit in last place, or unit of least precision).
If the Float
is positive, its ulp is the distance to the next-largest Float
with the
same precision; if it is negative, the next-smallest. (This definition works even if the
Float
is the largest in its binade.)
If the Float
is NaN, infinite, or zero, then None
is returned.
$$ f(\text{NaN}) = f(\pm\infty) = f(\pm 0.0) = \text{None}, $$
and, if $x$ is finite and nonzero,
$$ f(x) = \operatorname{Some}(2^{\lfloor \log_2 x \rfloor-p+1}), $$ where $p$ is the precision of $x$.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeOne, One, Zero};
use malachite_float::Float;
assert_eq!(Float::NAN.ulp(), None);
assert_eq!(Float::INFINITY.ulp(), None);
assert_eq!(Float::ZERO.ulp(), None);
let s = Float::ONE.ulp().map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("1.0"));
let s = Float::one_prec(100).ulp().map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("2.0e-30"));
let s = Float::from(std::f64::consts::PI)
.ulp()
.map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("4.0e-16"));
let s = Float::power_of_2(100u64).ulp().map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("1.0e30"));
let s = Float::power_of_2(-100i64).ulp().map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("8.0e-31"));
let s = Float::NEGATIVE_ONE.ulp().map(|x| x.to_string());
assert_eq!(s.as_ref().map(|s| s.as_str()), Some("1.0"));
sourcepub fn sci_mantissa_and_exponent_round<T: PrimitiveFloat>(
&self,
rm: RoundingMode
) -> Option<(T, i64, Ordering)>
pub fn sci_mantissa_and_exponent_round<T: PrimitiveFloat>( &self, rm: RoundingMode ) -> Option<(T, i64, Ordering)>
Returns a Float
’s scientific mantissa and exponent, rounding according to the specified
rounding mode. An Ordering
is also returned, indicating whether the mantissa and
exponent represent a value that is less than, equal to, or greater than the original value.
When $x$ is positive, we can write $x = 2^{e_s}m_s$, where $e_s$ is an integer and $m_s$ is
a rational number with $1 \leq m_s < 2$. We represent the rational mantissa as a float. The
conversion might not be exact, so we round to the nearest float using the provided rounding
mode. If the rounding mode is Exact
but the conversion is not exact, None
is returned.
$$
f(x, r) \approx \left (\frac{x}{2^{\lfloor \log_2 x \rfloor}},
\lfloor \log_2 x \rfloor\right ).
$$
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_float::Float;
use malachite_nz::natural::Natural;
use std::cmp::Ordering::{self, *};
let test = |x: Float, rm: RoundingMode, out: Option<(f32, i64, Ordering)>| {
assert_eq!(
x.sci_mantissa_and_exponent_round(rm)
.map(|(m, e, o)| (NiceFloat(m), e, o)),
out.map(|(m, e, o)| (NiceFloat(m), e, o))
);
};
test(Float::from(3u32), Floor, Some((1.5, 1, Equal)));
test(Float::from(3u32), Down, Some((1.5, 1, Equal)));
test(Float::from(3u32), Ceiling, Some((1.5, 1, Equal)));
test(Float::from(3u32), Up, Some((1.5, 1, Equal)));
test(Float::from(3u32), Nearest, Some((1.5, 1, Equal)));
test(Float::from(3u32), Exact, Some((1.5, 1, Equal)));
let x = Float::from(std::f64::consts::PI);
test(x.clone(), Floor, Some((1.5707963, 1, Less)));
test(x.clone(), Down, Some((1.5707963, 1, Less)));
test(x.clone(), Ceiling, Some((1.5707964, 1, Greater)));
test(x.clone(), Up, Some((1.5707964, 1, Greater)));
test(x.clone(), Nearest, Some((1.5707964, 1, Greater)));
test(x.clone(), Exact, None);
test(
Float::from(1000000000u32),
Nearest,
Some((1.8626451, 29, Equal)),
);
test(
Float::from(Natural::from(10u32).pow(52)),
Nearest,
Some((1.670478, 172, Greater)),
);
test(Float::from(Natural::from(10u32).pow(52)), Exact, None);
Trait Implementations§
source§impl<'a> Clone for ComparableFloatRef<'a>
impl<'a> Clone for ComparableFloatRef<'a>
source§fn clone(&self) -> ComparableFloatRef<'a>
fn clone(&self) -> ComparableFloatRef<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for ComparableFloatRef<'a>
impl<'a> Debug for ComparableFloatRef<'a>
source§impl<'a> Deref for ComparableFloatRef<'a>
impl<'a> Deref for ComparableFloatRef<'a>
source§impl<'a> Display for ComparableFloatRef<'a>
impl<'a> Display for ComparableFloatRef<'a>
source§impl<'a> Hash for ComparableFloatRef<'a>
impl<'a> Hash for ComparableFloatRef<'a>
source§fn hash<H: Hasher>(&self, state: &mut H)
fn hash<H: Hasher>(&self, state: &mut H)
Computes a hash of a ComparableFloatRef
.
The hash is compatible with ComparableFloatRef
equality: all NaN
s hash to the same
value.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
source§impl<'a> LowerHex for ComparableFloatRef<'a>
impl<'a> LowerHex for ComparableFloatRef<'a>
source§impl<'a> Ord for ComparableFloatRef<'a>
impl<'a> Ord for ComparableFloatRef<'a>
source§fn cmp(&self, other: &ComparableFloatRef<'a>) -> Ordering
fn cmp(&self, other: &ComparableFloatRef<'a>) -> Ordering
Compares two ComparableFloatRef
s.
This implementation does not follow the IEEE 754 standard. This is how
ComparableFloatRef
s are ordered, least to greatest:
- Negative infinity
- Negative nonzero finite floats
- Negative zero
- NaN
- Positive zero
- Positive nonzero finite floats
- Positive infinity
For different comparison behavior that follows the IEEE 754 standard, consider just using
Float
.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, OneHalf, Zero,
};
use malachite_float::{ComparableFloatRef, Float};
use std::cmp::Ordering::*;
assert_eq!(
ComparableFloatRef(&Float::NAN).partial_cmp(&ComparableFloatRef(&Float::NAN)),
Some(Equal)
);
assert!(ComparableFloatRef(&Float::ZERO) > ComparableFloatRef(&Float::NEGATIVE_ZERO));
assert!(ComparableFloatRef(&Float::ONE) < ComparableFloatRef(&Float::one_prec(100)));
assert!(ComparableFloatRef(&Float::INFINITY) > ComparableFloatRef(&Float::ONE));
assert!(ComparableFloatRef(&Float::NEGATIVE_INFINITY) < ComparableFloatRef(&Float::ONE));
assert!(ComparableFloatRef(&Float::ONE_HALF) < ComparableFloatRef(&Float::ONE));
assert!(ComparableFloatRef(&Float::ONE_HALF) > ComparableFloatRef(&Float::NEGATIVE_ONE));
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> OrdAbs for ComparableFloatRef<'a>
impl<'a> OrdAbs for ComparableFloatRef<'a>
source§fn cmp_abs(&self, other: &ComparableFloatRef<'a>) -> Ordering
fn cmp_abs(&self, other: &ComparableFloatRef<'a>) -> Ordering
Compares the absolute values of two ComparableFloatRef
s.
This implementation does not follow the IEEE 754 standard. This is how
ComparableFloatRef
s are ordered by absolute value, from least to greatest:
- NaN
- Positive and negative zero
- Nonzero finite floats
- Positive and negative infinity
For different comparison behavior that follows the IEEE 754 standard, consider just using
Float
.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Examples
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, OneHalf, Zero,
};
use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_float::{ComparableFloatRef, Float};
use std::cmp::Ordering::*;
assert_eq!(
ComparableFloatRef(&Float::NAN).partial_cmp_abs(&ComparableFloatRef(&Float::NAN)),
Some(Equal)
);
assert_eq!(
ComparableFloatRef(&Float::ZERO)
.partial_cmp_abs(&ComparableFloatRef(&Float::NEGATIVE_ZERO)),
Some(Equal)
);
assert!(ComparableFloatRef(&Float::ONE).lt_abs(&ComparableFloatRef(&Float::one_prec(100))));
assert!(ComparableFloatRef(&Float::INFINITY).gt_abs(&ComparableFloatRef(&Float::ONE)));
assert!(
ComparableFloatRef(&Float::NEGATIVE_INFINITY).gt_abs(&ComparableFloatRef(&Float::ONE))
);
assert!(ComparableFloatRef(&Float::ONE_HALF).lt_abs(&ComparableFloatRef(&Float::ONE)));
assert!(
ComparableFloatRef(&Float::ONE_HALF).lt_abs(&ComparableFloatRef(&Float::NEGATIVE_ONE))
);
source§impl<'a, 'b> PartialEq<ComparableFloatRef<'b>> for ComparableFloatRef<'a>
impl<'a, 'b> PartialEq<ComparableFloatRef<'b>> for ComparableFloatRef<'a>
source§fn eq(&self, other: &ComparableFloatRef<'b>) -> bool
fn eq(&self, other: &ComparableFloatRef<'b>) -> bool
Compares two ComparableFloatRef
s for equality.
This implementation ignores the IEEE 754 standard in favor of an equality operation that
respects the expected properties of symmetry, reflexivity, and transitivity. Using
ComparableFloatRef
, NaN
s are equal to themselves. There is a single, unique NaN
;
there’s no concept of signalling NaN
s. Positive and negative zero are two distinct values,
not equal to each other. ComparableFloatRef
s with different precisions are unequal.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits())
.
§Examples
use malachite_base::num::basic::traits::{NaN, NegativeZero, One, Two, Zero};
use malachite_float::{ComparableFloatRef, Float};
assert_eq!(
ComparableFloatRef(&Float::NAN),
ComparableFloatRef(&Float::NAN)
);
assert_eq!(
ComparableFloatRef(&Float::ZERO),
ComparableFloatRef(&Float::ZERO)
);
assert_eq!(
ComparableFloatRef(&Float::NEGATIVE_ZERO),
ComparableFloatRef(&Float::NEGATIVE_ZERO)
);
assert_ne!(
ComparableFloatRef(&Float::ZERO),
ComparableFloatRef(&Float::NEGATIVE_ZERO)
);
assert_eq!(
ComparableFloatRef(&Float::ONE),
ComparableFloatRef(&Float::ONE)
);
assert_ne!(
ComparableFloatRef(&Float::ONE),
ComparableFloatRef(&Float::TWO)
);
assert_ne!(
ComparableFloatRef(&Float::ONE),
ComparableFloatRef(&Float::one_prec(100))
);
source§impl<'a> PartialOrd for ComparableFloatRef<'a>
impl<'a> PartialOrd for ComparableFloatRef<'a>
source§fn partial_cmp(&self, other: &ComparableFloatRef<'_>) -> Option<Ordering>
fn partial_cmp(&self, other: &ComparableFloatRef<'_>) -> Option<Ordering>
Compares two ComparableFloatRef
s.
See the documentation for the Ord
implementation.
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a> PartialOrdAbs for ComparableFloatRef<'a>
impl<'a> PartialOrdAbs for ComparableFloatRef<'a>
source§fn partial_cmp_abs(&self, other: &ComparableFloatRef<'_>) -> Option<Ordering>
fn partial_cmp_abs(&self, other: &ComparableFloatRef<'_>) -> Option<Ordering>
Compares the absolute values of two ComparableFloatRef
s.
See the documentation for the Ord
implementation.
source§fn lt_abs(&self, other: &Rhs) -> bool
fn lt_abs(&self, other: &Rhs) -> bool
source§fn le_abs(&self, other: &Rhs) -> bool
fn le_abs(&self, other: &Rhs) -> bool
impl<'a> Eq for ComparableFloatRef<'a>
Auto Trait Implementations§
impl<'a> Freeze for ComparableFloatRef<'a>
impl<'a> RefUnwindSafe for ComparableFloatRef<'a>
impl<'a> Send for ComparableFloatRef<'a>
impl<'a> Sync for ComparableFloatRef<'a>
impl<'a> Unpin for ComparableFloatRef<'a>
impl<'a> UnwindSafe for ComparableFloatRef<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more