pub struct Voltage { /* private fields */ }
Expand description
Represents a voltage value, stored as whole microvolts (μV). This value can be positive or negative.
Reminder: 1000 μV = 1 mV, 1000 mV = 1 V, 1000 V = 1k V
This is an immutable type. Any math operators return a new Voltage
value.
Creating a Voltage value
You can create a Voltage
value using the from_micro_volts
method, or using one of the
extension methods on i32
and f32
:
use ohms::prelude::*;
let v1 = Voltage::from_micro_volts(325); // 325μV
// More ergonomic:
let v2 = 900.milli_volts(); // 900mV
let v3 = 12.volts(); // 12V
let v4 = 3.3f32.volts(); // 3.3V
Comparing Voltage values
You can compare two Voltage
values using the ==
, !=
, <
, >
, <=
and >=
operators.
use ohms::prelude::*;
let v1 = 3.3f32.volts(); // 3.3V
let v2 = 5.2f32.volts(); // 5.2V
if v1 > v2 {
println!("{:?} is greater than {:?}", v1, v2);
} else {
println!("{:?} is less than or equal to {:?}", v1, v2);
}
Combining Voltage values
You can use the +
and -
operators to add and subtract Voltage
values from each other.
The result is a new Voltage
value, rounded down to the nearest whole microvolt (μV).
If the result of the operation would overflow or underflow the i32
value, the operation will panic.
use ohms::prelude::*;
let v1 = 3.7f32.volts(); // 3.7V
let v2 = 9.volts(); // 9V
let v3 = v1 + v2; // 12.7V
let v4 = v2 - 6.volts(); // 3V
Scaling Voltage values
You can use the *
and /
operators to scale Voltage
values by a scalar i32
or f32
value.
The result is a new Voltage
value, rounded down to the nearest whole microvolt (μV).
If the result of operation would overflow or underflow the i32
value, the operation will panic.
If the result of the operation would be infinite or NaN, the operation will panic.
use ohms::prelude::*;
let v1 = 6.volts(); // 6V
let v2 = v1 * 2; // 12V
let v3 = 250.micro_volts(); // 250μV
let v4 = v3 / 2f32; // 125μV
Converting to other denominations
You can use the micro_volts
, milli_volts
, volts
, and kilo_volts
methods to convert a Voltage
value to a i32
or f32
value in the specified denomination.
use ohms::prelude::*;
let v1 = 3.3f32.volts(); // 3.3V
println!("{:.2}V is {:.1}mV", v1.volts(), v1.milli_volts());
Implementations§
source§impl Voltage
impl Voltage
sourcepub const fn from_micro_volts(microvolts: i32) -> Voltage
pub const fn from_micro_volts(microvolts: i32) -> Voltage
Creates a new Voltage
from a number of whole microvolts (μV).
It is recommended to use the micro_volts
, milli_volts
, volts
, and kilo_volts
extension
methods on i32
and f32
instead of this method for ergonomics.
sourcepub fn micro_volts(&self) -> i32
pub fn micro_volts(&self) -> i32
Returns the voltage value in whole microvolts (μV).
sourcepub fn milli_volts(&self) -> f32
pub fn milli_volts(&self) -> f32
Returns the voltage value in fractional millivolts (mV).
sourcepub fn kilo_volts(&self) -> f32
pub fn kilo_volts(&self) -> f32
Returns the voltage value in fractional kilovolts (kV).
sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns whether the voltage value is positive.
This returns true
if the voltage value is greater than or equal to zero volts (0V).
sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns whether the voltage value is negative.
This returns true
if the voltage value is less than zero volts (0V).
Trait Implementations§
source§impl Div<Current> for Voltage
impl Div<Current> for Voltage
source§fn div(self, current: Current) -> <Voltage as Div<Current>>::Output
fn div(self, current: Current) -> <Voltage as Div<Current>>::Output
Calculates the resistance of a resistive load given the voltage across it and the current.
Will be rounded down to the nearest whole milliohm (mΩ). Panics if the current is zero.
§type Output = Resistance
type Output = Resistance
/
operator.source§impl Div<Resistance> for Voltage
impl Div<Resistance> for Voltage
source§fn div(self, resistance: Resistance) -> <Voltage as Div<Resistance>>::Output
fn div(self, resistance: Resistance) -> <Voltage as Div<Resistance>>::Output
Calculates the current through a resistive load given the voltage across it.
Will be rounded down to the nearest whole microamp (μA). Panics if the resistance is zero.
source§impl Ord for Voltage
impl Ord for Voltage
source§impl PartialEq<Voltage> for Voltage
impl PartialEq<Voltage> for Voltage
source§impl PartialOrd<Voltage> for Voltage
impl PartialOrd<Voltage> for Voltage
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