Skip to content

Commit 32c86db

Browse files
committed
renames NAN to NaN and INFINITY to INF
Although less-than-ideal names, this prevents Subspace from clashing with the C++ standard library, which reserves these names as macros.
1 parent ea78391 commit 32c86db

18 files changed

+209
-280
lines changed

sus/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ if(${SUBSPACE_BUILD_TESTS})
294294
"mem/swap_unittest.cc"
295295
"mem/take_unittest.cc"
296296
"num/__private/literals_unittest.cc"
297-
"num/cmath_macros_unittest.cc"
298297
"num/cast_unittest.cc"
299298
"num/f32_unittest.cc"
300299
"num/f64_unittest.cc"
@@ -366,4 +365,3 @@ if(${SUBSPACE_BUILD_TESTS})
366365
)
367366
gtest_discover_tests(subspace_overflow_unittests)
368367
endif()
369-

sus/choice/choice_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ TEST(Choice, PartialOrder) {
859859
EXPECT_EQ(std::partial_order(u1, u2), std::partial_ordering::less);
860860

861861
// NaN is unordered.
862-
auto u3 = ChoiceFloatFloat::with<Order::First>(f32::NAN);
862+
auto u3 = ChoiceFloatFloat::with<Order::First>(f32::NaN);
863863
EXPECT_EQ(std::partial_order(u1, u3), std::partial_ordering::unordered);
864864

865865
// 0 == -0.

sus/construct/cast.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ struct CastImpl<T, T> {
7979
///
8080
/// * Casting from a float to an integer will perform a static_cast, which
8181
/// rounds the float towards zero, except:
82-
/// * `NAN` will return 0.
82+
/// * `NaN` will return 0.
8383
/// * Values larger than the maximum integer value, including
84-
/// [`f32::INFINITY`]($sus::num::f32::INFINITY),
84+
/// [`f32::INF`]($sus::num::f32::INF),
8585
/// will saturate to the maximum value of the integer type.
8686
/// * Values smaller than the minimum integer value, including
87-
/// [`f32::NEG_INFINITY`]($sus::num::f32::NEG_INFINITY), will saturate to
87+
/// [`f32::NEG_INF`]($sus::num::f32::NEG_INF), will saturate to
8888
/// the minimum value of the integer type.
8989
/// * Casting from an integer to a float will perform a `static_cast`, which
9090
/// converts to the nearest floating point value. The rounding direction for
@@ -97,10 +97,10 @@ struct CastImpl<T, T> {
9797
/// [`f32`]($sus::num::f32) (or float) performs the same action as a
9898
/// `static_cast` if the value is in range for [`f32`]($sus::num::f32),
9999
/// otherwise:
100-
/// * `NAN` will return a `NAN`.
100+
/// * `NaN` will return a `NaN`.
101101
/// * Values outside of [`f32`]($sus::num::f32)'s range will return
102-
/// [`f32::INFINITY`]($sus::num::f32::INFINITY) or
103-
/// [`f32::NEG_INFINITY`]($sus::num::f32::NEG_INFINITY) for positive and
102+
/// [`f32::INF`]($sus::num::f32::INF) or
103+
/// [`f32::NEG_INF`]($sus::num::f32::NEG_INF) for positive and
104104
/// negative values respectively.
105105
/// * Casting to and from [`std::byte`](
106106
/// https://en.cppreference.com/w/cpp/types/byte) produces the same values

sus/iter/iterator_defn.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class IteratorBase {
493493
///
494494
/// ```cpp
495495
/// sus_check(
496-
/// sus::Array<f32, 3>(2.4, f32::NAN, 1.3)
496+
/// sus::Array<f32, 3>(2.4, f32::NaN, 1.3)
497497
/// .into_iter()
498498
/// .reduce(&f32::max)
499499
/// .unwrap() ==
@@ -539,7 +539,7 @@ class IteratorBase {
539539
///
540540
/// ```cpp
541541
/// sus_check(
542-
/// sus::Array<f32, 3>(2.4, f32::NAN, 1.3)
542+
/// sus::Array<f32, 3>(2.4, f32::NaN, 1.3)
543543
/// .into_iter()
544544
/// .reduce(&f32::min)
545545
/// .unwrap() ==

sus/iter/iterator_unittest.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,26 +1011,26 @@ TEST(Iterator, PartialCmp) {
10111011
smol.iter().partial_cmp(bigg.iter()));
10121012
}
10131013
{
1014-
auto smol = sus::Array<f32, 2>(f32::NAN, f32::NAN);
1015-
auto bigg = sus::Array<f32, 2>(f32::NAN, f32::NAN);
1014+
auto smol = sus::Array<f32, 2>(f32::NaN, f32::NaN);
1015+
auto bigg = sus::Array<f32, 2>(f32::NaN, f32::NaN);
10161016
EXPECT_EQ(std::partial_ordering::unordered,
10171017
smol.iter().partial_cmp(bigg.iter()));
10181018
}
10191019
{
1020-
auto smol = sus::Array<f32, 2>(f32::NAN, 1.f);
1021-
auto bigg = sus::Array<f32, 2>(f32::NAN, 2.f);
1020+
auto smol = sus::Array<f32, 2>(f32::NaN, 1.f);
1021+
auto bigg = sus::Array<f32, 2>(f32::NaN, 2.f);
10221022
EXPECT_EQ(std::partial_ordering::unordered,
10231023
smol.iter().partial_cmp(bigg.iter()));
10241024
}
10251025
{
1026-
auto smol = sus::Array<f32, 2>(1.f, f32::NAN);
1027-
auto bigg = sus::Array<f32, 2>(f32::NAN, f32::NAN);
1026+
auto smol = sus::Array<f32, 2>(1.f, f32::NaN);
1027+
auto bigg = sus::Array<f32, 2>(f32::NaN, f32::NaN);
10281028
EXPECT_EQ(std::partial_ordering::unordered,
10291029
smol.iter().partial_cmp(bigg.iter()));
10301030
}
10311031
{
1032-
auto smol = sus::Array<f32, 2>(1.f, f32::NAN);
1033-
auto bigg = sus::Array<f32, 2>(2.f, f32::NAN);
1032+
auto smol = sus::Array<f32, 2>(1.f, f32::NaN);
1033+
auto bigg = sus::Array<f32, 2>(2.f, f32::NaN);
10341034
EXPECT_EQ(std::partial_ordering::less,
10351035
smol.iter().partial_cmp(bigg.iter()));
10361036
}
@@ -2164,11 +2164,11 @@ TEST(Iterator, Ge) {
21642164
}
21652165
{
21662166
auto it1 = sus::Array<f32, 2>(1.f, 4.f).into_iter();
2167-
auto it2 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2167+
auto it2 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
21682168
EXPECT_EQ(false, sus::move(it1).ge(sus::move(it2)));
21692169
}
21702170
{
2171-
auto it1 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2171+
auto it1 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
21722172
auto it2 = sus::Array<f32, 2>(1.f, 3.f).into_iter();
21732173
EXPECT_EQ(false, sus::move(it1).ge(sus::move(it2)));
21742174
}
@@ -2195,11 +2195,11 @@ TEST(Iterator, Gt) {
21952195
}
21962196
{
21972197
auto it1 = sus::Array<f32, 2>(1.f, 4.f).into_iter();
2198-
auto it2 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2198+
auto it2 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
21992199
EXPECT_EQ(false, sus::move(it1).gt(sus::move(it2)));
22002200
}
22012201
{
2202-
auto it1 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2202+
auto it1 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
22032203
auto it2 = sus::Array<f32, 2>(1.f, 3.f).into_iter();
22042204
EXPECT_EQ(false, sus::move(it1).gt(sus::move(it2)));
22052205
}
@@ -2226,11 +2226,11 @@ TEST(Iterator, Le) {
22262226
}
22272227
{
22282228
auto it1 = sus::Array<f32, 2>(1.f, 4.f).into_iter();
2229-
auto it2 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2229+
auto it2 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
22302230
EXPECT_EQ(false, sus::move(it1).le(sus::move(it2)));
22312231
}
22322232
{
2233-
auto it1 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2233+
auto it1 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
22342234
auto it2 = sus::Array<f32, 2>(1.f, 3.f).into_iter();
22352235
EXPECT_EQ(false, sus::move(it1).le(sus::move(it2)));
22362236
}
@@ -2257,11 +2257,11 @@ TEST(Iterator, Lt) {
22572257
}
22582258
{
22592259
auto it1 = sus::Array<f32, 2>(1.f, 4.f).into_iter();
2260-
auto it2 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2260+
auto it2 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
22612261
EXPECT_EQ(false, sus::move(it1).lt(sus::move(it2)));
22622262
}
22632263
{
2264-
auto it1 = sus::Array<f32, 2>(1.f, f32::NAN).into_iter();
2264+
auto it1 = sus::Array<f32, 2>(1.f, f32::NaN).into_iter();
22652265
auto it2 = sus::Array<f32, 2>(1.f, 3.f).into_iter();
22662266
EXPECT_EQ(false, sus::move(it1).lt(sus::move(it2)));
22672267
}

sus/num/__private/float_consts.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ inline constexpr i32 _self::MIN_EXP = __private::min_exp<_primitive>();
4545
inline constexpr i32 _self::MAX_EXP = __private::max_exp<_primitive>();
4646
inline constexpr i32 _self::MIN_10_EXP = __private::min_10_exp<_primitive>();
4747
inline constexpr i32 _self::MAX_10_EXP = __private::max_10_exp<_primitive>();
48-
inline constexpr _self _self::NAN = _self(__private::nan<_primitive>());
49-
inline constexpr _self _self::INFINITY =
48+
inline constexpr _self _self::NaN = _self(__private::nan<_primitive>());
49+
inline constexpr _self _self::INF =
5050
_self(__private::infinity<_primitive>());
51-
inline constexpr _self _self::NEG_INFINITY =
51+
inline constexpr _self _self::NEG_INF =
5252
_self(__private::negative_infinity<_primitive>());
5353

5454
inline constexpr _self _self::consts::E =

sus/num/__private/float_methods.inc

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,19 @@ static const i32 MAX_10_EXP;
7474
///
7575
/// This value is not constexpr because the value can differ in a constexpr
7676
/// evaluation context from a runtime context, leading to bugs.
77-
static const _self NAN;
77+
///
78+
/// We have chosen the name NaN so that we don't clash with the C++ standard
79+
/// library macro NAN. It's not an ideal name, but is the best we've been able
80+
/// to come up with so far.
81+
static const _self NaN;
7882
/// Infinity.
79-
static const _self INFINITY;
83+
///
84+
/// We have chosen the name INF so that we don't clash with the C++ standard
85+
/// library macro INFINITY. It's not an ideal name, but is the best we've been
86+
/// able to come up with so far.
87+
static const _self INF;
8088
/// Negative infinity.
81-
static const _self NEG_INFINITY;
89+
static const _self NEG_INF;
8290

8391
struct consts {
8492
/// Euler's number (e)
@@ -549,15 +557,15 @@ _sus_pure inline _self abs() const& noexcept {
549557
_sus_pure inline _self acos() const& noexcept {
550558
if (primitive_value < _primitive{-1} || primitive_value > _primitive{1})
551559
[[unlikely]]
552-
return NAN;
560+
return NaN;
553561
// MSVC acos(float) is returning a double for some reason.
554562
return static_cast<_primitive>(::acos(primitive_value));
555563
}
556564
/// Inverse hyperbolic cosine function, or NaN if the number is less than
557565
/// -1.
558566
_sus_pure inline _self acosh() const& noexcept {
559567
if (primitive_value < _primitive{-1}) [[unlikely]]
560-
return NAN;
568+
return NaN;
561569
// MSVC acosh(float) is returning a double for some reason.
562570
return static_cast<_primitive>(::acosh(primitive_value));
563571
}
@@ -566,14 +574,14 @@ _sus_pure inline _self acosh() const& noexcept {
566574
_sus_pure inline _self asin() const& noexcept {
567575
if (primitive_value < _primitive{-1} || primitive_value > _primitive{1})
568576
[[unlikely]]
569-
return NAN;
577+
return NaN;
570578
// MSVC asin(float) is returning a double for some reason.
571579
return static_cast<_primitive>(::asin(primitive_value));
572580
}
573581
/// Inverse hyperbolic sine function.
574582
_sus_pure inline _self asinh() const& noexcept {
575583
if (primitive_value < _primitive{-1}) [[unlikely]]
576-
return NAN;
584+
return NaN;
577585
// MSVC asinh(float) is returning a double for some reason.
578586
return static_cast<_primitive>(::asinh(primitive_value));
579587
}
@@ -721,7 +729,7 @@ _sus_pure inline _self mul_add(_self a, _self b) const& noexcept {
721729

722730
/// Returns the next representable value of the float type after `self` in the
723731
/// direction of `toward`. If `self == toward`, `toward` is returned. If either
724-
/// `self` or `toward` is NAN, NAN is returned.
732+
/// `self` or `toward` is NaN, NaN is returned.
725733
///
726734
/// This is implemented by the
727735
/// [cmath](https://en.cppreference.com/w/c/numeric/math/nextafter) library, see
@@ -875,10 +883,10 @@ _sus_pure inline isize round_to_isize() const& noexcept {
875883
}
876884
/// Returns a number that represents the sign of self.
877885
///
878-
/// * `1.0` if the number is positive, `+0.0` or [`INFINITY`](
879-
/// $sus::num::@doc.self::INFINITY).
886+
/// * `1.0` if the number is positive, `+0.0` or [`INF`](
887+
/// $sus::num::@doc.self::INF).
880888
/// * `-1.0` if the number is negative, `-0.0` or
881-
/// [`NEG_INFINITY`]($sus::num::@doc.self::NEG_INFINITY).
889+
/// [`NEG_INF`]($sus::num::@doc.self::NEG_INF).
882890
/// * `NaN` if the number is `NaN`. The input value is returned exactly,
883891
/// preserving signaling NaNs.
884892
_sus_pure inline _self signum() const& noexcept {
@@ -901,7 +909,7 @@ _sus_pure inline _self sinh() const& noexcept {
901909
/// Returns NaN if self is a negative number other than `-0.0`.
902910
_sus_pure inline _self sqrt() const& noexcept {
903911
if (primitive_value < -_primitive{0}) [[unlikely]]
904-
return NAN;
912+
return NaN;
905913
// MSVC sqrt(float) is returning a double for some reason.
906914
return static_cast<_primitive>(::sqrt(primitive_value));
907915
}

sus/num/__private/intrinsics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,10 +1873,10 @@ __sus_pure_const constexpr inline Out static_cast_to_smaller_float(
18731873
return static_cast<Out>(x); // Handles values in range.
18741874
}
18751875
if (x > T{max_value<Out>()}) {
1876-
return infinity<Out>(); // Handles large values and INFINITY.
1876+
return infinity<Out>(); // Handles large values and INF.
18771877
}
18781878
if (x < T{min_value<Out>()}) {
1879-
return negative_infinity<Out>(); // Handles small values and NEG_INFINITY.
1879+
return negative_infinity<Out>(); // Handles small values and NEG_INF.
18801880
}
18811881
return nan<Out>(); // All that's left are NaNs.
18821882
}

0 commit comments

Comments
 (0)