pub struct NiceFloat(/* private fields */);Expand description
Implementations§
Source§impl NiceFloat
impl NiceFloat
Sourcepub const INFINITY: Self
pub const INFINITY: Self
§Infinity.
A value representing infinity. Note that no distinction is made between positive and negative varieties.
§Examples
use dactyl::NiceFloat;
assert_eq!(NiceFloat::INFINITY.as_str(), "∞");
assert_eq!(NiceFloat::from(f64::INFINITY).as_str(), "∞");
assert_eq!(NiceFloat::from(f64::NEG_INFINITY).as_str(), "∞");Sourcepub const fn overflow(neg: bool, sep: NiceSeparator) -> Self
pub const fn overflow(neg: bool, sep: NiceSeparator) -> Self
§Overflow.
This is used for values with integer components that do not fit within
the u64 range.
§Examples
use dactyl::{NiceFloat, NiceSeparator};
assert_eq!(
NiceFloat::overflow(false, NiceSeparator::Comma).as_str(),
"> 18,446,744,073,709,551,615",
);
assert_eq!(
NiceFloat::overflow(true, NiceSeparator::Comma).as_str(),
"< -18,446,744,073,709,551,615",
);
// The same, triggered manually.
assert_eq!(
NiceFloat::from(f64::MAX).as_str(),
"> 18,446,744,073,709,551,615",
);
assert_eq!(
NiceFloat::from(-f64::MAX).as_str(),
"< -18,446,744,073,709,551,615",
);Source§impl NiceFloat
impl NiceFloat
Source§impl NiceFloat
impl NiceFloat
Sourcepub const fn compact_bytes(&self) -> &[u8] ⓘ
pub const fn compact_bytes(&self) -> &[u8] ⓘ
§Compact Bytes.
This returns a byte slice without trailing decimal zeroes. If the value has no fractional component at all, it will just return the integer portion.
§Examples
use dactyl::NiceFloat;
let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.as_bytes(), b"12,345.67800000");
assert_eq!(nice.compact_bytes(), b"12,345.678"); // Some zeroes to trim.
let nice = NiceFloat::from(12340.0);
assert_eq!(nice.as_bytes(), b"12,340.00000000");
assert_eq!(nice.compact_bytes(), b"12,340"); // No fraction at all.
let nice = NiceFloat::from(12345.6783333333_f64);
assert_eq!(nice.as_bytes(), b"12,345.67833333");
assert_eq!(nice.compact_bytes(), b"12,345.67833333"); // Nothing to trim.Sourcepub const fn compact_str(&self) -> &str
pub const fn compact_str(&self) -> &str
§Compact String.
This returns a string slice without trailing decimal zeroes. If the value has no fractional component at all, it will just return the integer portion.
§Examples
use dactyl::NiceFloat;
let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.as_str(), "12,345.67800000");
assert_eq!(nice.compact_str(), "12,345.678"); // Some zeroes to trim.
let nice = NiceFloat::from(12345.0);
assert_eq!(nice.as_str(), "12,345.00000000");
assert_eq!(nice.compact_str(), "12,345"); // No fraction at all.
let nice = NiceFloat::from(12345.6783333333_f64);
assert_eq!(nice.as_str(), "12,345.67833333");
assert_eq!(nice.compact_str(), "12,345.67833333"); // Nothing to trim.Sourcepub const fn precise_bytes(&self, precision: usize) -> &[u8] ⓘ
pub const fn precise_bytes(&self, precision: usize) -> &[u8] ⓘ
§Precise Bytes.
This truncates the fractional part to the desired number of places, and returns the corresponding byte slice.
If the precision is zero, only the integer portion will be returned.
Precisions >= 8 are meaningless, and return the equivalent of
NiceFloat::as_bytes.
§Examples
use dactyl::NiceFloat;
let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.precise_bytes(0), b"12,345");
assert_eq!(nice.precise_bytes(1), b"12,345.6");
assert_eq!(nice.precise_bytes(2), b"12,345.67");
assert_eq!(nice.precise_bytes(3), b"12,345.678");
assert_eq!(nice.precise_bytes(4), b"12,345.6780");
assert_eq!(nice.precise_bytes(5), b"12,345.67800");
assert_eq!(nice.precise_bytes(6), b"12,345.678000");
assert_eq!(nice.precise_bytes(7), b"12,345.6780000");
assert_eq!(nice.precise_bytes(8), b"12,345.67800000");
// This has no effect on weird floats.
assert_eq!(NiceFloat::NAN.precise_bytes(8), b"NaN");
assert_eq!(NiceFloat::INFINITY.precise_bytes(8), "∞".as_bytes());Sourcepub const fn precise_str(&self, precision: usize) -> &str
pub const fn precise_str(&self, precision: usize) -> &str
§Precise String.
This truncates the fractional part to the desired number of places, and returns the corresponding string slice.
If the precision is zero, only the integer portion will be returned.
Precisions >= 8 are meaningless, and return the equivalent of
NiceFloat::as_str.
§Examples
use dactyl::NiceFloat;
let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.precise_str(0), "12,345");
assert_eq!(nice.precise_str(1), "12,345.6");
assert_eq!(nice.precise_str(2), "12,345.67");
assert_eq!(nice.precise_str(3), "12,345.678");
assert_eq!(nice.precise_str(4), "12,345.6780");
assert_eq!(nice.precise_str(5), "12,345.67800");
assert_eq!(nice.precise_str(6), "12,345.678000");
assert_eq!(nice.precise_str(7), "12,345.6780000");
assert_eq!(nice.precise_str(8), "12,345.67800000");
// This has no effect on weird floats.
assert_eq!(NiceFloat::NAN.precise_str(8), "NaN");
assert_eq!(NiceFloat::INFINITY.precise_str(8), "∞");Source§impl NiceFloat
impl NiceFloat
Sourcepub fn with_separator(num: f64, sep: NiceSeparator, dot: NiceSeparator) -> Self
pub fn with_separator(num: f64, sep: NiceSeparator, dot: NiceSeparator) -> Self
§New Instance w/ Custom Separator.
Create a new instance, defining any arbitrary ASCII byte as the thousands separator, and another for the decimal point.
If you’re good with American commas/periods, just use
NiceFloat::from instead; it’s faster.
§Examples
use dactyl::{NiceFloat, NiceSeparator};
// The default is commas for thousands, a period for top and bottom.
assert_eq!(
NiceFloat::from(1234.5678_f64).as_str(),
"1,234.56780000",
);
// Some places prefer the opposite.
assert_eq!(
NiceFloat::with_separator(
1234.5678_f64,
NiceSeparator::Period,
NiceSeparator::Comma,
).as_str(),
"1.234,56780000",
);
// The punctuation is also honored for "special" values:
assert_eq!(
NiceFloat::with_separator(
0_f64,
NiceSeparator::Comma,
NiceSeparator::Space,
).as_str(),
"0 00000000",
);
assert_eq!(
NiceFloat::with_separator(
f64::MAX,
NiceSeparator::Underscore,
NiceSeparator::Period,
).as_str(),
"> 18_446_744_073_709_551_615",
);Source§impl NiceFloat
impl NiceFloat
Sourcepub const fn div_u8(e: u8, d: u8) -> Result<f64, f64>
pub const fn div_u8(e: u8, d: u8) -> Result<f64, f64>
§Divide Two u8 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_u8(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_u8(13, 13),
Ok(1.0),
"testing 13 / 13div_u8"
);
assert_eq!(
NiceFloat::div_u8(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_u8(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_u8(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_u8(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_u8(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_u8(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_u16(e: u16, d: u16) -> Result<f64, f64>
pub const fn div_u16(e: u16, d: u16) -> Result<f64, f64>
§Divide Two u16 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_u16(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_u16(13, 13),
Ok(1.0),
"testing 13 / 13div_u16"
);
assert_eq!(
NiceFloat::div_u16(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_u16(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_u16(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_u16(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_u16(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_u16(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_u32(e: u32, d: u32) -> Result<f64, f64>
pub const fn div_u32(e: u32, d: u32) -> Result<f64, f64>
§Divide Two u32 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_u32(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_u32(13, 13),
Ok(1.0),
"testing 13 / 13div_u32"
);
assert_eq!(
NiceFloat::div_u32(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_u32(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_u32(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_u32(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_u32(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_u32(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_u64(e: u64, d: u64) -> Result<f64, f64>
pub const fn div_u64(e: u64, d: u64) -> Result<f64, f64>
§Divide Two u64 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_u64(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_u64(13, 13),
Ok(1.0),
"testing 13 / 13div_u64"
);
assert_eq!(
NiceFloat::div_u64(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_u64(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_u64(5, 0).is_err_and(|e| e.is_infinite()),
);Integers with more than 15-16 digits — as can happen with u64 —
can also be problematic.
assert_eq!(
NiceFloat::div_u64(9_223_372_036_854_775_806, 1),
Err(9_223_372_036_854_776_000.0), // Almost!
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_u64(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_u64(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_u64(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_u128(e: u128, d: u128) -> Result<f64, f64>
pub const fn div_u128(e: u128, d: u128) -> Result<f64, f64>
§Divide Two u128 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_u128(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_u128(13, 13),
Ok(1.0),
"testing 13 / 13div_u128"
);
assert_eq!(
NiceFloat::div_u128(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_u128(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_u128(5, 0).is_err_and(|e| e.is_infinite()),
);Integers with more than 15-16 digits — as can happen with u128 —
can also be problematic.
assert_eq!(
NiceFloat::div_u128(9_223_372_036_854_775_806, 1),
Err(9_223_372_036_854_776_000.0), // Almost!
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_u128(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_u128(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_u128(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_i8(e: i8, d: i8) -> Result<f64, f64>
pub const fn div_i8(e: i8, d: i8) -> Result<f64, f64>
§Divide Two i8 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_i8(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_i8(13, 13),
Ok(1.0),
"testing 13 / 13div_i8"
);
assert_eq!(
NiceFloat::div_i8(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_i8(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_i8(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_i8(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_i8(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_i8(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_i16(e: i16, d: i16) -> Result<f64, f64>
pub const fn div_i16(e: i16, d: i16) -> Result<f64, f64>
§Divide Two i16 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_i16(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_i16(13, 13),
Ok(1.0),
"testing 13 / 13div_i16"
);
assert_eq!(
NiceFloat::div_i16(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_i16(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_i16(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_i16(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_i16(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_i16(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_i32(e: i32, d: i32) -> Result<f64, f64>
pub const fn div_i32(e: i32, d: i32) -> Result<f64, f64>
§Divide Two i32 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_i32(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_i32(13, 13),
Ok(1.0),
"testing 13 / 13div_i32"
);
assert_eq!(
NiceFloat::div_i32(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_i32(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_i32(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_i32(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_i32(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_i32(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_i64(e: i64, d: i64) -> Result<f64, f64>
pub const fn div_i64(e: i64, d: i64) -> Result<f64, f64>
§Divide Two i64 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_i64(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_i64(13, 13),
Ok(1.0),
"testing 13 / 13div_i64"
);
assert_eq!(
NiceFloat::div_i64(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_i64(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_i64(5, 0).is_err_and(|e| e.is_infinite()),
);Integers with more than 15-16 digits — as can happen with i64 —
can also be problematic.
assert_eq!(
NiceFloat::div_i64(9_223_372_036_854_775_806, 1),
Err(9_223_372_036_854_776_000.0), // Almost!
);
assert_eq!(
NiceFloat::div_i64(-9_223_372_036_854_775_806, 1),
Err(-9_223_372_036_854_776_000.0), // Almost!
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_i64(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_i64(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_i64(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_i128(e: i128, d: i128) -> Result<f64, f64>
pub const fn div_i128(e: i128, d: i128) -> Result<f64, f64>
§Divide Two i128 as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_i128(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_i128(13, 13),
Ok(1.0),
"testing 13 / 13div_i128"
);
assert_eq!(
NiceFloat::div_i128(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_i128(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_i128(5, 0).is_err_and(|e| e.is_infinite()),
);Integers with more than 15-16 digits — as can happen with i128 —
can also be problematic.
assert_eq!(
NiceFloat::div_i128(9_223_372_036_854_775_806, 1),
Err(9_223_372_036_854_776_000.0), // Almost!
);
assert_eq!(
NiceFloat::div_i128(-9_223_372_036_854_775_806, 1),
Err(-9_223_372_036_854_776_000.0), // Almost!
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_i128(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_i128(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_i128(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_usize(e: usize, d: usize) -> Result<f64, f64>
pub const fn div_usize(e: usize, d: usize) -> Result<f64, f64>
§Divide Two usize as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_usize(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_usize(13, 13),
Ok(1.0),
"testing 13 / 13div_usize"
);
assert_eq!(
NiceFloat::div_usize(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_usize(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_usize(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_usize(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_usize(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_usize(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.
Sourcepub const fn div_isize(e: isize, d: isize) -> Result<f64, f64>
pub const fn div_isize(e: isize, d: isize) -> Result<f64, f64>
§Divide Two isize as f64.
Recast two integers as floats and divide them, returning the result.
§Examples
use dactyl::NiceFloat;
assert_eq!(
NiceFloat::div_isize(0, 13),
Ok(0.0),
);
assert_eq!(
NiceFloat::div_isize(13, 13),
Ok(1.0),
"testing 13 / 13div_isize"
);
assert_eq!(
NiceFloat::div_isize(20, 16),
Ok(1.25),
);Result::Err is used to draw attention to weird/lossy values, such as
what happens when dividing by zero.
assert!(
NiceFloat::div_isize(0, 0).is_err_and(|e| e.is_nan()),
);
assert!(
NiceFloat::div_isize(5, 0).is_err_and(|e| e.is_infinite()),
);If you ultimately need a NiceFloat, the result can be converted as usual.
// Conditional niceness.
if let Ok(nice) = NiceFloat::div_isize(3, 100).map(NiceFloat::from) {
assert_eq!(nice.as_str(), "0.03000000");
}
// Unconditional niceness.
assert_eq!(
NiceFloat::from(NiceFloat::div_isize(3, 100)).as_str(),
"0.03000000", // Result was good.
);
assert_eq!(
NiceFloat::from(NiceFloat::div_isize(3, 0)).as_str(),
"∞", // Result was infinite!
);§Errors
The result is returned either way, but will come back as an error if
NaN, infinite, or the integer portion lost precision.