Struct rug::float::MiniFloat

source ·
pub struct MiniFloat { /* private fields */ }
Expand description

A small float that does not require any memory allocation.

This can be useful when you have a primitive number type but need a reference to a Float. The MiniFloat will have a precision according to the type of the primitive used to set its value.

  • i8, u8: the MiniFloat will have eight bits of precision.
  • i16, u16: the MiniFloat will have 16 bits of precision.
  • i32, u32: the MiniFloat will have 32 bits of precision.
  • i64, u64: the MiniFloat will have 64 bits of precision.
  • i128, u128: the MiniFloat will have 128 bits of precision.
  • isize, usize: the MiniFloat will have 32 or 64 bits of precision, depending on the platform.
  • f32: the MiniFloat will have 24 bits of precision.
  • f64: the MiniFloat will have 53 bits of precision.
  • Special: the MiniFloat will have the minimum possible precision.

The borrow method returns an object that can be coerced to a Float, as it implements Deref<Target = Float>.

Examples

use rug::float::MiniFloat;
use rug::Float;
// `a` requires a heap allocation, has 53-bit precision
let mut a = Float::with_val(53, 250);
// `b` can reside on the stack
let b = MiniFloat::from(-100f64);
a += &*b.borrow();
assert_eq!(a, 150);
// another computation:
a *= &*b.borrow();
assert_eq!(a, -15000);

Implementations§

source§

impl MiniFloat

source

pub const fn new() -> Self

Creates a MiniFloat with value 0 and the minimum possible precision.

Examples
use rug::float::MiniFloat;
let f = MiniFloat::new();
// Borrow f as if it were Float.
assert_eq!(*f.borrow(), 0);
source

pub unsafe fn as_nonreallocating_float(&mut self) -> &mut Float

Returns a mutable reference to a Float for simple operations that do not need to change the precision of the number.

Safety

It is undefined behavior modify the precision of the referenced Float or to swap it with another number.

Examples
use rug::float::MiniFloat;
let mut f = MiniFloat::from(1.0f32);
// addition does not change the precision
unsafe {
    *f.as_nonreallocating_float() += 2.0;
}
assert_eq!(*f.borrow(), 3.0);
source

pub fn borrow(&self) -> impl Deref<Target = Float> + '_

Borrows the floating-point number.

The returned object implements Deref<Target = Float>.

The borrow lasts until the returned object exits scope. Multiple borrows can be taken at the same time.

Examples
use rug::float::MiniFloat;
use rug::Float;
let f = MiniFloat::from(-13i32);
let b = f.borrow();
let abs_ref = b.abs_ref();
assert_eq!(Float::with_val(53, abs_ref), 13);
source

pub fn borrow_excl(&mut self) -> &Float

Borrows the floating-point number exclusively.

This is similar to the borrow method, but it requires exclusive access to the underlying MiniFloat; the returned reference can however be shared. The exclusive access is required to reduce the amount of housekeeping necessary, providing a more efficient operation.

Examples
use rug::float::MiniFloat;
use rug::Float;
let mut f = MiniFloat::from(-13i32);
let b = f.borrow_excl();
let abs_ref = b.abs_ref();
assert_eq!(Float::with_val(53, abs_ref), 13);

Trait Implementations§

source§

impl Assign<&MiniFloat> for MiniFloat

source§

fn assign(&mut self, other: &Self)

Peforms the assignement.
source§

impl<T: ToMini> Assign<T> for MiniFloat

source§

fn assign(&mut self, src: T)

Peforms the assignement.
source§

impl Assign for MiniFloat

source§

fn assign(&mut self, other: Self)

Peforms the assignement.
source§

impl AssignRound<&MiniFloat> for Float

§

type Round = Round

The rounding method.
§

type Ordering = Ordering

The direction from rounding.
source§

fn assign_round(&mut self, src: &MiniFloat, round: Round) -> Ordering

Peforms the assignment. Read more
source§

impl AssignRound<MiniFloat> for Float

§

type Round = Round

The rounding method.
§

type Ordering = Ordering

The direction from rounding.
source§

fn assign_round(&mut self, src: MiniFloat, round: Round) -> Ordering

Peforms the assignment. Read more
source§

impl Binary for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Clone for MiniFloat

source§

fn clone(&self) -> MiniFloat

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl Default for MiniFloat

source§

fn default() -> Self

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

impl Display for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl<T: ToMini> From<T> for MiniFloat

source§

fn from(src: T) -> Self

Converts to this type from the input type.
source§

impl LowerExp for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl LowerHex for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Octal for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl PartialEq<Float> for MiniFloat

source§

fn eq(&self, other: &Float) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<MiniFloat> for Float

source§

fn eq(&self, other: &MiniFloat) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Float> for MiniFloat

source§

fn partial_cmp(&self, other: &Float) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd<MiniFloat> for Float

source§

fn partial_cmp(&self, other: &MiniFloat) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl UpperExp for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl UpperHex for MiniFloat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Copy for MiniFloat

source§

impl Send for MiniFloat

source§

impl Sync for MiniFloat

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.