# 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 54 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 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;
fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { ... }
fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> { ... }
fn ieee_rem(self, rhs: 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 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_smallest(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.

## Associated Constants

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

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

## Required methods

#### fn smallest_normalized() -> Self

#### fn smallest_normalized() -> Self

Smallest (by magnitude) normalized finite number.

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

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

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

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

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

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

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

#### fn is_negative(self) -> bool

#### fn is_negative(self) -> bool

IEEE-754R isSignMinus: Returns whether the current value is negative.

This applies to zeros and NaNs as well.

#### fn is_denormal(self) -> bool

#### fn is_denormal(self) -> bool

IEEE-754R isSubnormal(): Returns whether the float is a denormal.

#### fn is_signaling(self) -> bool

#### fn is_signaling(self) -> bool

Returns `true`

if the float is a signaling NaN.

#### fn get_exact_inverse(self) -> Option<Self>

#### fn get_exact_inverse(self) -> Option<Self>

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

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

Returns: self * 2^{exp} for integral exponents.
Equivalent to C standard library function `ldexp`

.

## Provided methods

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.

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

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

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.

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.

IEEE-754R isNormal: Returns whether 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.

Returns `true`

if the current value is zero, subnormal, or
normal.

This means that the value is not infinite or NaN.

#### fn is_infinite(self) -> bool

#### fn is_infinite(self) -> bool

IEEE-754R isInfinite(): Returns whether the float is infinity.

#### 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_smallest(self) -> bool

#### fn is_smallest(self) -> bool

Returns `true`

if the number has the smallest possible non-zero
magnitude in the current semantics.

#### fn is_largest(self) -> bool

#### fn is_largest(self) -> bool

Returns `true`

if the number has the largest possible finite
magnitude in the current semantics.

#### fn is_integer(self) -> bool

#### fn is_integer(self) -> bool

Returns `true`

if the number is an exact integer.