Skip to content

Commit 5eddaa5

Browse files
committed
ECKIT-350 ECKIT-359 Modernise FP exception handling
1 parent f4477e2 commit 5eddaa5

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

tests/types/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ ecbuild_add_test( TARGET eckit_test_types_cache
88

99
ecbuild_add_test( TARGET eckit_test_types_doublecompare
1010
SOURCES test_doublecompare.cc
11-
LIBS eckit )
11+
LIBS eckit_maths )
1212

1313
ecbuild_add_test( TARGET eckit_test_types_floatcompare
1414
SOURCES test_floatcompare.cc
15-
LIBS eckit )
15+
LIBS eckit_maths )
1616

1717
ecbuild_add_test( TARGET eckit_test_types_time
1818
SOURCES test_time.cc

tests/types/test_doublecompare.cc

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@
1010

1111
#include <cmath>
1212

13+
#include "eckit/maths/FloatingPointExceptions.h"
1314
#include "eckit/types/FloatCompare.h"
1415

1516
#include "eckit/testing/Test.h"
1617

17-
using namespace eckit;
18-
using namespace eckit::testing;
19-
2018
namespace eckit::test {
2119

2220
//----------------------------------------------------------------------------------------------------------------------
2321

2422
namespace {
2523

2624
bool is_equal(double a, double b, double epsilon, int maxUlps) {
27-
return eckit::types::is_approximately_equal(a, b, epsilon, maxUlps);
25+
return types::is_approximately_equal(a, b, epsilon, maxUlps);
2826
}
2927

3028
bool is_equal(double a, double b, double epsilon) {
31-
return eckit::types::is_approximately_equal(a, b, epsilon);
29+
return types::is_approximately_equal(a, b, epsilon);
3230
}
3331

3432
bool is_equal(double a, double b) {
35-
return eckit::types::is_approximately_equal(a, b, 0.00001);
33+
return types::is_approximately_equal(a, b, 0.00001);
3634
}
3735

3836
const double dEps = std::numeric_limits<double>::epsilon();
@@ -91,9 +89,13 @@ CASE("test_large_numbers_of_opposite_sign") {
9189
EXPECT(!is_equal(-1000000.0, 1000001.0));
9290
EXPECT(!is_equal(-1000001.0, 1000000.0));
9391

94-
// Overflow occurs here in eckit::types::is_approximately_equal
92+
// Overflow can occur here (as in CRAY) in eckit::types::is_approximately_equal
93+
maths::FloatingPointExceptions::disable_floating_point_exceptions();
94+
9595
EXPECT(!is_equal(-dMax, dMax));
9696
EXPECT(!is_equal(-dMax, dMax, dEps));
97+
98+
maths::FloatingPointExceptions::enable_floating_point_exceptions();
9799
}
98100

99101
CASE("test_ulp_around_one") {
@@ -326,16 +328,6 @@ CASE("test_comparisons_ulps") {
326328

327329
//----------------------------------------------------------------------------------------------------------------------
328330

329-
#if defined(_CRAYC)
330-
#include <fenv.h>
331-
#else
332-
static int fedisableexcept(int excepts) {
333-
return 0;
334-
}
335-
static int FE_ALL_EXCEPT = 0;
336-
#endif
337-
338331
int main(int argc, char** argv) {
339-
fedisableexcept(FE_ALL_EXCEPT);
340-
return run_tests(argc, argv);
332+
return eckit::testing::run_tests(argc, argv);
341333
}

tests/types/test_floatcompare.cc

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,25 @@
1010

1111
#include <cmath>
1212

13+
#include "eckit/maths/FloatingPointExceptions.h"
1314
#include "eckit/types/FloatCompare.h"
1415

1516
#include "eckit/testing/Test.h"
1617

17-
using namespace std;
18-
using namespace eckit;
19-
using namespace eckit::testing;
20-
2118
namespace eckit::test {
2219

2320
namespace {
2421

2522
bool is_equal(float a, float b, float epsilon, int maxUlps) {
26-
return eckit::types::is_approximately_equal(a, b, epsilon, maxUlps);
23+
return types::is_approximately_equal(a, b, epsilon, maxUlps);
2724
}
2825

2926
bool is_equal(float a, float b, float epsilon) {
30-
return eckit::types::is_approximately_equal(a, b, epsilon);
27+
return types::is_approximately_equal(a, b, epsilon);
3128
}
3229

3330
bool is_equal(float a, float b) {
34-
return eckit::types::is_approximately_equal(a, b, 0.00001f);
31+
return types::is_approximately_equal(a, b, 0.00001f);
3532
}
3633

3734
const float dEps = std::numeric_limits<float>::epsilon();
@@ -90,9 +87,13 @@ CASE("test_large_numbers_of_opposite_sign") {
9087
EXPECT(!is_equal(-1000000.0, 1000001.0));
9188
EXPECT(!is_equal(-1000001.0, 1000000.0));
9289

93-
// Overflow occurs here in eckit::types::is_approximately_equal
90+
// Overflow can occur here (as in CRAY) in eckit::types::is_approximately_equal
91+
maths::FloatingPointExceptions::disable_floating_point_exceptions();
92+
9493
EXPECT(!is_equal(-dMax, dMax));
9594
EXPECT(!is_equal(-dMax, dMax, dEps));
95+
96+
maths::FloatingPointExceptions::enable_floating_point_exceptions();
9697
}
9798

9899
CASE("test_ulp_around_one") {
@@ -324,16 +325,6 @@ CASE("test_comparisons_ulps") {
324325

325326
//----------------------------------------------------------------------------------------------------------------------
326327

327-
#if defined(_CRAYC)
328-
#include <fenv.h>
329-
#else
330-
static int fedisableexcept(int excepts) {
331-
return 0;
332-
}
333-
static int FE_ALL_EXCEPT = 0;
334-
#endif
335-
336328
int main(int argc, char** argv) {
337-
fedisableexcept(FE_ALL_EXCEPT);
338-
return run_tests(argc, argv);
329+
return eckit::testing::run_tests(argc, argv);
339330
}

0 commit comments

Comments
 (0)