Skip to content

Commit 0c6a8a0

Browse files
Ravenwaterclaude
andauthored
refactor(constants): replace M_* with std::numbers and add precision … (#536)
* refactor(constants): replace M_* with std::numbers and add precision comparison example Replace legacy C-style M_PI, M_PI_4, M_E constants with C++20 std::numbers equivalents across 6 application files, eliminating the non-portable _USE_MATH_DEFINES workaround. Update comment reference tables in 8 files to show std::numbers as the primary API. Remove _USE_MATH_DEFINES from 3 common headers. Add numeric_constants.cpp example comparing pi, e, ln2, ln10, sqrt2, and phi across 15 number systems (cfloat, posit, fixpnt, lns, dd, qd) with ULP and relative error computed against a qd_cascade oracle. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * adding constants as a possible scope --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 416a2a2 commit 0c6a8a0

File tree

19 files changed

+345
-171
lines changed

19 files changed

+345
-171
lines changed

.github/workflows/conventional-commits.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ jobs:
6666
blas
6767
math
6868
numeric
69+
constants
6970
mixedprecision
7071
pop
7172
cmake

applications/accuracy/ode/first_order_ode.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// SPDX-License-Identifier: MIT
55
//
66
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
7-
#define _USE_MATH_DEFINES
87
#include <cmath>
8+
#include <numbers>
99
// Configure the posit library with arithmetic exceptions
1010
// enable posit arithmetic exceptions
1111
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
@@ -106,7 +106,7 @@ try {
106106
using Scalar = float;
107107
Scalar x0 = 0; // initial x
108108
Scalar y0 = 1; // initial y
109-
Scalar h = Scalar(M_PI_4); // step size between intervals
109+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
110110
int n = 4; // number of intervals
111111
std::cout << "\nThe ode is: dy/dx = (5*x*x - y)/exp(x + y)\n" << std::endl;
112112
std::cout << "Using float" << std::endl;
@@ -118,7 +118,7 @@ try {
118118
{ using Scalar = posit<16, 2>;
119119
Scalar x0 = 0; // initial x
120120
Scalar y0 = 1; // initial y
121-
Scalar h = Scalar(M_PI_4); // step size between intervals
121+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
122122
int n = 4; // number of intervals
123123
std::cout << "\nThe ode is: dy/dx = (5*x*x - y)/exp(x + y)\n" << std::endl;
124124
std::cout << "Using posit<16, 1>" << std::endl;
@@ -131,7 +131,7 @@ try {
131131
std::cout << "\nUsing posit<32, 1>" << std::endl;
132132
Scalar x0 = 0; // initial x
133133
Scalar y0 = 1; // initial y
134-
Scalar h = Scalar(M_PI_4); // step size between intervals
134+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
135135
int n = 4; // number of intervals
136136
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + n*h << std::endl;
137137
std::cout << "step size = " << h << std::endl;
@@ -142,7 +142,7 @@ try {
142142
std::cout << "\nUsing posit<64, 1>" << std::endl;
143143
Scalar x0 = 0; // initial x
144144
Scalar y0 = 1; // initial y
145-
Scalar h = Scalar(M_PI_4); // step size between intervals
145+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
146146
int n = 4; // number of intervals
147147
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + n*h << std::endl;
148148
std::cout << "step size = " << h << std::endl;

applications/accuracy/ode/general_runge_kutta.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,27 @@
55
// Author: Jacob Todd jtodd1@une.edu
66
//
77
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
8-
// #define _USE_MATH_DEFINES
9-
// #include <cmath>
108
// Configure the posit library with arithmetic exceptions
119
// enable posit arithmetic exceptions
1210
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
1311
#include <universal/number/posit/posit.hpp>
1412

1513
/*
1614
17-
Mathematical C++ Symbol Decimal Representation
18-
Expression
19-
pi M_PI 3.14159265358979323846
20-
pi/2 M_PI_2 1.57079632679489661923
21-
pi/4 M_PI_4 0.785398163397448309616
22-
1/pi M_1_PI 0.318309886183790671538
23-
2/pi M_2_PI 0.636619772367581343076
24-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
25-
sqrt(2) M_SQRT2 1.41421356237309504880
26-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
27-
e M_E 2.71828182845904523536
28-
log_2(e) M_LOG2E 1.44269504088896340736
29-
log_10(e) M_LOG10E 0.434294481903251827651
30-
log_e(2) M_LN2 0.693147180559945309417
31-
log_e(10) M_LN10 2.30258509299404568402
15+
C++20 <numbers> Equivalent Decimal
16+
std::numbers::pi M_PI 3.14159265358979323846
17+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
18+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
19+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
20+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
21+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
22+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
23+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
24+
std::numbers::e M_E 2.71828182845904523536
25+
std::numbers::log2e M_LOG2E 1.44269504088896340736
26+
std::numbers::log10e M_LOG10E 0.434294481903251827651
27+
std::numbers::ln2 M_LN2 0.693147180559945309417
28+
std::numbers::ln10 M_LN10 2.30258509299404568402
3229
3330
*/
3431

applications/accuracy/ode/runge_kutta4.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55
// Author: Jacob Todd jtodd1@une.edu
66
//
77
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
8-
#define _USE_MATH_DEFINES
98
#include <universal/utility/directives.hpp>
109
#include <cmath>
10+
#include <numbers>
1111
// Configure the posit library with arithmetic exceptions
1212
// enable posit arithmetic exceptions
1313
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
1414
#include <universal/number/posit/posit.hpp>
1515

1616
/*
1717
18-
Mathematical C++ Symbol Decimal Representation
19-
Expression
20-
pi M_PI 3.14159265358979323846
21-
pi/2 M_PI_2 1.57079632679489661923
22-
pi/4 M_PI_4 0.785398163397448309616
23-
1/pi M_1_PI 0.318309886183790671538
24-
2/pi M_2_PI 0.636619772367581343076
25-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
26-
sqrt(2) M_SQRT2 1.41421356237309504880
27-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
28-
e M_E 2.71828182845904523536
29-
log_2(e) M_LOG2E 1.44269504088896340736
30-
log_10(e) M_LOG10E 0.434294481903251827651
31-
log_e(2) M_LN2 0.693147180559945309417
32-
log_e(10) M_LN10 2.30258509299404568402
18+
C++20 <numbers> Equivalent Decimal
19+
std::numbers::pi M_PI 3.14159265358979323846
20+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
21+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
22+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
23+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
24+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
25+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
26+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
27+
std::numbers::e M_E 2.71828182845904523536
28+
std::numbers::log2e M_LOG2E 1.44269504088896340736
29+
std::numbers::log10e M_LOG10E 0.434294481903251827651
30+
std::numbers::ln2 M_LN2 0.693147180559945309417
31+
std::numbers::ln10 M_LN10 2.30258509299404568402
3332
3433
*/
3534

@@ -71,7 +70,7 @@ try {
7170
using Scalar = float;
7271
Scalar x0 = 0; // initial x
7372
Scalar y0 = 1; // initial y
74-
Scalar h = Scalar(M_PI_4); // step size between intervals
73+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
7574
std::cout << "\nThe ode is: dy/dx = (5*x*x - y)/exp(x + y)\n" << std::endl;
7675
std::cout << "Using float" << std::endl;
7776
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + Scalar(N) * h << std::endl;
@@ -82,7 +81,7 @@ try {
8281
{ using Scalar = posit<16, 2>;
8382
Scalar x0 = 0; // initial x
8483
Scalar y0 = 1; // initial y
85-
Scalar h = Scalar(M_PI_4); // step size between intervals
84+
Scalar h = Scalar((std::numbers::pi / 4.0)); // step size between intervals
8685
std::cout << "\nThe ode is: dy/dx = (5*x*x - y)/exp(x + y)\n" << std::endl;
8786
std::cout << "Using float" << std::endl;
8887
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + Scalar(N) * h << std::endl;
@@ -93,7 +92,7 @@ try {
9392
{ using Scalar = posit<16, 2>;
9493
Scalar x0 = 0; // initial x
9594
Scalar y0 = 1; // initial y
96-
double h = M_PI_4; // step size between intervals
95+
double h = (std::numbers::pi / 4.0); // step size between intervals
9796
std::cout << "\nThe ode is: dy/dx = (5*x*x - y)/exp(x + y)\n" << std::endl;
9897
std::cout << "Using posit<16, 1>" << std::endl;
9998
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + double(N)*h << std::endl;
@@ -105,7 +104,7 @@ try {
105104
std::cout << "\nUsing posit<32, 1>" << std::endl;
106105
Scalar x0 = 0; // initial x
107106
Scalar y0 = 1; // initial y
108-
double h = M_PI_4; // step size between intervals
107+
double h = (std::numbers::pi / 4.0); // step size between intervals
109108
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + double(N)*h << std::endl;
110109
std::cout << "step size = " << h << std::endl;
111110
rk4 (&myFunc, N, Scalar(h), x0, y0);
@@ -115,7 +114,7 @@ try {
115114
std::cout << "\nUsing posit<64, 1>" << std::endl;
116115
Scalar x0 = 0; // initial x
117116
Scalar y0 = 1; // initial y
118-
double h = M_PI_4; // step size between intervals
117+
double h = (std::numbers::pi / 4.0); // step size between intervals
119118
std::cout << "Appoximating y(x) from " << x0 << " to " << x0 + double(N)*h << std::endl;
120119
std::cout << "step size = " << h << std::endl;
121120
rk4 (&myFunc, N, Scalar(h), x0, y0);

applications/accuracy/pde/laplace.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@
1515

1616
/*
1717
18-
Mathematical C++ Symbol Decimal Representation
19-
Expression
20-
pi M_PI 3.14159265358979323846
21-
pi/2 M_PI_2 1.57079632679489661923
22-
pi/4 M_PI_4 0.785398163397448309616
23-
1/pi M_1_PI 0.318309886183790671538
24-
2/pi M_2_PI 0.636619772367581343076
25-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
26-
sqrt(2) M_SQRT2 1.41421356237309504880
27-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
28-
e M_E 2.71828182845904523536
29-
log_2(e) M_LOG2E 1.44269504088896340736
30-
log_10(e) M_LOG10E 0.434294481903251827651
31-
log_e(2) M_LN2 0.693147180559945309417
32-
log_e(10) M_LN10 2.30258509299404568402
18+
C++20 <numbers> Equivalent Decimal
19+
std::numbers::pi M_PI 3.14159265358979323846
20+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
21+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
22+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
23+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
24+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
25+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
26+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
27+
std::numbers::e M_E 2.71828182845904523536
28+
std::numbers::log2e M_LOG2E 1.44269504088896340736
29+
std::numbers::log10e M_LOG10E 0.434294481903251827651
30+
std::numbers::ln2 M_LN2 0.693147180559945309417
31+
std::numbers::ln10 M_LN10 2.30258509299404568402
3332
3433
*/
3534

applications/accuracy/roots/bisection.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@
1212

1313
/*
1414
15-
Mathematical C++ Symbol Decimal Representation
16-
Expression
17-
pi M_PI 3.14159265358979323846
18-
pi/2 M_PI_2 1.57079632679489661923
19-
pi/4 M_PI_4 0.785398163397448309616
20-
1/pi M_1_PI 0.318309886183790671538
21-
2/pi M_2_PI 0.636619772367581343076
22-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
23-
sqrt(2) M_SQRT2 1.41421356237309504880
24-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
25-
e M_E 2.71828182845904523536
26-
log_2(e) M_LOG2E 1.44269504088896340736
27-
log_10(e) M_LOG10E 0.434294481903251827651
28-
log_e(2) M_LN2 0.693147180559945309417
29-
log_e(10) M_LN10 2.30258509299404568402
15+
C++20 <numbers> Equivalent Decimal
16+
std::numbers::pi M_PI 3.14159265358979323846
17+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
18+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
19+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
20+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
21+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
22+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
23+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
24+
std::numbers::e M_E 2.71828182845904523536
25+
std::numbers::log2e M_LOG2E 1.44269504088896340736
26+
std::numbers::log10e M_LOG10E 0.434294481903251827651
27+
std::numbers::ln2 M_LN2 0.693147180559945309417
28+
std::numbers::ln10 M_LN10 2.30258509299404568402
3029
3130
*/
3231

applications/approximation/chebyshev/chebtests.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@
1515
// using Matrix = sw::universal::blas::matrix<Scalar>;
1616
/*
1717
18-
Mathematical C++ Symbol Decimal Representation
19-
Expression
20-
pi M_PI 3.14159265358979323846
21-
pi/2 M_PI_2 1.57079632679489661923
22-
pi/4 M_PI_4 0.785398163397448309616
23-
1/pi M_1_PI 0.318309886183790671538
24-
2/pi M_2_PI 0.636619772367581343076
25-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
26-
sqrt(2) M_SQRT2 1.41421356237309504880
27-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
28-
e M_E 2.71828182845904523536
29-
log_2(e) M_LOG2E 1.44269504088896340736
30-
log_10(e) M_LOG10E 0.434294481903251827651
31-
log_e(2) M_LN2 0.693147180559945309417
32-
log_e(10) M_LN10 2.30258509299404568402
18+
C++20 <numbers> Equivalent Decimal
19+
std::numbers::pi M_PI 3.14159265358979323846
20+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
21+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
22+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
23+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
24+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
25+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
26+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
27+
std::numbers::e M_E 2.71828182845904523536
28+
std::numbers::log2e M_LOG2E 1.44269504088896340736
29+
std::numbers::log10e M_LOG10E 0.434294481903251827651
30+
std::numbers::ln2 M_LN2 0.693147180559945309417
31+
std::numbers::ln10 M_LN10 2.30258509299404568402
3332
3433
*/
3534
// Configure the posit library with arithmetic exceptions

applications/approximation/chebyshev/nodes.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@
1212

1313
/*
1414
15-
Mathematical C++ Symbol Decimal Representation
16-
Expression
17-
pi M_PI 3.14159265358979323846
18-
pi/2 M_PI_2 1.57079632679489661923
19-
pi/4 M_PI_4 0.785398163397448309616
20-
1/pi M_1_PI 0.318309886183790671538
21-
2/pi M_2_PI 0.636619772367581343076
22-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
23-
sqrt(2) M_SQRT2 1.41421356237309504880
24-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
25-
e M_E 2.71828182845904523536
26-
log_2(e) M_LOG2E 1.44269504088896340736
27-
log_10(e) M_LOG10E 0.434294481903251827651
28-
log_e(2) M_LN2 0.693147180559945309417
29-
log_e(10) M_LN10 2.30258509299404568402
15+
C++20 <numbers> Equivalent Decimal
16+
std::numbers::pi M_PI 3.14159265358979323846
17+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
18+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
19+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
20+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
21+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
22+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
23+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
24+
std::numbers::e M_E 2.71828182845904523536
25+
std::numbers::log2e M_LOG2E 1.44269504088896340736
26+
std::numbers::log10e M_LOG10E 0.434294481903251827651
27+
std::numbers::ln2 M_LN2 0.693147180559945309417
28+
std::numbers::ln10 M_LN10 2.30258509299404568402
3029
3130
*/
3231

applications/mixed-precision/dsp/adc_mapping.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111

1212
/*
1313
14-
Mathematical C++ Symbol Decimal Representation
15-
Expression
16-
pi M_PI 3.14159265358979323846
17-
pi/2 M_PI_2 1.57079632679489661923
18-
pi/4 M_PI_4 0.785398163397448309616
19-
1/pi M_1_PI 0.318309886183790671538
20-
2/pi M_2_PI 0.636619772367581343076
21-
2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390
22-
sqrt(2) M_SQRT2 1.41421356237309504880
23-
1/sqrt(2) M_SQRT1_2 0.707106781186547524401
24-
e M_E 2.71828182845904523536
25-
log_2(e) M_LOG2E 1.44269504088896340736
26-
log_10(e) M_LOG10E 0.434294481903251827651
27-
log_e(2) M_LN2 0.693147180559945309417
28-
log_e(10) M_LN10 2.30258509299404568402
14+
C++20 <numbers> Equivalent Decimal
15+
std::numbers::pi M_PI 3.14159265358979323846
16+
std::numbers::pi / 2.0 M_PI_2 1.57079632679489661923
17+
std::numbers::pi / 4.0 M_PI_4 0.785398163397448309616
18+
std::numbers::inv_pi M_1_PI 0.318309886183790671538
19+
2.0 * std::numbers::inv_pi M_2_PI 0.636619772367581343076
20+
2.0 * std::numbers::inv_sqrtpi M_2_SQRTPI 1.12837916709551257390
21+
std::numbers::sqrt2 M_SQRT2 1.41421356237309504880
22+
1.0 / std::numbers::sqrt2 M_SQRT1_2 0.707106781186547524401
23+
std::numbers::e M_E 2.71828182845904523536
24+
std::numbers::log2e M_LOG2E 1.44269504088896340736
25+
std::numbers::log10e M_LOG10E 0.434294481903251827651
26+
std::numbers::ln2 M_LN2 0.693147180559945309417
27+
std::numbers::ln10 M_LN10 2.30258509299404568402
2928
3029
*/
3130

applications/mixed-precision/dsp/common.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include <SDKDDKVer.h>
1111
#endif // WINDOWS
1212

13-
// enable the mathematical constants in cmath
14-
#define _USE_MATH_DEFINES
15-
1613
#include <cstdint> // uint8_t, etc.
1714
#include <cmath> // for frexp/frexpf
1815
#include <cfloat> // for DBL_EPSILON, etc.

0 commit comments

Comments
 (0)