Browse Source

More precise double Math constants.

Double has 15-17 significant decimal digits precision, extended
the constant to have 15 decimal digits. On the other hand, float has
only 6-9 digits, so there is no need to have more than 9.

Added just-to-be-sure test for sqrt* constants.
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
51f2478b1e
  1. 12
      src/Math/Math.h
  2. 11
      src/Math/Test/MathTest.cpp
  3. 1
      src/Math/Test/MathTest.h

12
src/Math/Math.h

@ -50,14 +50,14 @@ template<class T> struct Constants {
#ifndef DOXYGEN_GENERATING_OUTPUT
template<> struct Constants<double> {
static inline constexpr double pi() { return 3.14159265359; }
static inline constexpr double sqrt2() { return 1.41421356237; }
static inline constexpr double sqrt3() { return 1.73205080757; }
static inline constexpr double pi() { return 3.141592653589793; }
static inline constexpr double sqrt2() { return 1.414213562373095; }
static inline constexpr double sqrt3() { return 1.732050807568877; }
};
template<> struct Constants<float> {
static inline constexpr float pi() { return 3.14159265359f; }
static inline constexpr float sqrt2() { return 1.41421356237f; }
static inline constexpr float sqrt3() { return 1.73205080757f; }
static inline constexpr float pi() { return 3.141592654f; }
static inline constexpr float sqrt2() { return 1.414213562f; }
static inline constexpr float sqrt3() { return 1.732050808f; }
};
namespace Implementation {

11
src/Math/Test/MathTest.cpp

@ -24,13 +24,22 @@ CORRADE_TEST_MAIN(Magnum::Math::Test::MathTest)
namespace Magnum { namespace Math { namespace Test {
MathTest::MathTest() {
addTests(&MathTest::degrad,
addTests(&MathTest::constants,
&MathTest::degrad,
&MathTest::normalize,
&MathTest::denormalize,
&MathTest::pow,
&MathTest::log);
}
void MathTest::constants() {
CORRADE_COMPARE(Math::pow<2>(Constants<float>::sqrt2()), 2.0f);
CORRADE_COMPARE(Math::pow<2>(Constants<float>::sqrt3()), 3.0f);
CORRADE_COMPARE(Math::pow<2>(Constants<double>::sqrt2()), 2.0);
CORRADE_COMPARE(Math::pow<2>(Constants<double>::sqrt3()), 3.0);
}
void MathTest::degrad() {
CORRADE_COMPARE(deg(90.0), Constants<double>::pi()/2);
CORRADE_COMPARE(deg(90.0f), Constants<float>::pi()/2);

1
src/Math/Test/MathTest.h

@ -23,6 +23,7 @@ class MathTest: public Corrade::TestSuite::Tester<MathTest> {
public:
MathTest();
void constants();
void degrad();
void normalize();
void denormalize();

Loading…
Cancel
Save