-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'features/float' into dev
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
pub trait FloatWithPrecision { | ||
fn round_with_precision(&self, precision: usize) -> Self; | ||
fn floor_with_precision(&self, precision: usize) -> Self; | ||
fn ceil_with_precision(&self, precision: usize) -> Self; | ||
} | ||
|
||
impl FloatWithPrecision for f64 { | ||
fn round_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f64.powi(precision as i32); | ||
(self * p).round() / p | ||
} | ||
|
||
fn floor_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f64.powi(precision as i32); | ||
(self * p).floor() / p | ||
} | ||
|
||
fn ceil_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f64.powi(precision as i32); | ||
(self * p).ceil() / p | ||
} | ||
} | ||
|
||
impl FloatWithPrecision for f32 { | ||
fn round_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f32.powi(precision as i32); | ||
(self * p).round() / p | ||
} | ||
|
||
fn floor_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f32.powi(precision as i32); | ||
(self * p).floor() / p | ||
} | ||
|
||
fn ceil_with_precision(&self, precision: usize) -> Self { | ||
let p = 10f32.powi(precision as i32); | ||
(self * p).ceil() / p | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod num; | |
pub mod pointer; | ||
pub mod stable; | ||
pub mod sugar; | ||
pub mod float; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters