This repository was archived by the owner on Apr 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Original file line number Diff line number Diff line change @@ -5,20 +5,20 @@ const TOINT: f64 = 1.0 / f64::EPSILON;
5
5
#[ inline]
6
6
#[ cfg_attr( all( test, assert_no_panic) , no_panic:: no_panic) ]
7
7
pub fn round ( mut x : f64 ) -> f64 {
8
- let ( f , i ) = ( x , x . to_bits ( ) ) ;
8
+ let i = x . to_bits ( ) ;
9
9
let e: u64 = i >> 52 & 0x7ff ;
10
10
let mut y: f64 ;
11
11
12
12
if e >= 0x3ff + 52 {
13
13
return x;
14
14
}
15
- if i >> 63 != 0 {
16
- x = -x;
17
- }
18
15
if e < 0x3ff - 1 {
19
16
// raise inexact if x!=0
20
17
force_eval ! ( x + TOINT ) ;
21
- return 0.0 * f;
18
+ return 0.0 * x;
19
+ }
20
+ if i >> 63 != 0 {
21
+ x = -x;
22
22
}
23
23
y = x + TOINT - TOINT - x;
24
24
if y > 0.5 {
@@ -35,3 +35,13 @@ pub fn round(mut x: f64) -> f64 {
35
35
y
36
36
}
37
37
}
38
+
39
+ #[ cfg( test) ]
40
+ mod tests {
41
+ use super :: round;
42
+
43
+ #[ test]
44
+ fn negative_zero ( ) {
45
+ assert_eq ! ( round( -0.0_f64 ) . to_bits( ) , ( -0.0_f64 ) . to_bits( ) ) ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change @@ -12,13 +12,13 @@ pub fn roundf(mut x: f32) -> f32 {
12
12
if e >= 0x7f + 23 {
13
13
return x;
14
14
}
15
- if i >> 31 != 0 {
16
- x = -x;
17
- }
18
15
if e < 0x7f - 1 {
19
16
force_eval ! ( x + TOINT ) ;
20
17
return 0.0 * x;
21
18
}
19
+ if i >> 31 != 0 {
20
+ x = -x;
21
+ }
22
22
y = x + TOINT - TOINT - x;
23
23
if y > 0.5f32 {
24
24
y = y + x - 1.0 ;
@@ -33,3 +33,13 @@ pub fn roundf(mut x: f32) -> f32 {
33
33
y
34
34
}
35
35
}
36
+
37
+ #[ cfg( test) ]
38
+ mod tests {
39
+ use super :: roundf;
40
+
41
+ #[ test]
42
+ fn negative_zero ( ) {
43
+ assert_eq ! ( roundf( -0.0_f32 ) . to_bits( ) , ( -0.0_f32 ) . to_bits( ) ) ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments