Enum si_scale::base::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

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 Eq for Base

source§

impl StructuralEq for Base

source§

impl StructuralPartialEq for Base

Auto Trait Implementations§

§

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

§

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.