-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
How to handle floating point comparison warnings (DO NOT MERGE) #1096
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (C) 2014 PX4 Development Team. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* 3. Neither the name PX4 nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
****************************************************************************/ | ||
|
||
/** | ||
* @file fp_helpers.h | ||
*/ | ||
|
||
#ifndef __FP_HELPERS_H | ||
#define __FP_HELPERS_H | ||
|
||
#include "float.h" | ||
#include "math.h" | ||
|
||
#define is_exactly_zero_float(f) (!isfinite(1.0f/(f))) | ||
#define is_exactly_zero_double(d) (!isfinite(1.0/(d))) | ||
#define is_equal_float(f1, f2) (fabsf((f1)- (f2)) > FLT_EPSILON) | ||
#define is_equal_double(d1, d2) (fabs((d1)- (d2)) > DBL_EPSILON) | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,8 @@ | |
#include <systemlib/perf_counter.h> | ||
#include <systemlib/err.h> | ||
|
||
#include "fp_helpers.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
@@ -267,7 +269,7 @@ void NonlinearSO3AHRSupdate(float gx, float gy, float gz, float ax, float ay, fl | |
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be changed to some sort of flag to signal no mag values. |
||
//! If magnetometer measurement is available, use it. | ||
if(!((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f))) { | ||
if(!(is_exactly_zero_float(mx) && is_exactly_zero_float(my) && is_exactly_zero_float(mz))) { | ||
float hx, hy, hz, bx, bz; | ||
float halfwx, halfwy, halfwz; | ||
|
||
|
@@ -297,7 +299,7 @@ void NonlinearSO3AHRSupdate(float gx, float gy, float gz, float ax, float ay, fl | |
} | ||
|
||
// Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) | ||
if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) { | ||
if(!(is_exactly_zero_float(ax) && is_exactly_zero_float(ay) && is_exactly_zero_float(az))) { | ||
float halfvx, halfvy, halfvz; | ||
|
||
// Normalise accelerometer measurement | ||
|
@@ -318,7 +320,7 @@ void NonlinearSO3AHRSupdate(float gx, float gy, float gz, float ax, float ay, fl | |
} | ||
|
||
// Apply feedback only when valid data has been gathered from the accelerometer or magnetometer | ||
if(halfex != 0.0f && halfey != 0.0f && halfez != 0.0f) { | ||
if(!is_exactly_zero_float(halfex) && !is_exactly_zero_float(halfey) && !is_exactly_zero_float(halfez)) { | ||
// Compute and apply integral feedback if enabled | ||
if(twoKi > 0.0f) { | ||
gyro_bias[0] += twoKi * halfex * dt; // integral error scaled by Ki | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ | |
#include "baro_calibration.h" | ||
#include "rc_calibration.h" | ||
#include "airspeed_calibration.h" | ||
#include "fp_helpers.h" | ||
|
||
/* oddly, ERROR is not defined for c++ */ | ||
#ifdef ERROR | ||
|
@@ -469,7 +470,7 @@ bool handle_command(struct vehicle_status_s *status, const struct safety_s *safe | |
case VEHICLE_CMD_COMPONENT_ARM_DISARM: { | ||
// Follow exactly what the mavlink spec says for values: 0.0f for disarm, 1.0f for arm. | ||
// We use an float epsilon delta to test float equality. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to < 0.5 for arm and > 0.5 for arm? If so, we should change the mavlink spec as well. Old code should continue to work. |
||
if (cmd->param1 != 0.0f && (fabsf(cmd->param1 - 1.0f) > 2.0f * FLT_EPSILON)) { | ||
if (!is_exactly_zero_float(cmd->param1) && is_equal_float(cmd->param1, 1.0f)) { | ||
mavlink_log_info(mavlink_fd, "Unsupported ARM_DISARM parameter: %.6f", cmd->param1); | ||
|
||
} else { | ||
|
@@ -479,7 +480,7 @@ bool handle_command(struct vehicle_status_s *status, const struct safety_s *safe | |
status->arming_state = ARMING_STATE_IN_AIR_RESTORE; | ||
} | ||
|
||
transition_result_t arming_res = arm_disarm(cmd->param1 != 0.0f, mavlink_fd, "arm/disarm component command"); | ||
transition_result_t arming_res = arm_disarm(!is_exactly_zero_float(cmd->param1), mavlink_fd, "arm/disarm component command"); | ||
|
||
if (arming_res == TRANSITION_DENIED) { | ||
mavlink_log_critical(mavlink_fd, "#audio: REJECTING component arm cmd"); | ||
|
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.
Seems like this one should have some sort of specified delta which is used to determine whether the previous versus new sonar values are different enough to care about. Certainly larger than FLT_EPSILON would seem to be too small to care about.