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

Replace fabsF macro with std::abs call to avoid having float / double mismatch warnings #28642

Closed
wants to merge 1 commit into from

Conversation

katzfey
Copy link
Contributor

@katzfey katzfey commented Nov 15, 2024

The fabsF macro is defined as either the float version or double version in ftype.h depending on HAL_WITH_EKF_DOUBLE. If it is defined as the float version and a double is used as the argument then we get a compiler warning in the Qurt build. If those are replaced with std:abs calls then that clears up the warnings since it is overloaded for both floats and doubles.

…oat / double mismatch warnings

AP_Math: Replace fabsF macro with std::abs call in templates to avoid having float / double mismatch warnings
@katzfey
Copy link
Contributor Author

katzfey commented Dec 16, 2024

Perhaps fabsF should be removed everywhere and replaced by std::abs?

@@ -733,7 +733,7 @@ void ModeGuided::pos_control_run()

float pos_offset_z_buffer = 0.0; // Vertical buffer size in m
if (guided_pos_terrain_alt) {
pos_offset_z_buffer = MIN(copter.wp_nav->get_terrain_margin() * 100.0, 0.5 * fabsF(guided_pos_target_cm.z));
pos_offset_z_buffer = MIN(copter.wp_nav->get_terrain_margin() * 100.0, 0.5 * std::abs(guided_pos_target_cm.z));
Copy link
Contributor

Choose a reason for hiding this comment

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

if the variable is a float, will it promote to double by making this change? That would slow things a lot on older boards

@tridge
Copy link
Contributor

tridge commented Dec 16, 2024

cppreference shows only integer versions?
image

@tridge
Copy link
Contributor

tridge commented Dec 16, 2024

we could add a float cast to the float version of fabsF()
also note that we generally trying to avoid the std library, it often causes more pain than it fixes

@katzfey
Copy link
Contributor Author

katzfey commented Dec 17, 2024

The float versions are here: https://en.cppreference.com/w/cpp/numeric/math/fabs

@katzfey
Copy link
Contributor Author

katzfey commented Dec 17, 2024

But if you want to avoid using std library then let me try with the float cast

@andyp1per
Copy link
Collaborator

depending on HAL_WITH_EKF_DOUBLE. If it is defined as the float version and a double is used as the argument then we get a compiler warning in the Qurt build. If those are replaced with std:abs calls then that clears up the warnings since it is overloaded for both floats and doubles

I thought this was the point - it's a feature not a bug. We want the right version that has been configured, using abs removes the type-safety

@katzfey
Copy link
Contributor Author

katzfey commented Dec 17, 2024

Closing this PR. Going with #28897 instead with the cast method instead of the switch to std::abs proposed in this PR.

@katzfey katzfey closed this Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants