diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 140dd5def..93854b629 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -3596,7 +3596,7 @@ inline void gcode_G28() { * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z */ - int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points; + int abl2 = sq(auto_bed_leveling_grid_points); double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations eqnBVector[abl2], // "B" vector of Z points @@ -3629,7 +3629,7 @@ inline void gcode_G28() { #if ENABLED(DELTA) // Avoid probing the corners (outside the round or hexagon print surface) on a delta printer. - float distance_from_center = sqrt(xProbe * xProbe + yProbe * yProbe); + float distance_from_center = HYPOT(xProbe, yProbe); if (distance_from_center > DELTA_PROBEABLE_RADIUS) continue; #endif //DELTA @@ -4252,7 +4252,7 @@ inline void gcode_M42() { return; } #else - if (sqrt(X_probe_location * X_probe_location + Y_probe_location * Y_probe_location) > DELTA_PROBEABLE_RADIUS) { + if (HYPOT(X_probe_location, Y_probe_location) > DELTA_PROBEABLE_RADIUS) { SERIAL_PROTOCOLLNPGM("? (X,Y) location outside of probeable radius."); return; } @@ -4342,7 +4342,7 @@ inline void gcode_M42() { #else // If we have gone out too far, we can do a simple fix and scale the numbers // back in closer to the origin. - while (sqrt(X_current * X_current + Y_current * Y_current) > DELTA_PROBEABLE_RADIUS) { + while (HYPOT(X_current, Y_current) > DELTA_PROBEABLE_RADIUS) { X_current /= 1.25; Y_current /= 1.25; if (verbose_level > 3) { @@ -4378,10 +4378,9 @@ inline void gcode_M42() { * data points we have so far */ sum = 0.0; - for (uint8_t j = 0; j <= n; j++) { - float ss = sample_set[j] - mean; - sum += ss * ss; - } + for (uint8_t j = 0; j <= n; j++) + sum += sq(sample_set[j] - mean); + sigma = sqrt(sum / (n + 1)); if (verbose_level > 0) { if (verbose_level > 1) { @@ -8139,7 +8138,7 @@ void prepare_move_to_destination() { * This is important when there are successive arc motions. */ // Vector rotation matrix values - float cos_T = 1 - 0.5 * theta_per_segment * theta_per_segment; // Small angle approximation + float cos_T = 1 - 0.5 * sq(theta_per_segment); // Small angle approximation float sin_T = theta_per_segment; float arc_target[NUM_AXIS]; diff --git a/Marlin/macros.h b/Marlin/macros.h index 7dbadee08..bf2d07180 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -36,6 +36,7 @@ // Macros for maths shortcuts #define RADIANS(d) ((d)*M_PI/180.0) #define DEGREES(r) ((r)*180.0/M_PI) +#define HYPOT(x,y) sqrt(sq(x)+sq(y)) // Macros to contrain values #define NOLESS(v,n) do{ if (v < n) v = n; }while(0) diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp index deda9f3df..03796100e 100644 --- a/Marlin/planner.cpp +++ b/Marlin/planner.cpp @@ -171,8 +171,8 @@ void Planner::calculate_trapezoid_for_block(block_t* block, float entry_factor, } #if ENABLED(ADVANCE) - volatile long initial_advance = block->advance * entry_factor * entry_factor; - volatile long final_advance = block->advance * exit_factor * exit_factor; + volatile long initial_advance = block->advance * sq(entry_factor); + volatile long final_advance = block->advance * sq(exit_factor); #endif // ADVANCE // block->accelerate_until = accelerate_steps; @@ -815,13 +815,13 @@ void Planner::check_axes_activity() { else { block->millimeters = sqrt( #if ENABLED(COREXY) - square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_AXIS]) + sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_AXIS]) #elif ENABLED(COREXZ) - square(delta_mm[X_HEAD]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_HEAD]) + sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_HEAD]) #elif ENABLED(COREYZ) - square(delta_mm[X_AXIS]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_HEAD]) + sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_HEAD]) #else - square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS]) + sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_AXIS]) #endif ); } @@ -1030,7 +1030,7 @@ void Planner::check_axes_activity() { dsy = current_speed[Y_AXIS] - previous_speed[Y_AXIS], dsz = fabs(csz - previous_speed[Z_AXIS]), dse = fabs(cse - previous_speed[E_AXIS]), - jerk = sqrt(dsx * dsx + dsy * dsy); + jerk = HYPOT(dsx, dsy); // if ((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) { vmax_junction = block->nominal_speed; @@ -1086,7 +1086,7 @@ void Planner::check_axes_activity() { } else { long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_steps_per_s2); - float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * (cse * cse * (EXTRUSION_AREA) * (EXTRUSION_AREA)) * 256; + float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * HYPOT(cse, EXTRUSION_AREA) * 256; block->advance = advance; block->advance_rate = acc_dist ? advance / (float)acc_dist : 0; } diff --git a/Marlin/planner.h b/Marlin/planner.h index 74fa3592e..74abd1cb2 100644 --- a/Marlin/planner.h +++ b/Marlin/planner.h @@ -290,7 +290,7 @@ class Planner { */ static float estimate_acceleration_distance(float initial_rate, float target_rate, float accel) { if (accel == 0) return 0; // accel was 0, set acceleration distance to 0 - return (target_rate * target_rate - initial_rate * initial_rate) / (accel * 2); + return (sq(target_rate) - sq(initial_rate)) / (accel * 2); } /** @@ -303,7 +303,7 @@ class Planner { */ static float intersection_distance(float initial_rate, float final_rate, float accel, float distance) { if (accel == 0) return 0; // accel was 0, set intersection distance to 0 - return (accel * 2 * distance - initial_rate * initial_rate + final_rate * final_rate) / (accel * 4); + return (accel * 2 * distance - sq(initial_rate) + sq(final_rate)) / (accel * 4); } /** @@ -312,7 +312,7 @@ class Planner { * 'distance'. */ static float max_allowable_speed(float accel, float target_velocity, float distance) { - return sqrt(target_velocity * target_velocity - 2 * accel * distance); + return sqrt(sq(target_velocity) - 2 * accel * distance); } static void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor); diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 4fd6d5eb8..3e933ee89 100755 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1356,7 +1356,7 @@ void kill_screen(const char* lcd_msg) { } #if ENABLED(DELTA) static float delta_clip_radius_2 = (DELTA_PRINTABLE_RADIUS) * (DELTA_PRINTABLE_RADIUS); - static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - a*a); } + static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - sq(a)); } static void lcd_move_x() { int clip = delta_clip(current_position[Y_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_X), X_AXIS, max(sw_endstop_min[X_AXIS], -clip), min(sw_endstop_max[X_AXIS], clip)); } static void lcd_move_y() { int clip = delta_clip(current_position[X_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_Y), Y_AXIS, max(sw_endstop_min[Y_AXIS], -clip), min(sw_endstop_max[Y_AXIS], clip)); } #else