engineering_repr/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
// (c) 2024 Ross Younger
#![doc = include_str!("../README.md")]
use std::cmp::Ordering;
use num_traits::{checked_pow, ConstZero, PrimInt};
mod string;
pub use string::{DisplayAdapter, EngineeringRepr};
mod serde_support;
/// Helper type for expressing numbers in engineering notation
///
/// # Type parameter
/// The type parameter `T` is the underlying storage type used for the significand of the number.
/// That is to say, an `EngineeringQuantity<u32>` uses a `u32` to store the numeric part.
#[derive(Debug, Clone, Copy, Default)]
pub struct EngineeringQuantity<T: EQSupported<T>> {
/// Significant bits
significand: T,
/// Engineering exponent i.e. powers of 1e3
exponent: i8,
}
/////////////////////////////////////////////////////////////////////////
// META (SUPPORTED STORAGE TYPES)
/// Marker trait indicating that a type is supported as a storage type for [`EngineeringQuantity`].
pub trait EQSupported<T: PrimInt>: PrimInt + std::fmt::Display + ConstZero + SignHelper<T> {
/// Always 1000 (used internally)
const EXPONENT_BASE: T;
}
macro_rules! supported_types {
{$($t:ty),+} => {$(
impl<> EQSupported<$t> for $t {
const EXPONENT_BASE: $t = 1000;
}
)+}
}
supported_types!(i16, i32, i64, i128, isize, u16, u32, u64, u128, usize);
/// Signedness helper data, used by string conversions
#[derive(Debug, Clone)]
pub struct AbsAndSign<T: PrimInt> {
abs: T,
negative: bool,
}
/// Signedness helper trait, used by string conversions.
/// This trait exists because `abs()` is, quite reasonably, only implemented
/// for types which are `num_traits::Signed`.
pub trait SignHelper<T: PrimInt> {
/// Unpacks a maybe-signed integer into its absolute value and sign bit
fn abs_and_sign(&self) -> AbsAndSign<T>;
}
macro_rules! impl_unsigned_helpers {
{$($t:ty),+} => {$(
impl<> SignHelper<$t> for $t {
fn abs_and_sign(&self) -> AbsAndSign<$t> {
AbsAndSign { abs: *self, negative: false }
}
}
)+}
}
macro_rules! impl_signed_helpers {
{$($t:ty),+} => {$(
impl<> SignHelper<$t> for $t {
fn abs_and_sign(&self) -> AbsAndSign<$t> {
AbsAndSign { abs: self.abs(), negative: self.is_negative() }
}
}
)+}
}
impl_unsigned_helpers!(u16, u32, u64, u128, usize);
impl_signed_helpers!(i16, i32, i64, i128, isize);
/////////////////////////////////////////////////////////////////////////
// BASICS
// Constructors & accessors
impl<T: EQSupported<T>> EngineeringQuantity<T> {
/// Raw constructor from component parts
#[must_use]
pub fn from_raw(significand: T, exponent: i8) -> Self {
Self {
significand,
exponent,
}
}
/// Raw accessor to retrieve the component parts
#[must_use]
pub fn to_raw(self) -> (T, i8) {
(self.significand, self.exponent)
}
}
// Comparisons
impl<T: EQSupported<T> + TryFrom<EngineeringQuantity<T>>> PartialEq for EngineeringQuantity<T> {
/// ```
/// use engineering_repr::EngineeringQuantity as EQ;
/// let q1 = EQ::from_raw(42u32,0);
/// let q2 = EQ::from_raw(42u32,0);
/// assert_eq!(q1, q2);
/// let q3 = EQ::from_raw(42,1);
/// let q4 = EQ::from_raw(42000,0);
/// assert_eq!(q3, q4);
/// ```
fn eq(&self, other: &Self) -> bool {
// Easy case first
if self.exponent == other.exponent {
return self.significand == other.significand;
}
let cmp = self.partial_cmp(other);
matches!(cmp, Some(Ordering::Equal))
}
}
impl<T: EQSupported<T> + TryFrom<EngineeringQuantity<T>>> PartialOrd for EngineeringQuantity<T> {
/// ```
/// use engineering_repr::EngineeringQuantity as EQ;
/// use more_asserts::assert_lt;
/// let q2 = EQ::from_raw(41999,0);
/// let q3 = EQ::from_raw(42,1);
/// let q4 = EQ::from_raw(42001,0);
/// assert_lt!(q2, q3);
/// assert_lt!(q3, q4);
/// ```
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
let v1 = T::try_from(*self);
let v2 = T::try_from(*other);
match (v1, v2) {
(Ok(vv1), Ok(vv2)) => Some(vv1.cmp(&vv2)),
_ => None,
}
}
}
// Type conversions
impl<T: EQSupported<T>> EngineeringQuantity<T> {
/// Conversion to a different storage type.
/// If you can convert from type A to type B,
/// then you can convert from `EngineeringQuantity<A>` to `EngineeringQuantity<B>`.
/// ```
/// use engineering_repr::EngineeringQuantity as EQ;
/// let q = EQ::from_raw(42u32, 0);
/// let q2 = q.convert::<u64>();
/// assert_eq!(q2.to_raw(), (42u64, 0));
/// ```
pub fn convert<U: EQSupported<U> + From<T>>(&self) -> EngineeringQuantity<U> {
let (sig, exp) = self.to_raw();
EngineeringQuantity::<U>::from_raw(sig.into(), exp)
}
/// Fallible conversion to a different storage type.
///
/// Note that conversion only fails if the significand doesn't fit into the destination storage type,
/// without reference to the exponent. This means that two numbers, which might be equal, may not both
/// be convertible to the same destination type if they are not normalised. For example:
/// ```
/// use engineering_repr::EngineeringQuantity as EQ;
/// let million1 = EQ::from_raw(1, 2); // 1e6
/// let million2 = EQ::from_raw(1_000_000, 0);
/// assert_eq!(million1, million2);
/// let r1 = million1.try_convert::<u16>().unwrap(); // OK, because stored as (1,2)
/// let r2 = million2.try_convert::<u16>().expect_err("overflow"); // Overflow, because 1_000_000 won't fit into a u16
/// ```
pub fn try_convert<U: EQSupported<U> + TryFrom<T>>(
&self,
) -> Result<EngineeringQuantity<U>, <U as std::convert::TryFrom<T>>::Error> {
let (sig, exp) = self.to_raw();
Ok(EngineeringQuantity::<U>::from_raw(sig.try_into()?, exp))
}
}
impl<T: EQSupported<T>> EngineeringQuantity<T> {
/// Scales the number to remove any unnecessary groups of trailing zeroes.
#[must_use]
pub fn normalise(self) -> Self {
let mut working = self;
loop {
let (div, rem) = (
working.significand / T::EXPONENT_BASE,
working.significand % T::EXPONENT_BASE,
);
if rem != T::ZERO {
break;
}
working.significand = div;
working.exponent += 1;
}
working
}
}
/////////////////////////////////////////////////////////////////////////
// CONVERSION FROM INTEGER
impl<T: EQSupported<T>, U: EQSupported<U>> From<T> for EngineeringQuantity<U>
where
U: From<T>,
{
/// Integers can always be promoted on conversion to [`EngineeringQuantity`].
/// (For demotions, you have to convert the primitive yourself and handle any failures.)
/// ```
/// let i = 42u32;
/// let _e = engineering_repr::EngineeringQuantity::<u64>::from(i);
/// ```
fn from(value: T) -> Self {
Self {
significand: value.into(),
exponent: 0,
}
}
}
/////////////////////////////////////////////////////////////////////////
// CONVERSION TO INTEGER
macro_rules! impl_try_from {
{$($t:ty),+} => {$(
impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for $t
where $t: TryFrom<U>,
{
type Error = crate::Error;
#[doc = concat!("\
Conversion to integer is always fallible, as the exponent might cause us to under or overflow.
```
use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(", stringify!($t), "::try_from(i).unwrap(), 11000);
```
")]
fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error> {
// TODO: This conversion fails on negative exponents
let exp: usize = eq.exponent.try_into().map_err(|_| Error::Underflow)?;
let Some(factor) = checked_pow(U::EXPONENT_BASE, exp) else {
return Err(Error::Overflow);
};
let result: U = factor * eq.significand;
std::convert::TryInto::<$t>::try_into(result).map_err(|_| Error::Overflow)
}
}
)+}
}
impl_try_from!(u16, u32, u64, u128, usize, i16, i32, i64, i128, isize);
/////////////////////////////////////////////////////////////////////////
// ERRORS
/// Local error type returned by failing conversions
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Error {
/// Numeric overflow
Overflow,
/// Numeric underflow
Underflow,
/// The input string could not be parsed
ParseError,
}
/////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod test {
use super::EngineeringQuantity as EQ;
use super::Error as EQErr;
#[test]
fn integers() {
for i in &[1i64, -1, 100, -100, 1000, 4000, -4000, 4_000_000] {
let ee = EQ::from_raw(*i, 0);
assert_eq!(i64::try_from(ee).unwrap(), *i);
let ee2 = EQ::from_raw(*i, 1);
assert_eq!(i64::try_from(ee2).unwrap(), *i * 1000, "input is {}", *i);
}
}
#[test]
fn equality() {
for (a, b, c, d) in &[
(1i64, 0, 1i64, 0),
(1, 1, 1000, 0),
(2000, 0, 2, 1),
(123_000_000, 0, 123_000, 1),
(123_000_000, 0, 123, 2),
(456_000_000_000_000, 0, 456_000, 3),
(456_000_000_000_000, 0, 456, 4),
] {
let e1 = EQ::from_raw(*a, *b);
let e2 = EQ::from_raw(*c, *d);
assert_eq!(e1, e2);
}
}
#[test]
fn conversion() {
let t = EQ::<u32>::from_raw(12345, 0);
let u = t.convert::<u64>();
assert_eq!(u.to_raw().0, <u32 as Into<u64>>::into(t.to_raw().0));
assert_eq!(t.to_raw().1, u.to_raw().1);
}
#[test]
fn overflow() {
let t = EQ::<u32>::from_raw(100_000, 0);
let _ = t.try_convert::<u16>().expect_err("TryFromIntError");
assert_eq!(u16::try_from(t), Err(EQErr::Overflow));
// 10^15 is too big for a u32, so will overflow on conversion to integer:
let t = EQ::<u32>::from_raw(1, 5);
assert_eq!(u64::try_from(t), Err(EQErr::Overflow));
}
#[test]
fn underflow() {
let t = EQ::<u32>::from_raw(1, -1);
assert_eq!(u32::try_from(t), Err(EQErr::Underflow));
}
#[test]
fn normalise() {
let q = EQ::from_raw(1_000_000, 0);
let q2 = q.normalise();
assert_eq!(q, q2);
assert_eq!(q2.to_raw(), (1, 2));
}
}