#hash #partial-eq #f64 #eq #f32

float-derive

A crate that allows deriving Eq and Hash for types that contain floating points

1 unstable release

0.1.0 Oct 29, 2022

#2008 in Rust patterns

Download history 936/week @ 2024-06-29 412/week @ 2024-07-06 924/week @ 2024-07-13 1103/week @ 2024-07-20 1026/week @ 2024-07-27 814/week @ 2024-08-03 1123/week @ 2024-08-10 1112/week @ 2024-08-17 657/week @ 2024-08-24 1026/week @ 2024-08-31 720/week @ 2024-09-07 1246/week @ 2024-09-14 1179/week @ 2024-09-21 1294/week @ 2024-09-28 1392/week @ 2024-10-05 852/week @ 2024-10-12

4,855 downloads per month

MIT/Apache

6KB

⚡ float-derive

A crate that allows deriving Eq and Hash for types that contain floats 🦀

crates license dependency-status

[dependencies]
float-derive = "0.1.0"
IEEE 754 floating point numbers can have the value NAN (not a number). It is unequal to any float, including itself! This is the reason `f32` and `f64` doesn’t implement the `Eq` trait.

This crate allows to derive Eq and Hash for traits that contain floating points by explicitly comparing floating points such that: NAN == NAN is true.

How to use

use float_derive::{FloatPartialEq, FloatEq, FloatHash};

#[derive(FloatPartialEq, FloatEq, FloatHash)]
struct MyStruct {
    a: i32,
    b: i32,
    my_float: f32,
    my_second_float: f64
}

#[derive(FloatPartialEq, FloatEq, FloatHash)]
enum MyEnum {
    A(i32, f32),
    B { a: i32, b: f64 },
    C
}

Credits

Dependencies

~1.5MB
~41K SLoC