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
//---------------------------------------------------------------------------------------------------- Use
use compact_str::{format_compact,CompactString};
use crate::num::{
Unsigned,Int,
constants::{
NAN,INFINITY,
},
};
use crate::macros::{
return_bad_float,str_u64,str_i64,
impl_common,impl_not_const,
impl_usize,impl_isize,
impl_math,impl_traits,
impl_impl_math,
};
//---------------------------------------------------------------------------------------------------- Float
/// Human readable float.
///
/// Takes a floating point number as input and returns a ready-to-[`print!()`] [`Float`].
///
/// The fractional floating point may or may not be rounded up/down in the [`String`].
///
/// The default [`Float::from`] implementation will print `3` decimal numbers.
///
/// This can be changed by using different functions when initially
/// creating the [`Float`], or converting an existing [`Float`], for example:
/// ```
/// # use readable::Float;
/// let f2 = Float::from_2(3.0);
/// let f6 = Float::from_6(3.0);
/// let f9 = Float::from_9(f2.inner());
///
/// assert!(f2 == 3.00);
/// assert!(f6 == 3.000000);
/// assert!(f9 == 3.000000000);
///```
///
/// ## Warning
/// This type (and this library in general) is meant for fast and
/// simple data formatting, and not necessarily correctness.
///
/// [`Float`] internally converts to a `u64` to add commas and as such
/// the maximum input values for [`Float`] before it starts becoming
/// inaccurate is somewhere right before [`u64::MAX`].
///
/// Formatting [`Float`] is also quite slower than [`Unsigned`] and [`Int`].
///
/// ## Size
/// This type may or may not be heap allocated.
///
/// ```rust
/// # use readable::*;
/// assert_eq!(std::mem::size_of::<Float>(), 32);
/// ```
///
/// ## Cloning
/// [`Clone`] may be a heap allocation clone:
/// ```rust
/// # use readable::Float;
/// // Stack allocated string.
/// let a = Float::from(100.0);
/// let b = a.clone();
///
/// // Heap allocated string.
/// let a = Float::from(f64::MAX);
/// let b = a.clone();
/// ```
///
/// The actual string used internally is not a [`String`](https://doc.rust-lang.org/std/string/struct.String.html),
/// but a [`CompactString`](https://docs.rs/compact_str) so that any string 24 bytes (12 bytes on 32-bit) or less are _stack_ allocated instead of _heap_ allocated.
///
/// The documentation will still refer to the inner string as a `String`. Anything returned will also be a `String`.
///
/// ## Float Errors
/// Inputting [`f64::NAN`], [`f64::INFINITY`], [`f64::NEG_INFINITY`] or the [`f32`] variants returns errors
///
/// ## Math
/// These operators are overloaded. They will always output a new [`Self`]:
/// - `Add +`
/// - `Sub -`
/// - `Div /`
/// - `Mul *`
/// - `Rem %`
///
/// They can either be:
/// - Combined with another [`Self`]: `Float::from(1.0) + Float::from(1.0)`
/// - Or with the inner number itself: `Float::from(1.0) + 1.0`
///
/// ```rust
/// # use readable::*;
/// // Regular operators.
/// assert!(Float::from(10.0) + 10.0 == Float::from(20.0));
/// assert!(Float::from(10.0) - 10.0 == Float::from(0.0));
/// assert!(Float::from(10.0) / 10.0 == Float::from(1.0));
/// assert!(Float::from(10.0) * 10.0 == Float::from(100.0));
/// assert!(Float::from(10.0) % 10.0 == Float::from(0.0));
/// ```
///
/// # Examples
/// ```rust
/// # use readable::Float;
/// assert_eq!(Float::from(0.0), "0.000");
///
/// // This gets rounded up to '.568'
/// assert_eq!(Float::from(1234.5678), "1,234.568");
/// // To prevent that, use 4 point.
/// assert_eq!(Float::from_4(1234.5678), "1,234.5678");
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Float(f64, #[cfg_attr(feature = "bincode", bincode(with_serde))] CompactString);
impl_math!(Float, f64);
impl_traits!(Float, f64);
//---------------------------------------------------------------------------------------------------- Float Constants
impl Float {
/// ```rust
/// # use readable::num::*;
/// assert_eq!(Float::ZERO, 0.0);
/// assert_eq!(Float::ZERO, "0.000");
/// ```
pub const ZERO: Self = Self(0.0, CompactString::new_inline("0.000"));
/// ```rust
/// # use readable::num::*;
/// assert_eq!(Float::NAN, "NaN");
/// assert!(Float::NAN.is_nan());
/// ```
pub const NAN: Self = Self(f64::NAN, CompactString::new_inline(NAN));
/// ```rust
/// # use readable::num::*;
/// assert_eq!(Float::INFINITY, "inf");
/// assert!(Float::INFINITY.is_infinite());
/// ```
pub const INFINITY: Self = Self(f64::INFINITY, CompactString::new_inline(INFINITY));
/// ```rust
/// # use readable::num::*;
/// assert_eq!(Float::UNKNOWN, 0.0);
/// assert_eq!(Float::UNKNOWN, "?.???");
/// ```
pub const UNKNOWN: Self = Self(0.0, CompactString::new_inline("?.???"));
}
//---------------------------------------------------------------------------------------------------- Float Impl
// Implements `from_X` functions.
macro_rules! impl_new {
( $num:tt ) => {
paste::item! {
#[doc = "Same as [`Float::from`] but with `" $num "` floating point."]
pub fn [<from_ $num>](f: f64) -> Self {
return_bad_float!(f, Self::nan, Self::infinity);
let fract = &format_compact!(concat!("{:.", $num, "}"), f.fract())[2..];
Self(f, format_compact!("{}.{}", str_u64!(f as u64), fract))
}
}
}
}
impl Float {
impl_common!(f64);
impl_not_const!();
impl_usize!();
impl_isize!();
#[inline]
/// Returns [`Self::ZERO`]
pub const fn zero() -> Self {
Self::ZERO
}
#[inline]
/// Returns [`Self::NAN`]
pub const fn nan() -> Self {
Self::NAN
}
#[inline]
/// Returns [`Self::INFINITY`]
pub const fn infinity() -> Self {
Self::INFINITY
}
#[inline]
/// Calls [`f64::is_nan`].
pub fn is_nan(&self) -> bool {
self.0.is_nan()
}
#[inline]
/// Calls [`f64::is_infinite`].
pub fn is_infinite(&self) -> bool {
self.0.is_infinite()
}
#[inline]
/// Same as [`Float::from`] but with no floating point on the inner [`String`].
///
/// The inner [`f64`] stays the same as the input.
///
/// This does not round _up_ or _down_, it completely ignores the floating point.
///
/// ## Examples
/// | Input | String Output |
/// |--------|---------------|
/// | 0.0 | `0`
/// | 50.123 | `50`
/// | 100.1 | `100`
pub fn from_0(f: f64) -> Self {
return_bad_float!(f, Self::nan, Self::infinity);
Self(f, CompactString::from(str_u64!(f as u64)))
}
seq_macro::seq!(N in 1..=14 {
impl_new!(N);
});
}
//---------------------------------------------------------------------------------------------------- From `u*`
// Implementation Macro.
macro_rules! impl_u {
($( $number:ty ),*) => {
$(
impl From<$number> for Float {
#[inline]
fn from(number: $number) -> Self {
Self(number as f64, format_compact!("{}.000", str_u64!(number as u64)))
}
}
)*
}
}
impl_u!(u8,u16,u32,u64,usize);
//---------------------------------------------------------------------------------------------------- From `i*`
macro_rules! impl_i {
($($number:ty),*) => {
$(
impl From<$number> for Float {
#[inline]
fn from(number: $number) -> Self {
Self(number as f64, format_compact!("{}.000", str_i64!(number as i64)))
}
}
)*
}
}
impl_i!(i8,i16,i32,i64,isize);
//---------------------------------------------------------------------------------------------------- From `f32/f64`
impl From<f32> for Float {
#[inline]
fn from(f: f32) -> Self {
return_bad_float!(f, Self::nan, Self::infinity);
Self::from(f as f64)
}
}
impl From<f64> for Float {
#[inline]
fn from(f: f64) -> Self {
return_bad_float!(f, Self::nan, Self::infinity);
let fract = &format_compact!("{:.3}", f.fract())[2..];
Self(f, format_compact!("{}.{}", str_u64!(f as u64), fract))
}
}
//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn special() {
assert_eq!(Float::from(0.0), "0.000");
assert_eq!(Float::zero(), "0.000");
assert_eq!(Float::nan(), NAN);
assert_eq!(Float::infinity(), INFINITY);
assert_eq!(Float::from(f64::NAN), NAN);
assert_eq!(Float::from(f64::INFINITY), INFINITY);
assert_eq!(Float::from(f64::NEG_INFINITY), INFINITY);
assert_eq!(Float::from(f32::NAN), NAN);
assert_eq!(Float::from(f32::INFINITY), INFINITY);
assert_eq!(Float::from(f32::NEG_INFINITY), INFINITY);
}
#[test]
fn float() {
assert_eq!(Float::from_0(0.1), "0");
assert_eq!(Float::from_1(0.1), "0.1");
assert_eq!(Float::from_2(0.01), "0.01");
assert_eq!(Float::from(0.001), "0.001");
assert_eq!(Float::from_4(0.0001), "0.0001");
assert_eq!(Float::from_5(0.00001), "0.00001");
assert_eq!(Float::from_6(0.000001), "0.000001");
assert_eq!(Float::from_7(0.0000001), "0.0000001");
assert_eq!(Float::from_8(0.00000001), "0.00000001");
assert_eq!(Float::from_9(0.000000001), "0.000000001");
assert_eq!(Float::from_10(0.0000000001), "0.0000000001");
assert_eq!(Float::from_11(0.00000000001), "0.00000000001");
assert_eq!(Float::from_12(0.000000000001), "0.000000000001");
assert_eq!(Float::from_13(0.0000000000001), "0.0000000000001");
assert_eq!(Float::from_14(0.00000000000001), "0.00000000000001");
}
}