File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,16 @@ quad_absolute(const Sleef_quad *op)
40
40
static inline Sleef_quad
41
41
quad_rint (const Sleef_quad *op)
42
42
{
43
- return Sleef_rintq1 (*op);
43
+ Sleef_quad halfway = Sleef_addq1_u05 (
44
+ Sleef_truncq1 (*op),
45
+ Sleef_copysignq1 (Sleef_cast_from_doubleq1 (0.5 ), *op)
46
+ );
47
+
48
+ // Sleef_rintq1 does not handle some near-halfway cases correctly, so we
49
+ // manually round up or down when x is not exactly halfway
50
+ return Sleef_icmpeqq1 (*op, halfway) ? Sleef_rintq1 (*op) : (
51
+ Sleef_icmpleq1 (*op, halfway) ? Sleef_floorq1 (*op) : Sleef_ceilq1 (*op)
52
+ );
44
53
}
45
54
46
55
static inline Sleef_quad
Original file line number Diff line number Diff line change @@ -209,6 +209,12 @@ def test_rounding_functions(op, val):
209
209
quad_result ), f"Zero sign mismatch for { op } ({ val } )"
210
210
211
211
212
+ def test_rint_near_halfway ():
213
+ assert np .rint (QuadPrecision ("7.4999999999999999" )) == 7
214
+ assert np .rint (QuadPrecision ("7.49999999999999999" )) == 7
215
+ assert np .rint (QuadPrecision ("7.5" )) == 8
216
+
217
+
212
218
@pytest .mark .parametrize ("op" , ["exp" , "exp2" ])
213
219
@pytest .mark .parametrize ("val" , [
214
220
# Basic cases
You can’t perform that action at this time.
0 commit comments