Skip to content

[ADD] fixed point operations pow and log ALSO fixed point test cases #1765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ _the openage authors_ are:
| Ana Trias-Labellarte | anatriaslabella | ana dawt triaslabella à ufl dawt edu |
| Eelco Empting | Eeelco | me à eelco dawt de |
| Jordan Sutton | jsutCodes | jsutcodes à gmail dawt com |
| Pablo Roizo | PRDeving | pablo dawt deving à gmail dawt com |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
18 changes: 18 additions & 0 deletions libopenage/util/fixed_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ class FixedPoint {
constexpr double tan() {
return std::tan(this->to_double());
}

constexpr double pow(const FixedPoint &exponent) {
return std::pow(this->to_double(), exponent.to_double());
}

constexpr double log() {
return std::log(this->to_double());
}
};


Expand Down Expand Up @@ -597,6 +605,16 @@ constexpr double tan(openage::util::FixedPoint<I, F, Inter> n) {
return n.tan();
}

template <typename I, unsigned F, typename Inter>
constexpr double pow(openage::util::FixedPoint<I, F, Inter> n, openage::util::FixedPoint<I, F, Inter> exponent) {
return n.pow(exponent);
}

template <typename I, unsigned F, typename Inter>
constexpr double log(openage::util::FixedPoint<I, F, Inter> n) {
return n.log();
}

template <typename I, unsigned F, typename Inter>
constexpr openage::util::FixedPoint<I, F, Inter> min(openage::util::FixedPoint<I, F, Inter> x, openage::util::FixedPoint<I, F, Inter> y) {
return openage::util::FixedPoint<I, F, Inter>::from_raw_value(
Expand Down
6 changes: 6 additions & 0 deletions libopenage/util/fixed_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void fixed_point() {

TestType e(120.7);
TestType f(-12.4);
TestType g(3.0);
e += f;

TESTEQUALS_FLOAT(e.to_double(), 108.3, 1e-7);
Expand All @@ -64,6 +65,11 @@ void fixed_point() {
TESTEQUALS_FLOAT(std::hypot(e, f), hypot(108.3, -12.4), 1e-7);
TESTEQUALS_FLOAT(std::min(e, f), -12.4, 1e-7);
TESTEQUALS_FLOAT(std::max(e, f), 108.3, 1e-7);
TESTEQUALS_FLOAT(std::sin(e), e.sin(), 1e-7);
TESTEQUALS_FLOAT(std::cos(e), e.cos(), 1e-7);
TESTEQUALS_FLOAT(std::tan(e), e.tan(), 1e-7);
TESTEQUALS_FLOAT(std::pow(e, g), e.pow(g), 1e-7);
TESTEQUALS_FLOAT(std::log(e), e.log(), 1e-7);
TESTEQUALS_FLOAT(TestType::min_value().to_double(), -2147483648.0, 1e-7);
TESTEQUALS_FLOAT(TestType::max_value().to_double(), 2147483648.0, 1e-7);

Expand Down
Loading