Skip to content
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

Added pow function fpm #1621

Closed
wants to merge 1 commit into from
Closed
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file would need to be removed

Empty file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one

Empty file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one

Empty file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one

Empty file.
13 changes: 13 additions & 0 deletions libopenage/util/fixed_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,19 @@ constexpr double hypot(openage::util::FixedPoint<I, F> x, openage::util::FixedPo
return x.hypot(y);
}

template <typename I, unsigned F>
constexpr openage::util::FixedPoint<I, F> pow(openage::util::FixedPoint<I, F> base, double exponent) {
// Handle special cases: 0^0, 1^x, x^0
if (base == 0.0 && exponent == 0.0) {
return openage::util::FixedPoint<I, F>::quiet_NaN(); // Undefined; return NaN
} else if (base == 1.0 || exponent == 0.0) {
Comment on lines +595 to +597
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why compare to doubles in the if conditions when we could compre to fixed point values instead?

return openage::util::FixedPoint<I, F>::from_raw_value(1);
}
double log_base = std::log(base.get_raw_value());
double result = exponent * log_base;
Comment on lines +600 to +601
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pure fixed-point math functions should avoid any floating point arithmetic. Avoiding the floating point rounding erros is the point of using fp math after all.

return openage::util::FixedPoint<I, F>::from_raw_value(std::exp(result));
}

template <typename I, unsigned F>
struct hash<openage::util::FixedPoint<I, F>> {
constexpr size_t operator()(const openage::util::FixedPoint<I, F> &n) const {
Expand Down