ardupilot/libraries/AP_Math
Lucas De Marchi e0a0514c79 AP_Math: turn MIN/MAX macros into inline functions
The problem with the current MIN/MAX macros is that they evaluate twice
the arguments. For example, these cases provide unintended results:

	// a is incremented twice
	a = MIN(a++, b);
	// foo() with side-effects
	a = MIN(foo(), b);

The alternative implementation here was provided by Daniel Frenzel using
template function. It doesn't have type safety as std::min and std::max,
but adding type safety would mean to check case by case what would be a
reasonable type and add proper casts. Here the arguments for MIN and MAX
can have different types and the return type is deduced from the
expression in the function.

Inspecting the current callers no place was found with the unintended
results above, but some in which now we don't calculate twice the
parameters will benefit from this new version. Examples:

	float velocity_max = MIN(_pos_control.get_speed_xy(), safe_sqrt(0.5f*_pos_control.get_accel_xy()*_radius));

	float acro_level_mix = constrain_float(1-MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0, 0, 1)*ahrs.cos_pitch()

	accel_x_cmss = (GRAVITY_MSS * 100) * (-(_ahrs.cos_yaw() * _ahrs.sin_pitch() / MAX(_ahrs.cos_pitch(),0.5f)) - _ahrs.sin_yaw() * _ahrs.sin_roll() / MAX(_ahrs.cos_roll(),0.5f));

	track_leash_slack = MIN(_track_leash_length*(leash_z-track_error_z)/leash_z, _track_leash_length*(leash_xy-track_error_xy)/leash_xy);

	RC_Channel_aux::move_servo(RC_Channel_aux::k_sprayer_pump, MIN(MAX(ground_speed * _pump_pct_1ms, 100 *_pump_min_pct),10000),0,10000);
2015-12-01 16:28:18 -02:00
..
examples AP_Math: examples use millis/micros/panic functions 2015-11-20 12:31:55 +09:00
AP_Math.cpp AP_Math: Move simple math function implementations to header for better 2015-09-09 09:57:51 +10:00
AP_Math.h AP_Math: turn MIN/MAX macros into inline functions 2015-12-01 16:28:18 -02:00
edc.cpp Minimize AP_Progmem.h includes 2015-10-30 14:35:32 +09:00
edc.h Minimize AP_Progmem.h includes 2015-10-30 14:35:32 +09:00
keywords.txt Beginnings of a math library for ArduPilot(Mega) systems. 2010-09-08 08:21:46 +00:00
location.cpp AP_Math: remove check for AVR CPUs 2015-11-04 12:14:14 +11:00
matrix3.cpp AP_Math: remove check for AVR CPUs 2015-11-04 12:14:14 +11:00
matrix3.h AP_Math: remove check for AVR CPUs 2015-11-04 12:14:14 +11:00
matrix_alg.cpp AP_Math: added O3 optimisation to core math libraries 2015-10-20 14:36:53 +11:00
polygon.cpp uncrustify libraries/AP_Math/polygon.cpp 2012-08-21 19:03:34 -07:00
polygon.h uncrustify libraries/AP_Math/polygon.h 2012-08-21 19:03:34 -07:00
quaternion.cpp AP_Math: added O3 optimisation to core math libraries 2015-10-20 14:36:53 +11:00
quaternion.h AP_Math: use matrix3 euler312 operations in quaternion library 2015-05-25 09:08:31 +10:00
rotations.h AP_Math: change ROTATION_YAW_293_PITCH_68_ROLL_180 to ROLL_90 2015-01-02 17:15:18 +09:00
vector2.cpp AP_Math: added O3 optimisation to core math libraries 2015-10-20 14:36:53 +11:00
vector2.h Math: add Vector2 is_zero method 2015-09-16 15:10:25 +09:00
vector3.cpp AP_Math: remove check for AVR CPUs 2015-11-04 12:14:14 +11:00
vector3.h AP_Math: remove check for AVR CPUs 2015-11-04 12:14:14 +11:00
vectorN.h AP_Math: Changes to fix the warnings in rover sitl build. 2015-02-11 18:16:46 +11:00