rsdiff_math/num/
traits.rs1use paste::paste;
6
7pub trait NaturalNumber {
8 fn harmonic(&self) -> f64;
9}
10
11macro_rules! natural {
12 ($($n:tt),*) => {
13 $(natural!(@impl $n);)*
14 };
15 (@impl $n:tt) => {
16 paste! {
17 impl NaturalNumber for [<u$n>] {
18 fn harmonic(&self) -> f64 {
19 let mut sum = 0.0;
20 for i in 1..=*self {
21 sum += (i as f64).recip();
22 }
23 sum
24 }
25 }
26
27 impl NaturalNumber for core::num::[<NonZeroU$n>] {
28 fn harmonic(&self) -> f64 {
29 self.get().harmonic()
30 }
31 }
32 }
33 };
34}
35
36natural!(8, 16, 32, 64, 128, size);