Skip to content

Commit af0ced0

Browse files
authored
math: fix round_to_even(), add tests (#25922)
1 parent 502d6a1 commit af0ced0

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎vlib/math/floor.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ pub fn round_to_even(x f64) f64 {
144144
half_minus_ulp := u64(u64(1) << (shift - 1)) - 1
145145
e_ -= u64(bias)
146146
bits += (half_minus_ulp + (bits >> (shift - e_)) & 1) >> e_
147-
bits &= frac_mask >> e_
148-
bits ^= frac_mask >> e_
147+
bits &= ~(frac_mask >> e_)
149148
} else if e_ == bias - 1 && bits & frac_mask != 0 {
150149
// round 0.5 < abs(x) < 1.
151150
bits = bits & sign_mask | uvone // +-1

‎vlib/math/floor_test.v‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import math
2+
3+
fn test_round_to_even() {
4+
assert math.round_to_even(0.123) == 0.0
5+
assert math.round_to_even(123.12345) == 123.00
6+
}

0 commit comments

Comments
 (0)