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
: theMiniFloat
will have eight bits of precision.i16
,u16
: theMiniFloat
will have 16 bits of precision.i32
,u32
: theMiniFloat
will have 32 bits of precision.i64
,u64
: theMiniFloat
will have 64 bits of precision.i128
,u128
: theMiniFloat
will have 128 bits of precision.isize
,usize
: theMiniFloat
will have 32 or 64 bits of precision, depending on the platform.f32
: theMiniFloat
will have 24 bits of precision.f64
: theMiniFloat
will have 53 bits of precision.Special
: theMiniFloat
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
impl MiniFloat
sourcepub const fn new() -> Self
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);
sourcepub unsafe fn as_nonreallocating_float(&mut self) -> &mut Float
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);
sourcepub fn borrow(&self) -> impl Deref<Target = Float> + '_
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);
sourcepub fn borrow_excl(&mut self) -> &Float
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 AssignRound<&MiniFloat> for Float
impl AssignRound<&MiniFloat> for Float
source§impl AssignRound<MiniFloat> for Float
impl AssignRound<MiniFloat> for Float
source§impl PartialEq<Float> for MiniFloat
impl PartialEq<Float> for MiniFloat
source§impl PartialEq<MiniFloat> for Float
impl PartialEq<MiniFloat> for Float
source§impl PartialOrd<Float> for MiniFloat
impl PartialOrd<Float> for MiniFloat
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<MiniFloat> for Float
impl PartialOrd<MiniFloat> for Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more