File tree 4 files changed +44
-3
lines changed
4 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -377,7 +377,6 @@ pub trait F64Ext {
377
377
#[ cfg( todo) ]
378
378
fn ceil ( self ) -> Self ;
379
379
380
- #[ cfg( todo) ]
381
380
fn round ( self ) -> Self ;
382
381
383
382
#[ cfg( todo) ]
@@ -498,7 +497,6 @@ impl F64Ext for f64 {
498
497
ceil ( self )
499
498
}
500
499
501
- #[ cfg( todo) ]
502
500
#[ inline]
503
501
fn round ( self ) -> Self {
504
502
round ( self )
Original file line number Diff line number Diff line change
1
+ macro_rules! force_eval {
2
+ ( $e: expr) => {
3
+ unsafe { :: core:: ptr:: read_volatile( & $e) ; }
4
+ }
5
+ }
6
+
1
7
mod fabs;
2
8
mod fabsf;
3
9
mod fmodf;
4
10
mod powf;
11
+ mod round;
5
12
mod scalbnf;
6
13
mod sqrtf;
7
14
8
15
pub use self :: fabs:: fabs;
9
16
pub use self :: fabsf:: fabsf;
10
17
pub use self :: fmodf:: fmodf;
11
18
pub use self :: powf:: powf;
19
+ pub use self :: round:: round;
12
20
pub use self :: scalbnf:: scalbnf;
13
21
pub use self :: sqrtf:: sqrtf;
14
22
Original file line number Diff line number Diff line change
1
+ use core:: f64;
2
+
3
+ const TOINT : f64 = 1.0 / f64:: EPSILON ;
4
+
5
+ pub fn round ( mut x : f64 ) -> f64 {
6
+ let ( f, i) = ( x, x. to_bits ( ) ) ;
7
+ let e: u64 = i >> 52 & 0x7ff ;
8
+ let mut y: f64 ;
9
+
10
+ if e >= 0x3ff + 52 {
11
+ return x;
12
+ }
13
+ if i >> 63 != 0 {
14
+ x = -x;
15
+ }
16
+ if e < 0x3ff - 1 {
17
+ // raise inexact if x!=0
18
+ force_eval ! ( x + TOINT ) ;
19
+ return 0.0 * f;
20
+ }
21
+ y = x + TOINT - TOINT - x;
22
+ if y > 0.5 {
23
+ y = y + x - 1.0 ;
24
+ } else if y <= -0.5 {
25
+ y = y + x + 1.0 ;
26
+ } else {
27
+ y = y + x;
28
+ }
29
+
30
+ if i >> 63 != 0 {
31
+ -y
32
+ } else {
33
+ y
34
+ }
35
+ }
Original file line number Diff line number Diff line change @@ -618,7 +618,7 @@ f64_f64! {
618
618
// log10,
619
619
// log1p,
620
620
// log2,
621
- // round,
621
+ round,
622
622
// sin,
623
623
// sinh,
624
624
// sqrt,
You can’t perform that action at this time.
0 commit comments