#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![no_std]
#![forbid(unsafe_code)]
-
#![feature(nll)]
#[macro_use]
use core::cmp::Ordering;
use core::fmt;
-use core::ops::{Neg, Add, Sub, Mul, Div, Rem};
-use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
+use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
+use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
use core::str::FromStr;
bitflags::bitflags! {
impl Status {
pub fn and<T>(self, value: T) -> StatusAnd<T> {
- StatusAnd {
- status: self,
- value,
- }
+ StatusAnd { status: self, value }
}
}
impl<T> StatusAnd<T> {
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
- StatusAnd {
- status: self.status,
- value: f(self.value),
- }
+ StatusAnd { status: self.status, value: f(self.value) }
}
}
value
}
}
- }
+ };
}
/// Category of internally-represented number.
///
/// New operations: sqrt, nexttoward.
///
-pub trait Float
- : Copy
+pub trait Float:
+ Copy
+ Default
+ FromStr<Err = ParseError>
+ PartialOrd
+ Sub<Output = StatusAnd<Self>>
+ Mul<Output = StatusAnd<Self>>
+ Div<Output = StatusAnd<Self>>
- + Rem<Output = StatusAnd<Self>> {
+ + Rem<Output = StatusAnd<Self>>
+{
/// Total number of bits in the in-memory format.
const BITS: usize;
if self.is_negative() { -self } else { self }
}
fn copy_sign(self, rhs: Self) -> Self {
- if self.is_negative() != rhs.is_negative() {
- -self
- } else {
- self
- }
+ if self.is_negative() != rhs.is_negative() { -self } else { self }
}
// Conversions
} else {
// Positive case is simpler, can pretend it's a smaller unsigned
// integer, and `to_u128` will take care of all the edge cases.
- self.to_u128_r(width - 1, round, is_exact).map(
- |r| r as i128,
- )
+ self.to_u128_r(width - 1, round, is_exact).map(|r| r as i128)
}
}
fn to_i128(self, width: usize) -> StatusAnd<i128> {
if !self.is_finite() {
return false;
}
- self.round_to_integral(Round::TowardZero).value.bitwise_eq(
- self,
- )
+ self.round_to_integral(Round::TowardZero).value.bitwise_eq(self)
}
/// If this value has an exact multiplicative inverse, return it.
fn ilogb(self) -> ExpInt;
/// Returns: self * 2<sup>exp</sup> for integral exponents.
+ /// Equivalent to C standard library function `ldexp`.
fn scalbn_r(self, exp: ExpInt, round: Round) -> Self;
fn scalbn(self, exp: ExpInt) -> Self {
self.scalbn_r(exp, Round::NearestTiesToEven)
}
- /// Equivalent of C standard library function.
+ /// Equivalent to C standard library function with the same name.
///
/// While the C standard says exp is an unspecified value for infinity and nan,
/// this returns INT_MAX for infinities, and INT_MIN for NaNs (see `ilogb`).
macro_rules! float_common_impls {
($ty:ident<$t:tt>) => {
- impl<$t> Default for $ty<$t> where Self: Float {
+ impl<$t> Default for $ty<$t>
+ where
+ Self: Float,
+ {
fn default() -> Self {
Self::ZERO
}
}
- impl<$t> ::core::str::FromStr for $ty<$t> where Self: Float {
+ impl<$t> ::core::str::FromStr for $ty<$t>
+ where
+ Self: Float,
+ {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, ParseError> {
Self::from_str_r(s, Round::NearestTiesToEven).map(|x| x.value)
// Rounding ties to the nearest even, by default.
- impl<$t> ::core::ops::Add for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::Add for $ty<$t>
+ where
+ Self: Float,
+ {
type Output = StatusAnd<Self>;
fn add(self, rhs: Self) -> StatusAnd<Self> {
self.add_r(rhs, Round::NearestTiesToEven)
}
}
- impl<$t> ::core::ops::Sub for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::Sub for $ty<$t>
+ where
+ Self: Float,
+ {
type Output = StatusAnd<Self>;
fn sub(self, rhs: Self) -> StatusAnd<Self> {
self.sub_r(rhs, Round::NearestTiesToEven)
}
}
- impl<$t> ::core::ops::Mul for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::Mul for $ty<$t>
+ where
+ Self: Float,
+ {
type Output = StatusAnd<Self>;
fn mul(self, rhs: Self) -> StatusAnd<Self> {
self.mul_r(rhs, Round::NearestTiesToEven)
}
}
- impl<$t> ::core::ops::Div for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::Div for $ty<$t>
+ where
+ Self: Float,
+ {
type Output = StatusAnd<Self>;
fn div(self, rhs: Self) -> StatusAnd<Self> {
self.div_r(rhs, Round::NearestTiesToEven)
}
}
- impl<$t> ::core::ops::Rem for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::Rem for $ty<$t>
+ where
+ Self: Float,
+ {
type Output = StatusAnd<Self>;
fn rem(self, rhs: Self) -> StatusAnd<Self> {
self.c_fmod(rhs)
}
}
- impl<$t> ::core::ops::AddAssign for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::AddAssign for $ty<$t>
+ where
+ Self: Float,
+ {
fn add_assign(&mut self, rhs: Self) {
*self = (*self + rhs).value;
}
}
- impl<$t> ::core::ops::SubAssign for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::SubAssign for $ty<$t>
+ where
+ Self: Float,
+ {
fn sub_assign(&mut self, rhs: Self) {
*self = (*self - rhs).value;
}
}
- impl<$t> ::core::ops::MulAssign for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::MulAssign for $ty<$t>
+ where
+ Self: Float,
+ {
fn mul_assign(&mut self, rhs: Self) {
*self = (*self * rhs).value;
}
}
- impl<$t> ::core::ops::DivAssign for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::DivAssign for $ty<$t>
+ where
+ Self: Float,
+ {
fn div_assign(&mut self, rhs: Self) {
*self = (*self / rhs).value;
}
}
- impl<$t> ::core::ops::RemAssign for $ty<$t> where Self: Float {
+ impl<$t> ::core::ops::RemAssign for $ty<$t>
+ where
+ Self: Float,
+ {
fn rem_assign(&mut self, rhs: Self) {
*self = (*self % rhs).value;
}
}
- }
+ };
}
pub mod ieee;