Skip to content

Commit

Permalink
BUG: Replace pow with Math::UnsignedPower in NumberToString GTest
Browse files Browse the repository at this point in the history
Aims to address test failures reported by Yuri Victorovich, at
#5245, saying:

```
/usr/ports/science/InsightToolkit/work/ITK-5.4.2/Modules/Core/Common/test/itkNumberToStringGTest.cxx:99: Failure
Expected equality of these values:
  numberToString(power_of_ten)
    Which is: "100000010000"
  '1' + std::string(exponent, '0')
    Which is: "100000000000"
Floating point type: float

...

/usr/ports/science/InsightToolkit/work/ITK-5.4.2/Modules/Core/Common/test/itkNumberToStringGTest.cxx:109: Failure
Expected equality of these values:
  numberToString(-power_of_ten)
    Which is: "-0.000009999999999999999"
  "-0." + std::string(-1 - exponent, '0') + '1'
    Which is: "-0.00001"
Floating point type: double/usr/ports/science/InsightToolkit/work/ITK-5.4.2/Modules/Core/Common/test/itkNumberToStringGTest.cxx:109: Failure
Expected equality of these values:
  numberToString(-power_of_ten)
    Which is: "-0.000009999999999999999"
  "-0." + std::string(-1 - exponent, '0') + '1'
    Which is: "-0.00001"
Floating point type: double
```

Using clang-19 on FreeBSD 14.2.

Sean McBride confirmed that this commit would solve the issue on his FreeBSD VM.
  • Loading branch information
N-Dekker committed Feb 20, 2025
1 parent 018e0ea commit 6016cf6
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 6016cf6

Please # to comment.