Skip to content

Commit

Permalink
settling on a naming convention for the mathematical constants that i…
Browse files Browse the repository at this point in the history
…s consistent across types
  • Loading branch information
Ravenwater committed Aug 31, 2024
1 parent d6c069a commit 1bd83f2
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 58 deletions.
24 changes: 19 additions & 5 deletions include/universal/math/constants/double_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace sw { namespace universal {
// best practice for C++11
// inject mathematical constants in our namespace

// a_b reads a over b, as in 1_pi being 1 over pi

// double values
constexpr double d_pi_4 = 0.785398163397448309616; // pi/4
constexpr double d_pi_3 = 1.04719755119659774615; // pi/3
Expand All @@ -19,15 +21,27 @@ constexpr double d_pi = 3.14159265358979323846; // pi
constexpr double d_3pi_4 = 4.71238898038468985769; // 3*pi/4
constexpr double d_2pi = 6.28318530717958647693; // 2*pi

constexpr double d_1_pi = 0.318309886183790671538; // 1/pi
constexpr double d_2_pi = 0.636619772367581343076; // 2/pi
constexpr double d_2_sqrtpi = 1.12837916709551257390; // 2/sqrt(pi)
constexpr double d_sqrt2 = 1.41421356237309504880; // sqrt(2)
constexpr double d_sqrt1_2 = 0.707106781186547524401; // 1/sqrt(2)
constexpr double d_phi = 1.61803398874989484820; // phi == golden ratio == most irrational number

constexpr double d_e = 2.71828182845904523536; // e
constexpr double d_log2e = 1.44269504088896340736; // log2(e)
constexpr double d_log10e = 0.434294481903251827651; // log10(e)

constexpr double d_ln2 = 0.693147180559945309417; // ln(2)
constexpr double d_ln10 = 2.30258509299404568402; // ln(10)

constexpr double d_sqrt2 = 1.414213562373095048801; // sqrt(2)
constexpr double d_sqrt3 = 1.732050807568877293527; // sqrt(3)
constexpr double d_sqrt5 = 2.236067977499789696409; // sqrt(5)

constexpr double d_1_phi = 0.618033988749894791503; // 1/phi

constexpr double d_1_e = 0.367879441171442321596; // 1/e

constexpr double d_1_pi = 0.318309886183790671538; // 1/pi
constexpr double d_2_pi = 0.636619772367581343076; // 2/pi

constexpr double d_1_sqrt2 = 0.707106781186547524401; // 1/sqrt(2)
constexpr double d_2_sqrtpi = 1.12837916709551257390; // 2/sqrt(pi) == 1 / sqrt(pi / 4)

}} // namespace sw::universal
8 changes: 6 additions & 2 deletions include/universal/math/constants/float_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace sw { namespace universal {
// best practice for C++11
// inject mathematical constants in our namespace

// double values
// a_b reads a over b, as in 1_pi being 1 over pi

// float values
constexpr double f_pi_4 = 0.785398163f; // pi/4
constexpr double f_pi_3 = 1.047197551f; // pi/3
constexpr double f_pi_2 = 1.570796327f; // pi/2
Expand All @@ -23,10 +25,12 @@ constexpr double f_1_pi = 0.318309886f; // 1/pi
constexpr double f_2_pi = 0.636619772f; // 2/pi
constexpr double f_2_sqrtpi = 1.128379167f; // 2/sqrt(pi)
constexpr double f_sqrt2 = 1.414213562f; // sqrt(2)
constexpr double f_sqrt1_2 = 0.707106781f; // 1/sqrt(2)
constexpr double f_1_sqrt2 = 0.707106781f; // 1/sqrt(2)

constexpr double f_e = 2.718281828f; // e
constexpr double f_log2e = 1.442695041f; // log2(e)
constexpr double f_log10e = 0.434294482f; // log10(e)

constexpr double f_ln2 = 0.693147181f; // ln(2)
constexpr double f_ln10 = 2.302585093f; // ln(10)

Expand Down
16 changes: 16 additions & 0 deletions include/universal/math/constants/longdouble_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace sw { namespace universal {
// best practice for C++11
// inject mathematical constants in our namespace

// a_b reads a over b, as in 1_pi being 1 over pi

// long double values
constexpr long double ld_pi_4 = 0.78539816339744830961566084581988l; // pi/4
constexpr long double ld_pi_3 = 1.0471975511965977461542144610932l; // pi/3
Expand All @@ -19,4 +21,18 @@ constexpr long double ld_pi = 3.1415926535897932384626433832795l; // pi
constexpr long double ld_3pi_4 = 4.7123889803846898576939650749193l; // 3*pi/4
constexpr long double ld_2pi = 6.283185307179586476925286766559l; // 2*pi

// TODO: generate long double constants for the following constants
constexpr long double ld_1_pi = 0.318309886183790671538l; // 1/pi
constexpr long double ld_2_pi = 0.636619772367581343076l; // 2/pi
constexpr long double ld_2_sqrtpi = 1.12837916709551257390l; // 2/sqrt(pi)
constexpr long double ld_sqrt2 = 1.41421356237309504880l; // sqrt(2)
constexpr long double ld_1_sqrt2 = 0.707106781186547524401l; // 1/sqrt(2)

constexpr long double ld_e = 2.71828182845904523536l; // e
constexpr long double ld_log2e = 1.44269504088896340736l; // log2(e)
constexpr long double ld_log10e = 0.434294481903251827651l; // log10(e)

constexpr long double ld_ln2 = 0.693147180559945309417l; // ln(2)
constexpr long double ld_ln10 = 2.30258509299404568402l; // ln(10)

}} // namespace sw::universal
1 change: 1 addition & 0 deletions include/universal/number/dd/dd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

///////////////////////////////////////////////////////////////////////////////////////
/// elementary math functions library
#include <universal/number/dd/math/constants/dd_constants.hpp>
#include <universal/number/dd/mathlib.hpp>
#include <universal/number/dd/mathext.hpp>

Expand Down
28 changes: 0 additions & 28 deletions include/universal/number/dd/dd_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,24 +861,7 @@ class dd {
constexpr int dd_max_precision = 106; // in bits

// simple constants
constexpr dd dd_thousand(1000.0, 0.0);
constexpr dd dd_hundred(100.0, 0.0);
constexpr dd dd_ten(10.0, 0.0);
constexpr dd dd_one(1.0, 0.0);
constexpr dd dd_half(0.5, 0.0);
constexpr dd dd_third(0.33333333333333331, 1.8503717077085941e-17);
constexpr dd dd_fourth(0.25, 0);

// precomputed double-double constants courtesy Scibuilders, Jack Poulson

constexpr dd dd_2pi (6.283185307179586232e+00, 2.449293598294706414e-16);
constexpr dd dd_pi (3.141592653589793116e+00, 1.224646799147353207e-16);
constexpr dd dd_pi2 (1.570796326794896558e+00, 6.123233995736766036e-17);
constexpr dd dd_pi4 (7.853981633974482790e-01, 3.061616997868383018e-17);
constexpr dd dd_3pi4 (2.356194490192344837e+00, 9.1848509936051484375e-17);
constexpr dd dd_e (2.718281828459045091e+00, 1.445646891729250158e-16);
constexpr dd dd_log2 (6.931471805599452862e-01, 2.319046813846299558e-17);
constexpr dd dd_log10 (2.302585092994045901e+00, -2.170756223382249351e-16);

constexpr double dd_eps = 4.93038065763132e-32; // 2^-104
constexpr double dd_min_normalized = 2.0041683600089728e-292; // = 2^(-1022 + 53)
Expand All @@ -888,18 +871,7 @@ constexpr dd dd_safe_max(1.7976931080746007281e+308, 9.97920154767359795037e+291

// precomputed double-double constants courtesy of constants example program, Theodore Omtzigt

constexpr dd dd_ln2 (0.69314718055994529e+00, 2.3190468138462996e-17);
constexpr dd dd_ln10 (2.30258509299404590e+00, -2.1707562233822494e-16);
constexpr dd dd_lge (1.44269504088896340e+00, 2.0355273740931027e-17);
constexpr dd dd_lg10 (3.32192809488736220e+00, 1.6616175169735918e-16);
constexpr dd dd_loge (0.43429448190325182e+00, 1.0983196502167652e-17);

constexpr dd dd_sqrt2 (1.41421356237309510e+00, -9.6672933134529122e-17);

constexpr dd dd_inv_pi (0.31830988618379069e+00, -1.9678676675182486e-17);
constexpr dd dd_inv_pi2 (0.63661977236758138e+00, -3.9357353350364972e-17);
constexpr dd dd_inv_e (0.36787944117144233e+00, -1.2428753672788364e-17);
constexpr dd dd_inv_sqrt2(0.70710678118654757e+00, -4.8336466567264561e-17);

//////////////////////// helper functions /////////////////////////////////

Expand Down
52 changes: 52 additions & 0 deletions include/universal/number/dd/math/constants/dd_constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once
// double_constants.hpp: definition of math constants in double precision
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

namespace sw { namespace universal {

// best practice for C++11
// inject mathematical constants in our namespace

// a_b reads a over b, as in 1_pi being 1 over pi

// precomputed double-double constants courtesy Scibuilders, Jack Poulson
constexpr dd dd_pi_4 (7.853981633974482790e-01, 3.061616997868383018e-17); // pi/4
constexpr dd dd_pi_3 (1.570796326794896558e+00, 6.123233995736766036e-17); // pi/2
constexpr dd dd_pi_2 (1.570796326794896558e+00, 6.123233995736766036e-17); // pi/2
constexpr dd dd_pi (3.141592653589793116e+00, 1.224646799147353207e-16); // pi
constexpr dd dd_3pi_4 (2.356194490192344837e+00, 9.1848509936051484375e-17);// 3*pi/4
constexpr dd dd_2pi (6.283185307179586232e+00, 2.449293598294706414e-16); // 2*pi

constexpr dd dd_phi (1.618033988749894848e+00, 0.0); // phi == golden ratio == most irrational number: dd constant TBD

constexpr dd dd_e (2.718281828459045091e+00, 1.445646891729250158e-16); // e
constexpr dd dd_log2 (6.931471805599452862e-01, 2.319046813846299558e-17); // log2(e)
constexpr dd dd_log10 (2.302585092994045901e+00, -2.170756223382249351e-16);// log10(e)

constexpr dd dd_ln2 (0.69314718055994529e+00, 2.3190468138462996e-17); // ln(2)
constexpr dd dd_ln10 (2.30258509299404590e+00, -2.1707562233822494e-16); // ln(10)

constexpr dd dd_lge (1.44269504088896340e+00, 2.0355273740931027e-17);
constexpr dd dd_lg10 (3.32192809488736220e+00, 1.6616175169735918e-16);
constexpr dd dd_loge (0.43429448190325182e+00, 1.0983196502167652e-17);

constexpr dd dd_sqrt2 (1.41421356237309510e+00, -9.6672933134529122e-17); // sqrt(2)
constexpr dd dd_sqrt3 (1.41421356237309510e+00, -9.6672933134529122e-17); // sqrt(3)
constexpr dd dd_sqrt5 (1.41421356237309510e+00, -9.6672933134529122e-17); // sqrt(5)

constexpr dd dd_1_phi (0.61803398874989479e+00, 0.0); // 1/phi: dd constant TBD

constexpr dd dd_1_e (0.36787944117144233e+00, -1.2428753672788364e-17); // 1/e

constexpr dd dd_1_pi (0.31830988618379069e+00, -1.9678676675182486e-17); // 1/pi
constexpr dd dd_2_pi (0.63661977236758138e+00, -3.9357353350364972e-17); // 2/pi == 1 / (pi/2)

constexpr dd dd_1_sqrt2 (0.70710678118654757e+00, -4.8336466567264561e-17); // 1/sqrt(2)
constexpr dd dd_2_sqrtpi (1.12837916709551257e+00, 1.5335459652770156e-17); // 2 / sqrt(pi) = 1 / (sqrt(pi/4)


}} // namespace sw::universal
20 changes: 10 additions & 10 deletions include/universal/number/dd/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ namespace sw { namespace universal {

// approximately reduce modulo pi/2 and then modulo pi/16.
dd t;
double q = std::floor(r.high() / dd_pi2.high() + 0.5);
t = r - dd_pi2 * q;
double q = std::floor(r.high() / dd_pi_2.high() + 0.5);
t = r - dd_pi_2 * q;
int j = static_cast<int>(q);
q = std::floor(t.high() / _pi16.high() + 0.5);
t -= _pi16 * q;
Expand Down Expand Up @@ -185,8 +185,8 @@ namespace sw { namespace universal {

// approximately reduce modulo pi/2 and then modulo pi/16
dd t;
double q = std::floor(r.high() / dd_pi2.high() + 0.5);
t = r - dd_pi2 * q;
double q = std::floor(r.high() / dd_pi_2.high() + 0.5);
t = r - dd_pi_2 * q;
int j = static_cast<int>(q);
q = std::floor(t.high() / _pi16.high() + 0.5);
t -= _pi16 * q;
Expand Down Expand Up @@ -271,8 +271,8 @@ namespace sw { namespace universal {

// approximately reduce module pi/2 and pi/16
dd t;
double q = std::floor(r.high() / dd_pi2.high() + 0.5);
t = r - dd_pi2 * q;
double q = std::floor(r.high() / dd_pi_2.high() + 0.5);
t = r - dd_pi_2 * q;
int j = static_cast<int>(q);
int abs_j = std::abs(j);
q = std::floor(t.high() / _pi16.high() + 0.5);
Expand Down Expand Up @@ -360,18 +360,18 @@ namespace sw { namespace universal {
return dd(SpecificValue::snan);
}

return (y.ispos()) ? dd_pi2 : -dd_pi2;
return (y.ispos()) ? dd_pi_2 : -dd_pi_2;
}
else if (y.iszero()) {
return (x.ispos()) ? dd(0.0) : dd_pi;
}

if (x == y) {
return (y.ispos()) ? dd_pi4 : -dd_3pi4;
return (y.ispos()) ? dd_pi_4 : -dd_3pi_4;
}

if (x == -y) {
return (y.ispos()) ? dd_3pi4 : -dd_pi4;
return (y.ispos()) ? dd_3pi_4 : -dd_pi_4;
}

dd r = sqrt(sqr(x) + sqr(y));
Expand Down Expand Up @@ -415,7 +415,7 @@ namespace sw { namespace universal {
}

if (abs_a.isone()) {
return (a.ispos()) ? dd_pi2 : -dd_pi2;
return (a.ispos()) ? dd_pi_2 : -dd_pi_2;
}

return atan2(a, sqrt(1.0 - sqr(a)));
Expand Down
31 changes: 19 additions & 12 deletions static/dd/api/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ namespace sw {
sw::universal::dd GenerateDoubleDouble(const std::string& str) {
using namespace sw::universal;
dd v(str);
auto oldPrec = std::cout.precision();
auto defaultPrecision = std::cout.precision();
// 53 bits = 16 decimal digits, 17 to include last, 15 typical valid digits
std::cout << std::setprecision(std::numeric_limits<double>::max_digits10);
std::cout << to_pair(v) << '\n';
std::cout << std::setprecision(oldPrec);
std::cout << std::setprecision(defaultPrecision);
return v;
}

void report(const sw::universal::dd& v, int precision = 17) {
auto oldPrec = std::cout.precision();
auto defaultPrecision = std::cout.precision();
std::cout << std::setprecision(precision) << to_pair(v) << " : " << v << '\n';
std::cout << std::setprecision(oldPrec);
std::cout << std::setprecision(defaultPrecision);
}

const dd dd_inv_int[] = {
Expand Down Expand Up @@ -255,8 +255,8 @@ try {
} constant_symbol_table[] = {
{ "dd_2pi", "6.283185307179586476925286766559005768", dd_2pi },
{ "dd_pi" , "3.141592653589793238462643383279502884", dd_pi },
{ "dd_pi2", "1.570796326794896619231321691639751442", dd_pi2 },
{ "dd_pi4", "0.785398163397448309615660845819875721", dd_pi4 },
{ "dd_pi2", "1.570796326794896619231321691639751442", dd_pi_2 },
{ "dd_pi4", "0.785398163397448309615660845819875721", dd_pi_4 },

{ "dd_e" , "2.718281828459045235360287471352662498", dd_e },

Expand All @@ -271,12 +271,19 @@ try {

{ "dd_sqrt2", "1.414213562373095048801688724209698079", dd_sqrt2 },

{ "dd_inv_pi", "0.318309886183790671537767526745028724", dd_inv_pi },
{ "dd_inv_pi2", "0.636619772367581343075535053490057448", dd_inv_pi2 },
{ "dd_inv_e", "0.367879441171442321595523770161460867", dd_inv_e },
{ "dd_inv_sqrt2", "0.707106781186547524400844362104849039", dd_inv_sqrt2 },
{ "dd_1_pi", "0.318309886183790671537767526745028724", dd_2_pi },
{ "dd_2_pi", "0.636619772367581343075535053490057448", dd_2_pi },
{ "dd_1_e", "0.367879441171442321595523770161460867", dd_1_e },
{ "dd_1_sqrt2", "0.707106781186547524400844362104849039", dd_1_sqrt2 },
};

{
// calculate 1 / sqrt(pi / 4)
dd sqrt_pi_4 = sqrt(dd_pi_4);
dd result = reciprocal(sqrt_pi_4);
std::cout << "1 / sqrt(pi / 4) : " << result << '\n';
std::cout << to_pair(result) << '\n';
}
/*
*
* ETLO August 6, 2024
Expand All @@ -300,14 +307,14 @@ dd_inv_pi2 : 6.36619772367581343075535053490057e-01 vs 6.366197723675813430
dd_inv_e : 3.67879441171442321595523770161459e-01 vs 3.67879441171442321595523770161459e-01 : ( 0.36787944117144233, -1.2428753672788364e-17) : 0.00000000000000000000000000000000e+00
dd_inv_sqrt2 : 7.07106781186547524400844362104854e-01 vs 7.07106781186547524400844362104854e-01 : ( 0.70710678118654757, -4.8336466567264561e-17) : 0.00000000000000000000000000000000e+00
*/
auto oldPrec = std::cout.precision();
auto defaultPrecision = std::cout.precision();
std::cout << std::setprecision(32);
for (auto record : constant_symbol_table) {
dd a(record.digits);
dd error = (a - record.value);
std::cout << std::left << std::setw(15) << record.name << " : " << a << " vs " << record.value << " : " << to_pair(a) << " : " << error << '\n';
}
std::cout << std::setprecision(oldPrec);
std::cout << std::setprecision(defaultPrecision);

ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
return EXIT_SUCCESS; // ignore failures
Expand Down
2 changes: 1 addition & 1 deletion static/dd/math/hyperbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ try {

{
std::cout << "double-double reference\n";
dd x = dd_pi4;
dd x = dd_pi_4;
std::cout << "sinh( " << x << " ) = " << sinh(x) << '\n';
std::cout << "cosh( " << x << " ) = " << cosh(x) << '\n';
std::cout << "tanh( " << x << " ) = " << tanh(x) << '\n';
Expand Down
4 changes: 4 additions & 0 deletions static/qd/math/trigonometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <universal/number/qd/qd.hpp>
#include <universal/verification/test_suite.hpp>

#include <universal/math/constants/float_constants.hpp>
#include <universal/math/constants/double_constants.hpp>
#include <universal/math/constants/longdouble_constants.hpp>

template<typename Real>
int VerifySinFunction(bool reportTestCases) {
using std::sin, std::abs;
Expand Down

0 comments on commit 1bd83f2

Please sign in to comment.