Skip to content

Commit 1ea2d88

Browse files
yuvaltassacopybara-github
authored andcommitted
Change mju_round to use standard round() function.
PiperOrigin-RevId: 942873144 Change-Id: I3fd0d630643c104ff464d619387ff36582c2a5d8
1 parent 4cb14be commit 1ea2d88

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ General
2727
``(nv x nC)``.
2828
- Removed the legacy sparse ancestor-walk inertia matrix ``mjData.qM``. The joint-space inertia matrix is now stored
2929
exclusively in the compressed sparse row (CSR) format ``mjData.M``.
30+
- :ref:`mju_round` now breaks ties away from zero rather than towards :math:`+\infty`. This only affects
31+
negative half-integers, e.g. ``mju_round(-2.5)`` now returns -3 rather than -2.
3032

3133
Version 3.10.0 (June 22, 2026)
3234
------------------------------

src/engine/engine_util_misc.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
#include "engine/engine_util_misc.h"
1616

1717
#include <ctype.h>
18+
#include <limits.h>
1819
#include <math.h>
1920
#include <stdint.h>
2021
#include <stdio.h>
2122
#include <stdlib.h>
2223
#include <string.h>
2324

24-
#include <mujoco/mjdata.h>
2525
#include <mujoco/mjmacro.h>
2626
#include <mujoco/mjmodel.h>
2727
#include "engine/engine_array_safety.h"
@@ -1762,14 +1762,10 @@ mjtNum mju_sign(mjtNum x) {
17621762

17631763
// round to nearest integer
17641764
int mju_round(mjtNum x) {
1765-
mjtNum lower = floor(x);
1766-
mjtNum upper = ceil(x);
1767-
1768-
if (x-lower < upper-x) {
1769-
return (int)lower;
1770-
} else {
1771-
return (int)upper;
1772-
}
1765+
double d = (double)x;
1766+
if (d > INT_MAX) return INT_MAX;
1767+
if (d < INT_MIN) return INT_MIN;
1768+
return (int)round(d);
17731769
}
17741770

17751771

test/engine/engine_util_misc_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/engine/engine_util_misc.h"
1818

1919
#include <array>
20+
#include <climits>
2021
#include <cmath>
2122
#include <cstddef>
2223
#include <cstdint>
@@ -1720,5 +1721,28 @@ TEST_F(ShellTFITest, NoInteriorSmallGrid) {
17201721
}
17211722
}
17221723

1724+
TEST_F(UtilMiscTest, Round) {
1725+
// basic rounding
1726+
EXPECT_EQ(mju_round(2.3), 2);
1727+
EXPECT_EQ(mju_round(2.7), 3);
1728+
EXPECT_EQ(mju_round(-2.3), -2);
1729+
EXPECT_EQ(mju_round(-2.7), -3);
1730+
1731+
// exact integers
1732+
EXPECT_EQ(mju_round(0.0), 0);
1733+
EXPECT_EQ(mju_round(3.0), 3);
1734+
EXPECT_EQ(mju_round(-3.0), -3);
1735+
1736+
// ties: round() rounds away from zero
1737+
EXPECT_EQ(mju_round(0.5), 1);
1738+
EXPECT_EQ(mju_round(1.5), 2);
1739+
EXPECT_EQ(mju_round(-0.5), -1);
1740+
EXPECT_EQ(mju_round(-1.5), -2);
1741+
1742+
// overflow clamps to INT_MAX/INT_MIN
1743+
EXPECT_EQ(mju_round(1e18), INT_MAX);
1744+
EXPECT_EQ(mju_round(-1e18), INT_MIN);
1745+
}
1746+
17231747
} // namespace
17241748
} // namespace mujoco

0 commit comments

Comments
 (0)