Skip to content

Commit

Permalink
try adding two math constants E and PI for FixedPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AyiStar committed Dec 1, 2023
1 parent 2391847 commit 0a6c454
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 21 additions & 4 deletions libopenage/util/fixed_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ constexpr static


/**
* Helper function that performs either a safe shift-right (amount > 0),
* or a safe shift-left (amount < 0).
* Helper function that performs either a safe shift-right (amount < 0),
* or a safe shift-left (amount >= 0).
*/
template <int amount, typename T>
constexpr static
Expand Down Expand Up @@ -169,6 +169,17 @@ class FixedPoint {
return FixedPoint::from_int(0);
}

/**
* Constants
*/
static constexpr FixedPoint e() {
return from_fixedpoint(FixedPoint<int64_t, 61>::from_raw_value(6267931151224907085ll));
}

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

/**
* Factory function to get a fixed-point number from an integer.
*/
Expand All @@ -193,10 +204,16 @@ class FixedPoint {
/**
* Factory function to get a fixed-point number from a fixed-point number of different type.
*/
template <typename other_int_type, unsigned int other_fractional_bits>
template <typename other_int_type, unsigned int other_fractional_bits, typename std::enable_if<(fractional_bits > other_fractional_bits)>::type* = nullptr>
static constexpr FixedPoint from_fixedpoint(const FixedPoint<other_int_type, other_fractional_bits> &other) {
return FixedPoint::from_raw_value(
safe_shift<fractional_bits - other_fractional_bits, int_type>(static_cast<int_type>(other.get_raw_value())));
}

template <typename other_int_type, unsigned int other_fractional_bits, typename std::enable_if<(fractional_bits <= other_fractional_bits)>::type* = nullptr>
static constexpr FixedPoint from_fixedpoint(const FixedPoint<other_int_type, other_fractional_bits> &other) {
return FixedPoint::from_raw_value(
safe_shift<fractional_bits - other_fractional_bits, int_type>(other.get_raw_value()));
static_cast<int_type>(other.get_raw_value() / safe_shiftleft<other_fractional_bits - fractional_bits, other_int_type>(1)));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions libopenage/util/fixed_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../testing/testing.h"
#include "stringformatter.h"
#include "math_constants.h"

namespace openage {
namespace util {
Expand Down Expand Up @@ -98,6 +99,14 @@ void fixed_point() {
std::stringstream sstr("1234.5678");
sstr >> e;
TESTEQUALS_FLOAT(e.to_double(), 1234.5678, 1e-7);

TESTEQUALS_FLOAT(TestType::e().to_double(), math::E, 1e-7);
TESTEQUALS_FLOAT(TestType::pi().to_double(), math::PI, 1e-7);

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

}

}}} // openage::util::tests

0 comments on commit 0a6c454

Please sign in to comment.