Struct NiceFloat

Source
pub struct NiceFloat(/* private fields */);
Expand description

§Nice Float.

This struct can be used to quickly and efficiently stringify a float.

§Examples

use dactyl::NiceFloat;

assert_eq!(
    NiceFloat::from(1234.5501234501_f64).as_str(),
    "1,234.55012345", // Decimals only go to eight places.
);

Implementations§

Source§

impl NiceFloat

Source

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(), "∞");
Source

pub const NAN: Self

§NaN.

A value representing a Not-a-Number.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::NAN.as_str(), "NaN");
assert_eq!(NiceFloat::from(f64::NAN).as_str(), "NaN");
Source

pub const ZERO: Self

§Zero.

A value representing zero.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::ZERO.as_str(), "0.00000000");
assert_eq!(NiceFloat::from(0_f64).as_str(), "0.00000000");
Source

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

Source

pub const fn as_bytes(&self) -> &[u8]

§As Byte Slice.

Return the value as a byte slice.

Source

pub const fn as_str(&self) -> &str

§As String Slice.

Return the value as a string slice.

Source

pub const fn is_empty(&self) -> bool

§Is Empty?

No! Haha. But for consistency, this method exists.

Source

pub const fn len(&self) -> usize

§Length.

Return the length of the nice byte/string representation.

Note this will never be zero.

Source§

impl NiceFloat

Source

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.
Source

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.
Source

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());
Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl AsRef<[u8]> for NiceFloat

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for NiceFloat

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<str> for NiceFloat

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Clone for NiceFloat

Source§

fn clone(&self) -> NiceFloat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NiceFloat

Source§

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

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

impl Default for NiceFloat

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for NiceFloat

Source§

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

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

impl From<NiceFloat> for String

Source§

fn from(src: NiceFloat) -> Self

Converts to this type from the input type.
Source§

impl From<Result<f64, f64>> for NiceFloat

Source§

fn from(src: Result<f64, f64>) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for NiceFloat

Source§

fn from(num: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for NiceFloat

Source§

fn from(num: f64) -> Self

Converts to this type from the input type.
Source§

impl Hash for NiceFloat

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for NiceFloat

Source§

fn cmp(&self, rhs: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NiceFloat

Source§

fn eq(&self, rhs: &Self) -> 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 PartialOrd for NiceFloat

Source§

fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for NiceFloat

Source§

impl Eq for NiceFloat

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.