Struct Value

Source
pub struct Value {
    pub mantissa: f64,
    pub prefix: Prefix,
    pub base: Base,
}
Expand description

Defines the representation of the value.

Fields§

§mantissa: f64

Mantissa of the value after scaling.

§prefix: Prefix

Prefix indicating the scale.

§base: Base

Indicates if the base is 1000 or 1024.

Implementations§

Source§

impl Value

Source

pub fn new<F>(x: F) -> Self
where F: Into<f64>,

Returns a Value for the default base B1000, meaning 1 k = 1000, 1 m = 1e-3, etc.

§Example
use si_scale::prelude::{Base, Prefix, Value};

let actual = Value::new(-4.6e-5);
let expected = Value {
    mantissa: -46f64,
    prefix: Prefix::Micro,
    base: Base::B1000,
};
assert_eq!(actual, expected);
§Note

As always the case in floating point operations, you may encounter approximate representations. For instance:

use si_scale::prelude::{Base, Prefix, Value};

let actual = Value::new(-4.3e-5);
let expected = Value {
    mantissa: -43.00000000000001f64,
    prefix: Prefix::Micro,
    base: Base::B1000,
};
assert_eq!(actual, expected);
Source

pub fn new_with<F, C>(x: F, base: Base, prefix_constraint: C) -> Self
where F: Into<f64>, C: AsRef<Constraint>,

Returns a Value for the provided base.

§Example
use si_scale::prelude::{Constraint, Base, Prefix, Value};

// 4 MiB
let actual = Value::new_with(4 * 1024 * 1024, Base::B1024, Constraint::None);
let expected = Value {
    mantissa: 4f64,
    prefix: Prefix::Mega,
    base: Base::B1024,
};
assert_eq!(actual, expected);
Source

pub fn to_f64(&self) -> f64

Converts self to a f64.

§Example
use si_scale::prelude::{Base, Prefix, Value};

let value = Value {
    mantissa: 1.3f64,
    prefix: Prefix::Unit,
    base: Base::B1000,
};
assert_eq!(value.to_f64(), 1.3);
Source

pub fn signum(&self) -> f64

Returns a number that represents the sign of self.

  • 1.0 if the number is positive, +0.0 or INFINITY
  • -1.0 if the number is negative, -0.0 or NEG_INFINITY
  • NAN if the number is NAN
§Example
use std::convert::From;
use si_scale::value::Value;

let number = -1.5e3f32;
let value: Value = number.into();

assert_eq!(value.signum(), number.signum() as f64);

Trait Implementations§

Source§

impl Debug for Value

Source§

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

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

impl Display for Value

Source§

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

A basic but limited way to display the value; it does not allow mantissa formatting. Consider using the format_value!() macro instead.

§Example
use std::convert::From;
use si_scale::prelude::Value;

let value: Value = 5.3e5.into();

let actual = format!("{}", value);
let expected = "530 k".to_string();
assert_eq!(actual, expected);
Source§

impl From<&f32> for Value

Source§

fn from(x: &f32) -> Self

Converts to this type from the input type.
Source§

impl From<&f64> for Value

Source§

fn from(x: &f64) -> Self

Converts to this type from the input type.
Source§

impl From<&i16> for Value

Source§

fn from(x: &i16) -> Self

Converts to this type from the input type.
Source§

impl From<&i32> for Value

Source§

fn from(x: &i32) -> Self

Converts to this type from the input type.
Source§

impl From<&i8> for Value

Source§

fn from(x: &i8) -> Self

Converts to this type from the input type.
Source§

impl From<&u16> for Value

Source§

fn from(x: &u16) -> Self

Converts to this type from the input type.
Source§

impl From<&u32> for Value

Source§

fn from(x: &u32) -> Self

Converts to this type from the input type.
Source§

impl From<&u8> for Value

Source§

fn from(x: &u8) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for f64

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(x: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(x: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(x: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(x: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(x: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(x: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(x: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(x: u8) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> 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 StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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> ToString for T
where T: Display + ?Sized,

Source§

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

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.