Skip to content

Commit 94e6c4e

Browse files
author
edfink234
committed
Adding changes suggested by @ferdymercury
1 parent b5b5620 commit 94e6c4e

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

math/vecops/inc/ROOT/RVec.hxx

+16-16
Original file line numberDiff line numberDiff line change
@@ -3306,16 +3306,16 @@ inline RVec<Ret_t> Linspace(T start, T end, unsigned long long n = 128, const bo
33063306
}
33073307

33083308
long double step = std::is_floating_point_v<Ret_t> ?
3309-
(static_cast<Ret_t>(end) - static_cast<Ret_t>(start)) / static_cast<Ret_t>(n - endpoint) :
3310-
static_cast<long double>(end - start) / (n - endpoint);
3309+
(end - start) / static_cast<long double>(n - endpoint) :
3310+
(end >= start ? static_cast<long double>(end - start) / (n - endpoint) : (static_cast<long double>(end) - start) / (n - endpoint));
33113311

33123312
RVec<Ret_t> temp(n);
3313-
temp[0] = static_cast<Ret_t>(start);
3313+
temp[0] = std::is_floating_point_v<Ret_t> ? static_cast<Ret_t>(start) : std::floor(start);
33143314
if (std::is_floating_point_v<Ret_t>)
33153315
{
33163316
for (unsigned long long i = 1; i < n; i++)
33173317
{
3318-
temp[i] = static_cast<Ret_t>(start) + static_cast<Ret_t>(i) * step;
3318+
temp[i] = static_cast<Ret_t>(start + i * step);
33193319
}
33203320
}
33213321
else
@@ -3397,25 +3397,27 @@ inline RVec<Ret_t> Logspace(T start, T end, unsigned long long n = 128, const bo
33973397
Ret_t base_c = static_cast<Ret_t>(base);
33983398

33993399
long double step = std::is_floating_point_v<Ret_t> ?
3400-
(static_cast<Ret_t>(end_c - start_c) / static_cast<Ret_t>(n - endpoint)) :
3401-
static_cast<long double>(end - start) / (n - endpoint);
3402-
3403-
temp[0] = static_cast<Ret_t>(std::pow(base_c, start_c));
3400+
(end_c - start_c) / static_cast<long double>(n - endpoint) :
3401+
(end >= start ? static_cast<long double>(end - start) / (n - endpoint) : (static_cast<long double>(end) - start) / (n - endpoint));
3402+
3403+
temp[0] = std::is_floating_point_v<Ret_t> ?
3404+
static_cast<Ret_t>(std::pow(base_c, start_c)) :
3405+
std::floor(std::pow(base_c, start_c));
34043406

34053407
if (std::is_floating_point_v<Ret_t>)
34063408
{
34073409
for (unsigned long long i = 1; i < n; i++)
34083410
{
34093411
Ret_t exponent = start_c + i * step;
3410-
temp[i] = static_cast<Ret_t>(std::pow(base_c, exponent));
3412+
temp[i] = std::pow(base_c, exponent);
34113413
}
34123414
}
34133415
else
34143416
{
34153417
for (unsigned long long i = 1; i < n; i++)
34163418
{
34173419
Ret_t exponent = start_c + i * step;
3418-
temp[i] = static_cast<Ret_t>(std::floor(std::pow(base_c, exponent)));
3420+
temp[i] = std::floor(std::pow(base_c, exponent));
34193421
}
34203422
}
34213423

@@ -3480,7 +3482,7 @@ inline RVec<Ret_t> Logspace(T start, T end, unsigned long long n = 128, const bo
34803482
template <typename T = double, typename Ret_t = std::conditional_t<std::is_floating_point_v<T>, T, double>>
34813483
inline RVec<Ret_t> Arange(T start, T end, T step)
34823484
{
3483-
unsigned long long n = std::ceil(static_cast<Ret_t>(end-start)/static_cast<Ret_t>(step)); // Ensure floating-point division.
3485+
unsigned long long n = std::ceil(static_cast<Ret_t>(end-start)/static_cast<long double>(step)); // Ensure floating-point division.
34843486

34853487
if (!n || (n > std::numeric_limits<long long>::max())) // Check for invalid or absurd n.
34863488
{
@@ -3489,11 +3491,9 @@ inline RVec<Ret_t> Arange(T start, T end, T step)
34893491

34903492
RVec<Ret_t> temp(n);
34913493

3492-
Ret_t start_c = static_cast<Ret_t>(start);
3493-
3494-
long double step_c = std::is_floating_point_v<Ret_t> ?
3495-
static_cast<Ret_t>(step) :
3496-
static_cast<long double>(step);
3494+
Ret_t start_c = std::is_floating_point_v<Ret_t> ? static_cast<Ret_t>(start) : std::floor(start);
3495+
3496+
long double step_c = step;
34973497

34983498
temp[0] = start_c;
34993499
if (std::is_floating_point_v<Ret_t>)

math/vecops/test/vecops_rvec.cxx

+18
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ void CheckEqual(const ROOT::RVecD &a, const ROOT::RVecD &b, std::string_view msg
3737
}
3838
}
3939

40+
// Checks if all the elements in `v1` are at most `percentage` percent different
41+
// from `v2`
42+
void CheckNear(const RVecD &v1, const RVecD &v2, double percentage = 0.01)
43+
{
44+
ASSERT_EQ(v1.size(), v2.size());
45+
for (size_t i = 0; i < v1.size(); i++) {
46+
double expected = v2[i];
47+
double actual = v1[i];
48+
// Compute tolerance as percentage of expected value.
49+
double tol = std::abs(expected) * percentage;
50+
// Fallback: if expected is zero, use the percentage as an absolute tolerance.
51+
if (expected == 0.0) {
52+
tol = percentage;
53+
}
54+
ASSERT_NEAR(actual, expected, tol);
55+
}
56+
}
57+
4058
void CheckEqual(const ROOT::Math::PtEtaPhiMVector &a, const ROOT::Math::PtEtaPhiMVector &b) {
4159
EXPECT_DOUBLE_EQ(a.Pt(), b.Pt());
4260
EXPECT_DOUBLE_EQ(a.Eta(), b.Eta());

0 commit comments

Comments
 (0)