Skip to content

Commit

Permalink
Merge pull request #5248 from N-Dekker/Use-UnsignedPower-in-NumberToS…
Browse files Browse the repository at this point in the history
…tring

BUG: Replace `pow` with `Math::UnsignedPower` in NumberToString GTest
  • Loading branch information
thewtex authored Feb 21, 2025
2 parents 6a5db85 + 762b472 commit fa343ed
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Modules/Core/Common/test/itkNumberToStringGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

// First include the header file to be tested:
#include "itkNumberToString.h"
#include "itkMath.h"
#include <gtest/gtest.h>
#include <cmath> // For std::pow.

namespace
{
Expand Down Expand Up @@ -91,22 +91,22 @@ Test_decimal_notation_supports_up_to_twentyone_digits()
const itk::NumberToString<TValue> numberToString{};
const auto message = std::string("Floating point type: ") + floatingPointTypeName<TValue>;

for (int8_t exponent{ 20 }; exponent > 0; --exponent)
for (uint64_t exponent{ 20 }; exponent > 0; --exponent)
{
const TValue power_of_ten{ std::pow(TValue{ 10 }, static_cast<TValue>(exponent)) };
const TValue power_of_ten{ static_cast<TValue>(itk::Math::UnsignedPower(10, exponent - 1)) * TValue{ 10 } };

// Test +/- 10 ^ exponent
EXPECT_EQ(numberToString(power_of_ten), '1' + std::string(exponent, '0')) << message;
EXPECT_EQ(numberToString(-power_of_ten), "-1" + std::string(exponent, '0')) << message;
}

for (int8_t exponent{ -6 }; exponent < 0; ++exponent)
for (uint64_t exponent{ 6 }; exponent > 0; --exponent)
{
const TValue power_of_ten{ std::pow(TValue{ 10 }, static_cast<TValue>(exponent)) };
const TValue power_of_ten{ TValue{ 1 } / static_cast<TValue>(itk::Math::UnsignedPower(10, exponent)) };

// Test +/- 10 ^ exponent
EXPECT_EQ(numberToString(power_of_ten), "0." + std::string(-1 - exponent, '0') + '1') << message;
EXPECT_EQ(numberToString(-power_of_ten), "-0." + std::string(-1 - exponent, '0') + '1') << message;
// Test +/- 10 ^ -exponent
EXPECT_EQ(numberToString(power_of_ten), "0." + std::string(exponent - 1, '0') + '1') << message;
EXPECT_EQ(numberToString(-power_of_ten), "-0." + std::string(exponent - 1, '0') + '1') << message;
}
}

Expand Down

0 comments on commit fa343ed

Please # to comment.