pub struct NiceFloat<T: PrimitiveFloat>(pub T);
Expand description

NiceFloat is a wrapper around primitive float types that provides nicer Eq, Ord, Hash, Display, and FromStr instances.

In most languages, floats behave weirdly due to the IEEE 754 standard. The NiceFloat type ignores the standard in favor of more intuitive behavior.

  • Using NiceFloat, NaNs are equal to themselves. There is a single, unique NaN; there’s no concept of signalling NaNs. Positive and negative zero are two distinct values, not equal to each other.
  • The NiceFloat hash respects this equality.
  • NiceFloat has a total order. These are the classes of floats, in ascending order:
    • Negative infinity
    • Negative nonzero finite floats
    • Negative zero
    • NaN
    • Positive zero
    • Positive nonzero finite floats
    • Positive infinity
  • NiceFloat uses a different Display implementation than floats do by default in Rust. For example, Rust will format f32::MIN_POSITIVE_SUBNORMAL as something with many zeros, but NiceFloat(f32::MIN_POSITIVE_SUBNORMAL) just formats it as "1.0e-45". The conversion function uses David Tolnay’s ryu crate, with a few modifications:
    • All finite floats have a decimal point. For example, Ryu by itself would convert f32::MIN_POSITIVE_SUBNORMAL to "1e-45".
    • Positive infinity, negative infinity, and NaN are converted to the strings "Infinity", "-Infinity", and “NaN”, respectively.
  • FromStr accepts these strings.

Tuple Fields§

§0: T

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Formats a NiceFloat as a string.

This is identical to the Display::fmt implementation.

Returns the “default value” for a type. Read more

Formats a NiceFloat as a string.

NiceFloat uses a different Display implementation than floats do by default in Rust. For example, Rust will convert f32::MIN_POSITIVE_SUBNORMAL to something with many zeros, but NiceFloat(f32::MIN_POSITIVE_SUBNORMAL) just converts to "1.0e-45". The conversion function uses David Tolnay’s ryu crate, with a few modifications:

  • All finite floats have a decimal point. For example, Ryu by itself would convert f32::MIN_POSITIVE_SUBNORMAL to "1e-45".
  • Positive infinity, negative infinity, and NaN are converted to the strings "Infinity", "-Infinity", and “NaN”, respectively.
Worst-case complexity

Constant time and additional memory.

Examples
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::float::NiceFloat;

assert_eq!(NiceFloat(0.0).to_string(), "0.0");
assert_eq!(NiceFloat(-0.0).to_string(), "-0.0");
assert_eq!(NiceFloat(f32::POSITIVE_INFINITY).to_string(), "Infinity");
assert_eq!(NiceFloat(f32::NEGATIVE_INFINITY).to_string(), "-Infinity");
assert_eq!(NiceFloat(f32::NAN).to_string(), "NaN");

assert_eq!(NiceFloat(1.0).to_string(), "1.0");
assert_eq!(NiceFloat(-1.0).to_string(), "-1.0");
assert_eq!(
    NiceFloat(f32::MIN_POSITIVE_SUBNORMAL).to_string(),
    "1.0e-45"
);
assert_eq!(
    NiceFloat(std::f64::consts::E).to_string(),
    "2.718281828459045"
);
assert_eq!(
    NiceFloat(std::f64::consts::PI).to_string(),
    "3.141592653589793"
);

Converts a &str to a NiceFloat.

If the &str does not represent a valid NiceFloat, an Err is returned.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ = src.len().

Examples
use malachite_base::num::float::NiceFloat;
use std::str::FromStr;

assert_eq!(NiceFloat::from_str("NaN").unwrap(), NiceFloat(f32::NAN));
assert_eq!(NiceFloat::from_str("-0.00").unwrap(), NiceFloat(-0.0f64));
assert_eq!(NiceFloat::from_str(".123").unwrap(), NiceFloat(0.123f32));
The associated error which can be returned from parsing.

Computes a hash of a NiceFloat.

The hash is compatible with NiceFloat equality: all NaNs hash to the same value.

Worst-case complexity

Constant time and additional memory.

Feeds a slice of this type into the given Hasher. Read more

Compares two NiceFloats.

This implementation ignores the IEEE 754 standard in favor of an equality operation that respects the expected properties of symmetry, reflexivity, and transitivity. Using NiceFloat, NaNs are equal to themselves. There is a single, unique NaN; there’s no concept of signalling NaNs. Positive and negative zero are two distinct values, not equal to each other.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::float::NiceFloat;

assert!(NiceFloat(0.0) > NiceFloat(-0.0));
assert!(NiceFloat(f32::NAN) < NiceFloat(0.0));
assert!(NiceFloat(f32::NAN) > NiceFloat(-0.0));
assert!(NiceFloat(f32::POSITIVE_INFINITY) > NiceFloat(f32::NAN));
assert!(NiceFloat(f32::NAN) < NiceFloat(1.0));
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more

Compares two NiceFloats for equality.

This implementation ignores the IEEE 754 standard in favor of a comparison operation that respects the expected properties of antisymmetry, reflexivity, and transitivity. NiceFloat has a total order. These are the classes of floats, in ascending order:

  • Negative infinity
  • Negative nonzero finite floats
  • Negative zero
  • NaN
  • Positive zero
  • Positive nonzero finite floats
  • Positive infinity
Worst-case complexity

Constant time and additional memory.

Examples
use malachite_base::num::float::NiceFloat;

assert_eq!(NiceFloat(0.0), NiceFloat(0.0));
assert_eq!(NiceFloat(f32::NAN), NiceFloat(f32::NAN));
assert_ne!(NiceFloat(f32::NAN), NiceFloat(0.0));
assert_ne!(NiceFloat(0.0), NiceFloat(-0.0));
assert_eq!(NiceFloat(1.0), NiceFloat(1.0));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Compares a NiceFloat to another NiceFloat.

See the documentation for the Ord implementation.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of a signed type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer and not too large or too small.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a floating point type to a value of an unsigned type, returning an error if an exact conversion is not possible.

The conversion succeeds if the floating point value is an integer, not negative (negative zero is ok), and not too large.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of a signed type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the precision of the signed value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Converts a value of an unsigned type to a value of a floating point type, returning an error if an exact conversion is not possible.

The conversion succeeds if the unsigned value is not too large to represent (which can only happen when converting a u128 to an f32) and the precision of the unsigned value is not too high.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

Returns the String produced by Ts Debug implementation.

Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.