Skip to content

Commit 9e6cd43

Browse files
Fix casting negative double to unsigned int in GenerateGroupingPowder -- ornl-next (#39222)
Fix casting negative double to unsigned int in GenerateGroupingPowder (#39205) * Fix casting negative double to unsigned int A size_t is an unsigned int, but the way this method was casting resulted in a negative value being cast to a large positive value. It now uses an int64_t for the intermediate step instead. * Add release note * Results changed in this test * Avoid int -> double conversion Without this cast, the second component of the sum will be converted from an int64_t to a double, resulting in a compiler warning and potential loss of precision. Co-authored-by: James Clarke <139879523+jclarkeSTFC@users.noreply.github.com>
1 parent a6c924b commit 9e6cd43

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

Framework/DataHandling/src/GenerateGroupingPowder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ class SphericalSectorLabelor : public Labelor {
7979
: Labelor(t_step, a_step, a_start), inv_tt_step(1.0 / tt_step), inv_aa_step(1.0 / aa_step),
8080
num_aa_step(int(std::ceil(360.0 / a_step))) {};
8181
size_t operator()(SpectrumInfo const &spectrumInfo, size_t i) override {
82-
return static_cast<size_t>(spectrumInfo.twoTheta(i) * inv_tt_step) * num_aa_step +
83-
static_cast<size_t>(std::floor((spectrumInfo.azimuthal(i) - aa_start) * inv_aa_step)) % num_aa_step + 1;
82+
return static_cast<size_t>(
83+
static_cast<int64_t>(spectrumInfo.twoTheta(i) * inv_tt_step * num_aa_step) +
84+
static_cast<int64_t>(std::floor((spectrumInfo.azimuthal(i) - aa_start) * inv_aa_step)) % num_aa_step + 1);
8485
};
8586
};
8687

Framework/DataHandling/test/GenerateGroupingPowder2Test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ class GenerateGroupingPowder2Test : public CxxTest::TestSuite {
465465
const bool numberByAngle{true};
466466
const bool splitSides{true};
467467
const std::vector<int> groups_exp{7, 8, 9, 10, 11, 12, 13, 14};
468-
const std::vector<double> pixel_groups_exp{11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 12, 12, 12, 12, 12, 12};
468+
const std::vector<double> pixel_groups_exp{12, 12, 12, 10, 10, 10, 9, 9, 9, 8, 8, 10, 10, 10, 12, 11, 11, 13};
469469
run_SNAPliteTest("PowderGrouping_no_azimuth_angle_numbering", ang1, ang2, numberByAngle, splitSides, groups_exp,
470470
pixel_groups_exp);
471471
}

Framework/DataHandling/test/GenerateGroupingPowderTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ class GenerateGroupingPowderTest : public CxxTest::TestSuite {
470470
const bool numberByAngle{true};
471471
const bool splitSides{true};
472472
const std::vector<int> groups_exp{7, 8, 9, 10, 11, 12, 13, 14};
473-
const std::vector<double> pixel_groups_exp{11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 12, 12, 12, 12, 12, 12};
473+
const std::vector<double> pixel_groups_exp{12, 12, 12, 10, 10, 10, 9, 9, 9, 8, 8, 10, 10, 10, 12, 11, 11, 13};
474474
run_SNAPliteTest("PowderGrouping_no_azimuth_angle_numbering", ang1, ang2, numberByAngle, splitSides, groups_exp,
475475
pixel_groups_exp);
476476
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed bug when calculating pixel groups in :ref:`GenerateGroupingPowder <algm-GenerateGroupingPowder>` where under certain conditions the number of pixels in a group was not calculated correctly. This could lead to incorrect grouping of pixels in the output workspace. The issue was caused by an incorrect handling of the pixel indices when calculating the groups. The fix ensures that the pixel indices are handled correctly, resulting in accurate grouping of pixels in the output workspace.

0 commit comments

Comments
 (0)