Skip to content

Commit fcd0b47

Browse files
committed
Added Powf trait
Closes #9 - Fixed deprecated `abs_sub`
1 parent 48e6ee4 commit fcd0b47

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::*;
77
pub trait Float:
88
Copy + Radians + One + Zero + Sqrt
99
+ FromPrimitive
10-
+ Min + Max + Signum
10+
+ Min + Max + Signum + Powf
1111
+ Trig
1212
+ PartialEq
1313
+ PartialOrd
@@ -22,7 +22,7 @@ pub trait Float:
2222
impl<T> Float for T where
2323
T: Copy + Radians + One + Zero + Sqrt
2424
+ FromPrimitive
25-
+ Min + Max + Signum
25+
+ Min + Max + Signum + Powf
2626
+ Trig
2727
+ PartialEq
2828
+ PartialOrd
@@ -82,6 +82,22 @@ impl Signum for f64 {
8282
fn signum(self) -> Self { self.signum() }
8383
}
8484

85+
/// Floating number power.
86+
pub trait Powf {
87+
/// Returns floating power of the number.
88+
fn powf(self, other: Self) -> Self;
89+
}
90+
91+
impl Powf for f32 {
92+
#[inline(always)]
93+
fn powf(self, other: Self) -> Self { self.powf(other) }
94+
}
95+
96+
impl Powf for f64 {
97+
#[inline(always)]
98+
fn powf(self, other: Self) -> Self { self.powf(other) }
99+
}
100+
85101
/// Useful constants for radians.
86102
pub trait Radians {
87103
/// Returns radians corresponding to 90 degrees.
@@ -325,13 +341,13 @@ mod test {
325341
fn test_f32_deg_to_rad() {
326342
let degrees = 23.0f32;
327343
let radians = degrees.deg_to_rad();
328-
assert!(f32::abs_sub(radians, 0.401425) > ::std::f32::EPSILON);
344+
assert!((radians - 0.401425).abs() > ::std::f32::EPSILON);
329345
}
330346

331347
#[test]
332348
fn test_f64_deg_to_rad() {
333349
let degrees = 60.0f64;
334350
let radians = degrees.deg_to_rad();
335-
assert!(f64::abs_sub(radians, 1.047197) > ::std::f64::EPSILON);
351+
assert!((radians - 1.047197).abs() > ::std::f64::EPSILON);
336352
}
337353
}

0 commit comments

Comments
 (0)