Enum Base

Source
pub enum Base {
    B1000,
    B1024,
}
Expand description

Represents the base for units Prefix.

Variants§

§

B1000

The most common base, where 1 k means 1000, 1 M means 1000^2, …

§

B1024

A very common base for bibytes, where 1 kiB means 1024, 1 MiB means 1024 * 1024, …

Implementations§

Source§

impl Base

Source

pub fn integral_exponent_for<F>(&self, x: F) -> i32
where F: Into<f64>,

Using floor(), returns the closest integer exponent to represent the provided value x in the self Base.

The returned integer exponent is a multiple of 3 in order to match the prefixes’ exponents.

§Example
use si_scale::base::Base;

let x: f32  = 5.4e4;
let actual = Base::B1000.integral_exponent_for(x);
assert_eq!(actual, 3);  // 1e3

let x: f64  = -5.4e-4;
let actual = Base::B1000.integral_exponent_for(x);
assert_eq!(actual, -6);  // 1e-6
Source

pub fn pow(&self, exponent: i32) -> f64

This helper function returns a f64 scaling factor for the mantissa, obtained by raising self to the power of the provided exponent divided by 3.

§Example
use si_scale::base::Base;

assert_eq!(Base::B1000.pow(9), 1e9);
assert_eq!(Base::B1024.pow(3), 1024f64)

Trait Implementations§

Source§

impl Debug for Base

Source§

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

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

impl PartialEq for Base

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Base

Source§

impl StructuralPartialEq for Base

Auto Trait Implementations§

§

impl Freeze for Base

§

impl RefUnwindSafe for Base

§

impl Send for Base

§

impl Sync for Base

§

impl Unpin for Base

§

impl UnwindSafe for Base

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> 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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.