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.
bool
: theMiniFloat
will have the minimum possible precision.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 const fn const_from_bool(val: bool) -> Self
pub const fn const_from_bool(val: bool) -> Self
Creates a MiniFloat
from a bool
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float;
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const ONE_MINI: MiniFloat = MiniFloat::const_from_bool(true);
const ONE_BORROW: BorrowFloat = ONE_MINI.borrow();
const ONE: &Float = BorrowFloat::const_deref(&ONE_BORROW);
assert_eq!(*ONE, 1);
assert_eq!(ONE.prec(), float::prec_min());
sourcepub const fn const_from_i8(val: i8) -> Self
pub const fn const_from_i8(val: i8) -> Self
Creates a MiniFloat
from a i8
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TWO_MINI: MiniFloat = MiniFloat::const_from_i8(2i8);
const TWO_BORROW: BorrowFloat = TWO_MINI.borrow();
const TWO: &Float = BorrowFloat::const_deref(&TWO_BORROW);
assert_eq!(*TWO, 2);
assert_eq!(TWO.prec(), i8::BITS);
sourcepub const fn const_from_i16(val: i16) -> Self
pub const fn const_from_i16(val: i16) -> Self
Creates a MiniFloat
from a i16
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const KIBI_MINI: MiniFloat = MiniFloat::const_from_i16(1i16 << 10);
const KIBI_BORROW: BorrowFloat = KIBI_MINI.borrow();
const KIBI: &Float = BorrowFloat::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1i16 << 10);
assert_eq!(KIBI.prec(), i16::BITS);
sourcepub const fn const_from_i32(val: i32) -> Self
pub const fn const_from_i32(val: i32) -> Self
Creates a MiniFloat
from a i32
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const MEBI_MINI: MiniFloat = MiniFloat::const_from_i32(1i32 << 20);
const MEBI_BORROW: BorrowFloat = MEBI_MINI.borrow();
const MEBI: &Float = BorrowFloat::const_deref(&MEBI_BORROW);
assert_eq!(*MEBI, 1i32 << 20);
assert_eq!(MEBI.prec(), i32::BITS);
sourcepub const fn const_from_i64(val: i64) -> Self
pub const fn const_from_i64(val: i64) -> Self
Creates a MiniFloat
from a i64
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TEBI_MINI: MiniFloat = MiniFloat::const_from_i64(1i64 << 40);
const TEBI_BORROW: BorrowFloat = TEBI_MINI.borrow();
const TEBI: &Float = BorrowFloat::const_deref(&TEBI_BORROW);
assert_eq!(*TEBI, 1i64 << 40);
assert_eq!(TEBI.prec(), i64::BITS);
sourcepub const fn const_from_i128(val: i128) -> Self
pub const fn const_from_i128(val: i128) -> Self
Creates a MiniFloat
from a i128
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const YOBI_MINI: MiniFloat = MiniFloat::const_from_i128(1i128 << 80);
const YOBI_BORROW: BorrowFloat = YOBI_MINI.borrow();
const YOBI: &Float = BorrowFloat::const_deref(&YOBI_BORROW);
assert_eq!(*YOBI, 1i128 << 80);
assert_eq!(YOBI.prec(), i128::BITS);
sourcepub const fn const_from_isize(val: isize) -> Self
pub const fn const_from_isize(val: isize) -> Self
Creates a MiniFloat
from a isize
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const KIBI_MINI: MiniFloat = MiniFloat::const_from_isize(1isize << 10);
const KIBI_BORROW: BorrowFloat = KIBI_MINI.borrow();
const KIBI: &Float = BorrowFloat::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1isize << 10);
assert_eq!(KIBI.prec(), isize::BITS);
sourcepub const fn const_from_u8(val: u8) -> Self
pub const fn const_from_u8(val: u8) -> Self
Creates a MiniFloat
from a u8
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TWO_MINI: MiniFloat = MiniFloat::const_from_u8(2u8);
const TWO_BORROW: BorrowFloat = TWO_MINI.borrow();
const TWO: &Float = BorrowFloat::const_deref(&TWO_BORROW);
assert_eq!(*TWO, 2);
assert_eq!(TWO.prec(), u8::BITS);
sourcepub const fn const_from_u16(val: u16) -> Self
pub const fn const_from_u16(val: u16) -> Self
Creates a MiniFloat
from a u16
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const KIBI_MINI: MiniFloat = MiniFloat::const_from_u16(1u16 << 10);
const KIBI_BORROW: BorrowFloat = KIBI_MINI.borrow();
const KIBI: &Float = BorrowFloat::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1u16 << 10);
assert_eq!(KIBI.prec(), u16::BITS);
sourcepub const fn const_from_u32(val: u32) -> Self
pub const fn const_from_u32(val: u32) -> Self
Creates a MiniFloat
from a u32
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const MEBI_MINI: MiniFloat = MiniFloat::const_from_u32(1u32 << 20);
const MEBI_BORROW: BorrowFloat = MEBI_MINI.borrow();
const MEBI: &Float = BorrowFloat::const_deref(&MEBI_BORROW);
assert_eq!(*MEBI, 1u32 << 20);
assert_eq!(MEBI.prec(), u32::BITS);
sourcepub const fn const_from_u64(val: u64) -> Self
pub const fn const_from_u64(val: u64) -> Self
Creates a MiniFloat
from a u64
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TEBI_MINI: MiniFloat = MiniFloat::const_from_u64(1u64 << 40);
const TEBI_BORROW: BorrowFloat = TEBI_MINI.borrow();
const TEBI: &Float = BorrowFloat::const_deref(&TEBI_BORROW);
assert_eq!(*TEBI, 1u64 << 40);
assert_eq!(TEBI.prec(), u64::BITS);
sourcepub const fn const_from_u128(val: u128) -> Self
pub const fn const_from_u128(val: u128) -> Self
Creates a MiniFloat
from a u128
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const YOBI_MINI: MiniFloat = MiniFloat::const_from_u128(1u128 << 80);
const YOBI_BORROW: BorrowFloat = YOBI_MINI.borrow();
const YOBI: &Float = BorrowFloat::const_deref(&YOBI_BORROW);
assert_eq!(*YOBI, 1u128 << 80);
assert_eq!(YOBI.prec(), u128::BITS);
sourcepub const fn const_from_usize(val: usize) -> Self
pub const fn const_from_usize(val: usize) -> Self
Creates a MiniFloat
from a usize
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const KIBI_MINI: MiniFloat = MiniFloat::const_from_usize(1usize << 10);
const KIBI_BORROW: BorrowFloat = KIBI_MINI.borrow();
const KIBI: &Float = BorrowFloat::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1usize << 10);
assert_eq!(KIBI.prec(), usize::BITS);
sourcepub const fn const_from_f32(val: f32) -> Self
pub const fn const_from_f32(val: f32) -> Self
Creates a MiniFloat
from an f32
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TWO_HALF_MINI: MiniFloat = MiniFloat::const_from_f32(2.5);
const TWO_HALF_BORROW: BorrowFloat = TWO_HALF_MINI.borrow();
const TWO_HALF: &Float = BorrowFloat::const_deref(&TWO_HALF_BORROW);
assert_eq!(*TWO_HALF, 2.5);
assert_eq!(TWO_HALF.prec(), f32::MANTISSA_DIGITS);
sourcepub const fn const_from_f64(val: f64) -> Self
pub const fn const_from_f64(val: f64) -> Self
Creates a MiniFloat
from an f64
.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float::{BorrowFloat, MiniFloat};
use rug::Float;
const TWO_HALF_MINI: MiniFloat = MiniFloat::const_from_f64(2.5);
const TWO_HALF_BORROW: BorrowFloat = TWO_HALF_MINI.borrow();
const TWO_HALF: &Float = BorrowFloat::const_deref(&TWO_HALF_BORROW);
assert_eq!(*TWO_HALF, 2.5);
assert_eq!(TWO_HALF.prec(), f64::MANTISSA_DIGITS);
sourcepub const fn const_from_special(val: Special) -> Self
pub const fn const_from_special(val: Special) -> Self
Creates a MiniFloat
from a Special
value.
This is equivalent to MiniFloat::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::float;
use rug::float::{BorrowFloat, MiniFloat, Special};
use rug::Float;
const INF_MINI: MiniFloat = MiniFloat::const_from_special(Special::Infinity);
const INF_BORROW: BorrowFloat = INF_MINI.borrow();
const INF: &Float = BorrowFloat::const_deref(&INF_BORROW);
assert!(INF.is_infinite());
assert_eq!(INF.prec(), float::prec_min());
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 const fn borrow(&self) -> BorrowFloat<'_>
pub const fn borrow(&self) -> BorrowFloat<'_>
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 Assign<MiniFloat> for MiniComplex
impl Assign<MiniFloat> for MiniComplex
source§impl AssignRound<&MiniFloat> for Float
impl AssignRound<&MiniFloat> for Float
source§impl AssignRound<MiniFloat> for Float
impl AssignRound<MiniFloat> for Float
source§impl From<MiniFloat> for MiniComplex
impl From<MiniFloat> for MiniComplex
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