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)
4040static inline Sleef_quad
4141quad_rint (const Sleef_quad *op)
4242{
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+ );
4453}
4554
4655static inline Sleef_quad
Original file line number Diff line number Diff line change @@ -209,6 +209,12 @@ def test_rounding_functions(op, val):
209209 quad_result ), f"Zero sign mismatch for { op } ({ val } )"
210210
211211
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+
212218@pytest .mark .parametrize ("op" , ["exp" , "exp2" ])
213219@pytest .mark .parametrize ("val" , [
214220 # Basic cases
You can’t perform that action at this time.
0 commit comments