Trait rustc_apfloat::Float

source ·
pub trait Float: Copy + Default + FromStr<Err = ParseError> + PartialOrd + Display + Neg<Output = Self> + AddAssign + SubAssign + MulAssign + DivAssign + RemAssign + Add<Output = StatusAnd<Self>> + Sub<Output = StatusAnd<Self>> + Mul<Output = StatusAnd<Self>> + Div<Output = StatusAnd<Self>> + Rem<Output = StatusAnd<Self>> {
    const BITS: usize;
    const PRECISION: usize;
    const MAX_EXP: ExpInt;
    const MIN_EXP: ExpInt;
    const ZERO: Self;
    const INFINITY: Self;
    const NAN: Self;
    const SMALLEST: Self;
Show 59 methods // Required methods fn qnan(payload: Option<u128>) -> Self; fn snan(payload: Option<u128>) -> Self; fn largest() -> Self; fn smallest_normalized() -> Self; fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn mul_add_r( self, multiplicand: Self, addend: Self, round: Round ) -> StatusAnd<Self>; fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn ieee_rem(self, rhs: Self) -> StatusAnd<Self>; fn c_fmod(self, rhs: Self) -> StatusAnd<Self>; fn round_to_integral(self, round: Round) -> StatusAnd<Self>; fn next_up(self) -> StatusAnd<Self>; fn from_bits(input: u128) -> Self; fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>; fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>; fn to_bits(self) -> u128; fn to_u128_r( self, width: usize, round: Round, is_exact: &mut bool ) -> StatusAnd<u128>; fn cmp_abs_normal(self, rhs: Self) -> Ordering; fn bitwise_eq(self, rhs: Self) -> bool; fn is_negative(self) -> bool; fn is_denormal(self) -> bool; fn is_signaling(self) -> bool; fn category(self) -> Category; fn get_exact_inverse(self) -> Option<Self>; fn ilogb(self) -> ExpInt; fn scalbn_r(self, exp: ExpInt, round: Round) -> Self; fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self; // Provided methods fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { ... } fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> { ... } fn next_down(self) -> StatusAnd<Self> { ... } fn abs(self) -> Self { ... } fn copy_sign(self, rhs: Self) -> Self { ... } fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self> { ... } fn from_i128(input: i128) -> StatusAnd<Self> { ... } fn from_u128(input: u128) -> StatusAnd<Self> { ... } fn to_i128_r( self, width: usize, round: Round, is_exact: &mut bool ) -> StatusAnd<i128> { ... } fn to_i128(self, width: usize) -> StatusAnd<i128> { ... } fn to_u128(self, width: usize) -> StatusAnd<u128> { ... } fn min(self, other: Self) -> Self { ... } fn max(self, other: Self) -> Self { ... } fn minimum(self, other: Self) -> Self { ... } fn maximum(self, other: Self) -> Self { ... } fn is_normal(self) -> bool { ... } fn is_finite(self) -> bool { ... } fn is_zero(self) -> bool { ... } fn is_infinite(self) -> bool { ... } fn is_nan(self) -> bool { ... } fn is_non_zero(self) -> bool { ... } fn is_finite_non_zero(self) -> bool { ... } fn is_pos_zero(self) -> bool { ... } fn is_neg_zero(self) -> bool { ... } fn is_pos_infinity(self) -> bool { ... } fn is_neg_infinity(self) -> bool { ... } fn is_smallest(self) -> bool { ... } fn is_smallest_normalized(self) -> bool { ... } fn is_largest(self) -> bool { ... } fn is_integer(self) -> bool { ... } fn scalbn(self, exp: ExpInt) -> Self { ... } fn frexp(self, exp: &mut ExpInt) -> Self { ... }
}
Expand description

A self-contained host- and target-independent arbitrary-precision floating-point software implementation.

apfloat uses significand bignum integer arithmetic as provided by functions in the ieee::sig.

Written for clarity rather than speed, in particular with a view to use in the front-end of a cross compiler so that target arithmetic can be correctly performed on the host. Performance should nonetheless be reasonable, particularly for its intended use. It may be useful as a base implementation for a run-time library during development of a faster target-specific one.

All 5 rounding modes in the IEEE-754R draft are handled correctly for all implemented operations. Currently implemented operations are add, subtract, multiply, divide, fused-multiply-add, conversion-to-float, conversion-to-integer and conversion-from-integer. New rounding modes (e.g. away from zero) can be added with three or four lines of code.

Four formats are built-in: IEEE single precision, double precision, quadruple precision, and x87 80-bit extended double (when operating with full extended precision). Adding a new format that obeys IEEE semantics only requires adding two lines of code: a declaration and definition of the format.

