Skip to content

Commit

Permalink
Rename trait Float into TypedFloat and use num::Float (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelmas authored Aug 11, 2023
1 parent ef0b3eb commit e1978ed
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 125 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ It that example:

Most methods and traits available on the underlying type are available on the types of this crate.

Most constants are also available, with the most appropriate `Float` type (except `NAN` for obvious reasons) in the [`tf64`] and [`tf32`] modules (in [`tf64::consts`] and [`tf32::consts`] respectively when the constant comes from [`core::f64::consts`] or [`core::f32::consts`]). Those modules are named that way to avoid conflicts or confusion with the primitives [`f32`] and [`f64`].
Most constants are also available, with the most appropriate `TypedFloat` type (except `NAN` for obvious reasons) in the [`tf64`] and [`tf32`] modules (in [`tf64::consts`] and [`tf32::consts`] respectively when the constant comes from [`core::f64::consts`] or [`core::f32::consts`]). Those modules are named that way to avoid conflicts or confusion with the primitives [`f32`] and [`f64`].

As none of the types of this crate can be `NaN`, the traits [`core::cmp::Ord`] and [`core::cmp::Eq`] are implemented for all of them.

⚠️ Like for primitives [`f32`] and [`f64`],`-0.0 == +0.0` is `true` for all types of this crate. For that reason, [`std::hash::Hash`] is not implemented.
⚠️ Like for primitives [`f32`] and [`f64`],`-0.0 == +0.0` is `true` for all types of this crate. For that reason, [`core::hash::Hash`] is not implemented.
To facilitate comparisons, the methods `is_positive_zero` and `is_negative_zero` are added.

# Similar crates
Expand All @@ -85,7 +85,7 @@ Is on [docs.rs](https://docs.rs/typed_floats).
[`core::f64::consts`]: https://doc.rust-lang.org/core/f64/consts/index.html
[`core::cmp::Ord`]: https://doc.rust-lang.org/core/cmp/trait.Ord.html "`Ord`"
[`core::cmp::Eq`]: https://doc.rust-lang.org/core/cmp/trait.Eq.html "`Eq`"
[`std::hash::Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html "`Hash`"
[`core::hash::Hash`]: https://doc.rust-lang.org/core/hash/trait.Hash.html "`Hash`"
[`NonNaN`]: https://docs.rs/typed_floats/latest/typed_floats/struct.NonNaN.html
[`NonNaNFinite`]: https://docs.rs/typed_floats/latest/typed_floats/struct.NonNaNFinite.html
[`NonZeroNonNaN`]: https://docs.rs/typed_floats/latest/typed_floats/struct.NonZeroNonNaN.html
Expand Down
1 change: 1 addition & 0 deletions typed_floats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ version = "0.1.34"
[dependencies]
serde_json = {version = "1.0"}
thiserror = "1.0"
num = "0.4"
typed_floats_macros = {version = "=0.1.34", path = "../typed_floats_macros"}
36 changes: 11 additions & 25 deletions typed_floats/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = include_str!("../README.truncated.md")]
//! # Rules
//!
//! Conversions rules for operations are summarized in [`Float`].
//! Conversions rules for operations are summarized in [`TypedFloat`].
//!
//! # Examples
//!
Expand Down Expand Up @@ -374,8 +374,10 @@ pub trait DivEuclid<T> {
fn div_euclid(self, rhs: T) -> Self::Output;
}

use num::Float;

typed_floats_macros::generate_docs!(
pub trait Float:
pub trait TypedFloat:
Eq
+ Copy
+ Ord
Expand All @@ -398,30 +400,14 @@ typed_floats_macros::generate_docs!(
+ TryFrom<NonZeroI16>
+ TryFrom<NonZeroI32>
+ TryFrom<NonZeroI64>
+ std::ops::Add
+ std::ops::Sub
+ std::ops::Mul
+ std::ops::Div
+ std::ops::Rem
+ core::ops::Add
+ core::ops::Sub
+ core::ops::Mul
+ core::ops::Div
+ core::ops::Rem
{
/// The primitive float type (f32 or f64)
type Content: Sized
+ Clone
+ Copy
+ PartialOrd
+ PartialEq
+ core::fmt::Debug
+ core::fmt::Display
+ std::ops::Add<Output = Self::Content>
+ std::ops::Sub<Output = Self::Content>
+ std::ops::Mul<Output = Self::Content>
+ std::ops::Div<Output = Self::Content>
+ std::ops::Rem<Output = Self::Content>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign;
type Content: Float;

/// Creates a new value from a primitive type
/// It adds a little overhead compared to `new_unchecked`
Expand Down Expand Up @@ -622,7 +608,7 @@ typed_floats_macros::generate_docs!(
}
);

/// An error that can occur when converting from a string into a typed `Float`
/// An error that can occur when converting from a string into a `TypedFloat`
#[derive(Error, Debug)]
pub enum FromStrError {
/// The string did not contain a valid float number
Expand Down
8 changes: 4 additions & 4 deletions typed_floats_macros/src/add_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ fn generate_op_table(floats: &[FloatDefinition], op: &OpRhs) -> proc_macro2::Tok
let result = op.get_result(float, rhs, floats);

let result_str = match result {
Some(result) => result.name,
None => float_type,
ReturnTypeDefinition::FloatDefinition(result) => result.name,
ReturnTypeDefinition::NativeFloat => float_type,
};
line.push(result_str.to_string());
}
Expand Down Expand Up @@ -147,8 +147,8 @@ fn generate_fn_table(floats: &[FloatDefinition]) -> proc_macro2::TokenStream {
let result = op.get_result(float, floats);

match result {
Some(result) => line.push(result.name.to_string()),
None => line.push(float_type.to_string()),
ReturnTypeDefinition::FloatDefinition(result) => line.push(result.name.to_string()),
ReturnTypeDefinition::NativeFloat => line.push(float_type.to_string()),
};
}

Expand Down
12 changes: 6 additions & 6 deletions typed_floats_macros/src/gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::*;
fn test_op_checks(
float: &FloatDefinition,
op_name: &str,
result_type: &Option<FloatDefinition>,
result_type: &ReturnTypeDefinition,
var: &proc_macro2::Ident,
) -> proc_macro2::TokenStream {
let mut res = proc_macro2::TokenStream::new();

if let Some(def) = result_type {
if let ReturnTypeDefinition::FloatDefinition(def) = result_type {
let full_type = def.name;

if def.s.accept_inf {
Expand Down Expand Up @@ -131,8 +131,8 @@ pub(crate) fn generate_tests_self(float_type: &'static str) -> proc_macro2::Toke
let test_float = &op.get_test("a");

let get = match &op.get_result(float, &floats_f64) {
None => quote! { res },
Some(_) => {
ReturnTypeDefinition::NativeFloat => quote! { res },
ReturnTypeDefinition::FloatDefinition(_) => {
quote! { res.get() }
}
};
Expand Down Expand Up @@ -237,8 +237,8 @@ pub(crate) fn generate_tests_self_rhs(float_type: &'static str) -> proc_macro2::
let test_float = &op.get_test_primitive("a", "b");

let get = match &op.get_result(float, float_rhs, &floats_f64) {
None => quote! { res },
Some(_) => {
ReturnTypeDefinition::NativeFloat => quote! { res },
ReturnTypeDefinition::FloatDefinition(_) => {
quote! { res.get() }
}
};
Expand Down
Loading

0 comments on commit e1978ed

Please sign in to comment.