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

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

#2514 in Rust patterns

Download history 790/week @ 2024-12-26 356/week @ 2025-01-02 1281/week @ 2025-01-09 1598/week @ 2025-01-16 1055/week @ 2025-01-23 1288/week @ 2025-01-30 1038/week @ 2025-02-06 1600/week @ 2025-02-13 1529/week @ 2025-02-20 1079/week @ 2025-02-27 1091/week @ 2025-03-06 1001/week @ 2025-03-13 1158/week @ 2025-03-20 1497/week @ 2025-03-27 1376/week @ 2025-04-03 1605/week @ 2025-04-10

5,795 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

~2MB
~43K SLoC