All operations return the status of that operation as an exception bit-mask, so multiple operations can be done consecutively with their results or-ed together. The returned status can be useful for compiler diagnostics; e.g., inexact, underflow and overflow can be easily diagnosed on constant folding, and compiler optimizers can determine what exceptions would be raised by folding operations and optimize, or perhaps not optimize, accordingly.

At present, underflow tininess is detected after rounding; it should be straight forward to add support for the before-rounding case too.

The library reads hexadecimal floating point numbers as per C99, and correctly rounds if necessary according to the specified rounding mode. Syntax is required to have been validated by the caller.

It also reads decimal floating point numbers and correctly rounds according to the specified rounding mode.

Non-zero finite numbers are represented internally as a sign bit, a 16-bit signed exponent, and the significand as an array of integer limbs. After normalization of a number of precision P the exponent is within the range of the format, and if the number is not denormal the P-th bit of the significand is set as an explicit integer bit. For denormals the most significant bit is shifted right so that the exponent is maintained at the format’s minimum, so that the smallest denormal has just the least significant bit of the significand set. The sign of zeros and infinities is significant; the exponent and significand of such numbers is not stored, but has a known implicit (deterministic) value: 0 for the significands, 0 for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and significand are deterministic, although not really meaningful, and preserved in non-conversion operations. The exponent is implicitly all 1 bits.

apfloat does not provide any exception handling beyond default exception handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause by encoding Signaling NaNs with the first bit of its trailing significand as 0.

Future work

Some features that may or may not be worth adding:

Optional ability to detect underflow tininess before rounding.

New formats: x87 in single and double precision mode (IEEE apart from extended exponent range) (hard).

New operations: sqrt, nexttoward.

Required Associated Constants§

source

const BITS: usize

Total number of bits in the in-memory format.

source

const PRECISION: usize

Number of bits in the significand. This includes the integer bit.

source

const MAX_EXP: ExpInt

The largest E such that 2^E is representable; this matches the definition of IEEE 754.

source

const MIN_EXP: ExpInt

The smallest E such that 2^E is a normalized number; this matches the definition of IEEE 754.

source

const ZERO: Self

Positive Zero.

source

const INFINITY: Self

Positive Infinity.

source

const NAN: Self

NaN (Not a Number).

source

const SMALLEST: Self

Smallest (by magnitude) finite number. Might be denormalized, which implies a relative loss of precision.

Required Methods§

source

fn qnan(payload: Option<u128>) -> Self

Factory for QNaN values.

source

fn snan(payload: Option<u128>) -> Self

Factory for SNaN values.

source

fn largest() -> Self

Largest finite number.

source

fn smallest_normalized() -> Self

Smallest (by magnitude) normalized finite number.

source

fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>

source

fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>

source

fn mul_add_r( self, multiplicand: Self, addend: Self, round: Round ) -> StatusAnd<Self>

source

fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>

source

fn ieee_rem(self, rhs: Self) -> StatusAnd<Self>

IEEE remainder.

source

fn c_fmod(self, rhs: Self) -> StatusAnd<Self>

C fmod, or llvm frem.

source

fn round_to_integral(self, round: Round) -> StatusAnd<Self>

source

fn next_up(self) -> StatusAnd<Self>

IEEE-754R 2008 5.3.1: nextUp.

source

fn from_bits(input: u128) -> Self

source

fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>

source

fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>

source

fn to_bits(self) -> u128

source

fn to_u128_r( self, width: usize, round: Round, is_exact: &mut bool ) -> StatusAnd<u128>

source

fn cmp_abs_normal(self, rhs: Self) -> Ordering

source

fn bitwise_eq(self, rhs: Self) -> bool

Bitwise comparison for equality (QNaNs compare equal, 0!=-0).

source

fn is_negative(self) -> bool

IEEE-754R isSignMinus: Returns true if and only if the current value is negative.

This applies to zeros and NaNs as well.

source

fn is_denormal(self) -> bool

IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.

source

fn is_signaling(self) -> bool

Returns true if and only if the float is a signaling NaN.

source

fn category(self) -> Category

source

fn get_exact_inverse(self) -> Option<Self>

If this value has an exact multiplicative inverse, return it.

source

fn ilogb(self) -> ExpInt

Returns the exponent of the internal representation of the Float.

Because the radix of Float is 2, this is equivalent to floor(log2(x)). For special Float values, this returns special error codes:

NaN -> \c IEK_NAN 0 -> \c IEK_ZERO Inf -> \c IEK_INF

source

