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
Use Utilities::fixed_power() where possible. #16441
Conversation
For the record - I think it's reasonable to do this for the sake of uniformity but every version of GCC I tested correctly expands |
I still think it's a good idea to do this. It does not cost anything on our part and it ensures that it does what we think it should do. I like this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the comment @drwells pointed out. I think this is a good PR :)!
std::sqrt(std::pow(nrml[i * d1 + j * d2](0), 2.) + | ||
std::pow(nrml[i * d1 + j * d2](1), 2.) + | ||
std::pow(nrml[i * d1 + j * d2](2), 2.)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@drwells I think this is an example where std::pow and Utilities::fixed_power will be different. std::pow(x,2.) does not expand to x*x as far as I remember trying. You need to have std::pow(x,2) with an integer. I think this PR makes this a lot clearer. I prefer expliciting the fixed_power.
<
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, you could alternatively also use std::hypot
(works in 3D since C++17).
source/grid/grid_out.cc
Outdated
const double distance_to_camera = | ||
std::sqrt(std::pow(point[0] - camera_position[0], 2.) + | ||
std::pow(point[1] - camera_position[1], 2.) + | ||
std::pow(point[2] - camera_position[2], 2.)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a good candidate for std::hypot
.
const double distance_to_camera = std::sqrt( | ||
std::pow(point[0] - camera_position[0], 2.) + | ||
std::pow(point[1] - camera_position[1], 2.) + | ||
std::pow(point[2] - camera_position[2], 2.)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Good points about the use of |
Technically, with #16455 in mind, we can also write these
and
But let's handle that in a follow-up PR. |
None of these are in performance critical places, and since it's easier (and shorter) to read, let's leave these in if you don't mind too much. |
@drwells If you give your okay for this PR we can merge it |
@drwells Ping? |
This addresses the majority of what #13321 is about. In particular, it replaces the expensive calls to
std::pow()
byUtilities::pow()
if the exponent is known and an integer.