pub fn primitive_float_sqrt_rational<T>(x: &Rational) -> Twhere
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float> + PrimitiveFloat,Expand description
Computes the square root of a Rational, returning a primitive float result.
If the square root is equidistant from two primitive floats, the primitive float with fewer 1s
in its binary expansion is chosen. See RoundingMode for a description of the Nearest
rounding mode.
The square root of any negative number is NaN.
$$ f(x) = \sqrt{x}+\varepsilon. $$
- If $\sqrt{x}$ is infinite, zero, or
NaN, $\varepsilon$ may be ignored or assumed to be 0. - If $\sqrt{x}$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
\sqrt{x}\rfloor-p}$, where $p$ is precision of the output (typically 24 if
Tis af32and 53 ifTis af64, but less if the output is subnormal).
Special cases:
- $f(0)=0.0$
Overflow:
- If the absolute value of the result is too large to represent, $\infty$ is returned instead.
- If the absolute value of the result is too small to represent, 0.0 is returned instead.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::float::NiceFloat;
use malachite_float::arithmetic::sqrt::primitive_float_sqrt_rational;
use malachite_q::Rational;
assert_eq!(
NiceFloat(primitive_float_sqrt_rational::<f64>(&Rational::ZERO)),
NiceFloat(0.0)
);
assert_eq!(
NiceFloat(primitive_float_sqrt_rational::<f64>(
&Rational::from_unsigneds(1u8, 3)
)),
NiceFloat(0.5773502691896257)
);
assert_eq!(
NiceFloat(primitive_float_sqrt_rational::<f64>(&Rational::from(10000))),
NiceFloat(100.0)
);
assert_eq!(
NiceFloat(primitive_float_sqrt_rational::<f64>(&Rational::from(
-10000
))),
NiceFloat(f64::NAN)
);