fn scalbn_r(self, exp: ExpInt, round: Round) -> Self

Returns: self * 2^exp for integral exponents.

source

fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self

Equivalent of C standard library function.

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).

Provided Methods§

source

fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self>

source

fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self>

source

fn next_down(self) -> StatusAnd<Self>

IEEE-754R 2008 5.3.1: nextDown.

NOTE since nextDown(x) = -nextUp(-x), we only implement nextUp with appropriate sign switching before/after the computation.

source

fn abs(self) -> Self

source

fn copy_sign(self, rhs: Self) -> Self

source

fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self>

source

fn from_i128(input: i128) -> StatusAnd<Self>

source

fn from_u128(input: u128) -> StatusAnd<Self>

source

fn to_i128_r( self, width: usize, round: Round, is_exact: &mut bool ) -> StatusAnd<i128>

Convert a floating point number to an integer according to the rounding mode. In case of an invalid operation exception, deterministic values are returned, namely zero for NaNs and the minimal or maximal value respectively for underflow or overflow. If the rounded value is in range but the floating point number is not the exact integer, the C standard doesn’t require an inexact exception to be raised. IEEE-854 does require it so we do that.

Note that for conversions to integer type the C standard requires round-to-zero to always be used.

The *is_exact output tells whether the result is exact, in the sense that converting it back to the original floating point type produces the original value. This is almost equivalent to result==Status::OK, except for negative zeroes.

source

fn to_i128(self, width: usize) -> StatusAnd<i128>

source

fn to_u128(self, width: usize) -> StatusAnd<u128>

source

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

Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if both are not NaN. If either argument is a NaN, returns the other argument.

source

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

Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if both are not NaN. If either argument is a NaN, returns the other argument.

source

fn minimum(self, other: Self) -> Self

Implements IEEE 754-2018 minimum semantics. Returns the smaller of 2 arguments, propagating NaNs and treating -0 as less than +0.

source

fn maximum(self, other: Self) -> Self

Implements IEEE 754-2018 maximum semantics. Returns the larger of 2 arguments, propagating NaNs and treating -0 as less than +0.

source

fn is_normal(self) -> bool

IEEE-754R isNormal: Returns true if and only if the current value is normal.

This implies that the current value of the float is not zero, subnormal, infinite, or NaN following the definition of normality from IEEE-754R.

source

fn is_finite(self) -> bool

Returns true if and only if the current value is zero, subnormal, or normal.

This means that the value is not infinite or NaN.

source

fn is_zero(self) -> bool

Returns true if and only if the float is plus or minus zero.

source

fn is_infinite(self) -> bool

IEEE-754R isInfinite(): Returns true if and only if the float is infinity.

source

fn is_nan(self) -> bool

Returns true if and only if the float is a quiet or signaling NaN.

source

fn is_non_zero(self) -> bool

source

fn is_finite_non_zero(self) -> bool

source

fn is_pos_zero(self) -> bool

source

fn is_neg_zero(self) -> bool

source

fn is_pos_infinity(self) -> bool

source

fn is_neg_infinity(self) -> bool

source

fn is_smallest(self) -> bool

Returns true if and only if the number has the smallest possible non-zero magnitude in the current semantics.

source

fn is_smallest_normalized(self) -> bool

Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.

source

fn is_largest(self) -> bool

Returns true if and only if the number has the largest possible finite magnitude in the current semantics.

source

fn is_integer(self) -> bool

Returns true if and only if the number is an exact integer.

source

fn scalbn(self, exp: ExpInt) -> Self

source

fn frexp(self, exp: &mut ExpInt) -> Self

Implementors§

source§

impl<F: FloatConvert<IeeeFloat<FallbackS<F>>>> Float for DoubleFloat<F>where Self: From<IeeeFloat<FallbackS<F>>>,

source§

const BITS: usize = _

source§

const PRECISION: usize = Fallback<F>::PRECISION

source§

const MAX_EXP: ExpInt = Fallback<F>::MAX_EXP

source§

const MIN_EXP: ExpInt = Fallback<F>::MIN_EXP

source§

const ZERO: Self = _

source§

const INFINITY: Self = _

source§

const NAN: Self = _

source§

const SMALLEST: Self = _

source§

impl<S: Semantics> Float for IeeeFloat<S>

source§

const BITS: usize = S::BITS

source§

const PRECISION: usize = S::PRECISION

source§

const MAX_EXP: ExpInt = S::MAX_EXP

source§

const MIN_EXP: ExpInt = S::MIN_EXP

source§

const ZERO: Self = _

source§

const INFINITY: Self = _

source§

const NAN: Self = _

source§

const SMALLEST: Self = _