Change mju_round to use standard round() function.
PiperOrigin-RevId: 942873144 Change-Id: I3fd0d630643c104ff464d619387ff36582c2a5d8
This commit is contained in:
committed by
Copybara-Service
parent
4cb14bef47
commit
1ea2d884d2
@@ -27,6 +27,8 @@ General
|
|||||||
``(nv x nC)``.
|
``(nv x nC)``.
|
||||||
- Removed the legacy sparse ancestor-walk inertia matrix ``mjData.qM``. The joint-space inertia matrix is now stored
|
- Removed the legacy sparse ancestor-walk inertia matrix ``mjData.qM``. The joint-space inertia matrix is now stored
|
||||||
exclusively in the compressed sparse row (CSR) format ``mjData.M``.
|
exclusively in the compressed sparse row (CSR) format ``mjData.M``.
|
||||||
|
- :ref:`mju_round` now breaks ties away from zero rather than towards :math:`+\infty`. This only affects
|
||||||
|
negative half-integers, e.g. ``mju_round(-2.5)`` now returns -3 rather than -2.
|
||||||
|
|
||||||
Version 3.10.0 (June 22, 2026)
|
Version 3.10.0 (June 22, 2026)
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|||||||
@@ -15,13 +15,13 @@
|
|||||||
#include "engine/engine_util_misc.h"
|
#include "engine/engine_util_misc.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <mujoco/mjdata.h>
|
|
||||||
#include <mujoco/mjmacro.h>
|
#include <mujoco/mjmacro.h>
|
||||||
#include <mujoco/mjmodel.h>
|
#include <mujoco/mjmodel.h>
|
||||||
#include "engine/engine_array_safety.h"
|
#include "engine/engine_array_safety.h"
|
||||||
@@ -1762,14 +1762,10 @@ mjtNum mju_sign(mjtNum x) {
|
|||||||
|
|
||||||
// round to nearest integer
|
// round to nearest integer
|
||||||
int mju_round(mjtNum x) {
|
int mju_round(mjtNum x) {
|
||||||
mjtNum lower = floor(x);
|
double d = (double)x;
|
||||||
mjtNum upper = ceil(x);
|
if (d > INT_MAX) return INT_MAX;
|
||||||
|
if (d < INT_MIN) return INT_MIN;
|
||||||
if (x-lower < upper-x) {
|
return (int)round(d);
|
||||||
return (int)lower;
|
|
||||||
} else {
|
|
||||||
return (int)upper;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "src/engine/engine_util_misc.h"
|
#include "src/engine/engine_util_misc.h"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <climits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -1720,5 +1721,28 @@ TEST_F(ShellTFITest, NoInteriorSmallGrid) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UtilMiscTest, Round) {
|
||||||
|
// basic rounding
|
||||||
|
EXPECT_EQ(mju_round(2.3), 2);
|
||||||
|
EXPECT_EQ(mju_round(2.7), 3);
|
||||||
|
EXPECT_EQ(mju_round(-2.3), -2);
|
||||||
|
EXPECT_EQ(mju_round(-2.7), -3);
|
||||||
|
|
||||||
|
// exact integers
|
||||||
|
EXPECT_EQ(mju_round(0.0), 0);
|
||||||
|
EXPECT_EQ(mju_round(3.0), 3);
|
||||||
|
EXPECT_EQ(mju_round(-3.0), -3);
|
||||||
|
|
||||||
|
// ties: round() rounds away from zero
|
||||||
|
EXPECT_EQ(mju_round(0.5), 1);
|
||||||
|
EXPECT_EQ(mju_round(1.5), 2);
|
||||||
|
EXPECT_EQ(mju_round(-0.5), -1);
|
||||||
|
EXPECT_EQ(mju_round(-1.5), -2);
|
||||||
|
|
||||||
|
// overflow clamps to INT_MAX/INT_MIN
|
||||||
|
EXPECT_EQ(mju_round(1e18), INT_MAX);
|
||||||
|
EXPECT_EQ(mju_round(-1e18), INT_MIN);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace mujoco
|
} // namespace mujoco
|
||||||
|
|||||||
Reference in New Issue
Block a user