Skip to content

Commit

Permalink
Complete full set of math constants
Browse files Browse the repository at this point in the history
  • Loading branch information
AyiStar committed Dec 4, 2023
1 parent 0a6c454 commit 83dc9d5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
59 changes: 58 additions & 1 deletion libopenage/util/fixed_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,73 @@ class FixedPoint {
}

/**
* Constants
* Math constants represented in FixedPoint
*/
// naming, definition and value are kept compatible with `math_constants.h`
static constexpr FixedPoint e() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(6267931151224907085ll));
}

static constexpr FixedPoint log2e() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(3326628274461080622ll));
}

static constexpr FixedPoint log10e() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(1001414895036696345ll));
}

static constexpr FixedPoint ln2() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(1598288580650331957ll));
}

static constexpr FixedPoint ln10() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(5309399739799983627ll));
}

static constexpr FixedPoint pi() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(7244019458077122842ll));
}

static constexpr FixedPoint pi_2() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(3622009729038561421ll));
}

static constexpr FixedPoint pi_4() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(1811004864519280710ll));
}

static constexpr FixedPoint inv_pi() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(733972625820500306ll));
}

static constexpr FixedPoint inv2_pi() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(1467945251641000613ll));
}

static constexpr FixedPoint inv2_sqrt_pi() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(2601865214189558307ll));
}

static constexpr FixedPoint tau() {
return from_fixedpoint(FixedPoint<int64_t, 60>::from_raw_value(7244019458077122842ll));
}

static constexpr FixedPoint degs_per_rad() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(40244552544872904ll));
}

static constexpr FixedPoint rads_per_deg() {
return from_fixedpoint(FixedPoint<int64_t, 57>::from_raw_value(8257192040480628449ll));
}

static constexpr FixedPoint sqrt_2() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(3260954456333195553ll));
}

static constexpr FixedPoint inv_sqrt_2() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(1630477228166597776ll));
}

/**
* Factory function to get a fixed-point number from an integer.
*/
Expand Down
15 changes: 15 additions & 0 deletions libopenage/util/fixed_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,22 @@ void fixed_point() {
TESTEQUALS_FLOAT(e.to_double(), 1234.5678, 1e-7);

TESTEQUALS_FLOAT(TestType::e().to_double(), math::E, 1e-7);
TESTEQUALS_FLOAT(TestType::log2e().to_double(), math::LOG2E, 1e-7);
TESTEQUALS_FLOAT(TestType::log10e().to_double(), math::LOG10E, 1e-7);
TESTEQUALS_FLOAT(TestType::ln2().to_double(), math::LN2, 1e-7);
TESTEQUALS_FLOAT(TestType::ln10().to_double(), math::LN10, 1e-7);
TESTEQUALS_FLOAT(TestType::pi().to_double(), math::PI, 1e-7);
TESTEQUALS_FLOAT(TestType::pi_2().to_double(), math::PI_2, 1e-7);
TESTEQUALS_FLOAT(TestType::pi_4().to_double(), math::PI_4, 1e-7);
TESTEQUALS_FLOAT(TestType::inv_pi().to_double(), math::INV_PI, 1e-7);
TESTEQUALS_FLOAT(TestType::inv2_pi().to_double(), math::INV2_PI, 1e-7);
TESTEQUALS_FLOAT(TestType::inv2_sqrt_pi().to_double(), math::INV2_SQRT_PI, 1e-7);
TESTEQUALS_FLOAT(TestType::tau().to_double(), math::TAU, 1e-7);
TESTEQUALS_FLOAT(TestType::degs_per_rad().to_double(), math::DEGSPERRAD, 1e-7);
TESTEQUALS_FLOAT(TestType::rads_per_deg().to_double(), math::RADSPERDEG, 1e-7);
TESTEQUALS_FLOAT(TestType::sqrt_2().to_double(), math::SQRT_2, 1e-7);
TESTEQUALS_FLOAT(TestType::inv_sqrt_2().to_double(), math::INV_SQRT_2, 1e-7);


using TestTypeShort = FixedPoint<int32_t, 16>;
TESTEQUALS_FLOAT(TestTypeShort::e().to_double(), math::E, 1e-3);
Expand Down

0 comments on commit 83dc9d5

Please sign in to comment.