Math Functions

Lesson 85: Using sqrt() Function

Learn how sqrt() finds square roots for distance, geometry, and formula-based embedded calculations.

Progress indicator

Lesson 85 of 85

Learning Objectives

  • Understand what sqrt() does mathematically and why square-root transformation is useful.
  • Learn sqrt() syntax and where root-based formulas appear in embedded logic.
  • Use sqrt() for distance/magnitude calculations from multiple components.
  • Understand sqrt() domain rules, numeric types, and conversion behavior.
  • Avoid common mistakes such as negative-domain inputs and precision assumptions.

Concept Explanation

What is sqrt()

sqrt() returns the square root of a value. Square root means the number that, when multiplied by itself, gives the original value.

Example: because 4 x 4 = 16, sqrt(16) returns 4. This helps convert squared formulas back into linear units.

sqrt() Syntax

Basic syntax: sqrt(x)

  • sqrt(9) returns 3.
  • sqrt(2) returns approximately 1.414.
  • sqrt(0) returns 0.

sqrt() is commonly used after sq() or squared-sum formulas.

How sqrt() Works

  1. Accept a numeric input value.
  2. Compute principal (non-negative) square root.
  3. Return floating-point result.

The returned root is always the principal non-negative root in standard usage.

Square Root Concept

Square root is commonly used to convert squared sums back into a linear magnitude, such as distance from two-axis sensor data.

A classic pattern is sqrt(x*x + y*y) for 2D magnitude. This appears in motion, joystick, and orientation logic.

sqrt() with Different Data Types

  • Input may be int or float, but result is floating-point.
  • Integer inputs are converted internally during calculation.
  • Cast carefully if you need integer-only final values.

For tight control loops, keep intermediate calculations in float and convert only near final hardware output stage.

When to Use sqrt()

  • Distance or magnitude formulas (for example x/y components).
  • Signal/physics style equations that involve squared values.
  • Converting squared error terms into human-readable scale.
  • Normalizing multi-axis inputs before threshold decisions.

Example Code

This example computes 2D magnitude with sqrt() and maps it to LED brightness.

const int LED_PIN = 2;
float axisX = 0.0;
float axisY = 0.0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // Simulated 2D input movement.
  axisX = axisX + 0.8;
  axisY = axisY + 0.5;
  if (axisX > 8.0) axisX = 0.0;
  if (axisY > 8.0) axisY = 0.0;

  float magnitude = sqrt(axisX * axisX + axisY * axisY);
  int pwmValue = (int)(magnitude * 30.0);
  pwmValue = constrain(pwmValue, 0, 255);

  analogWrite(LED_PIN, pwmValue);

  Serial.print("x: ");
  Serial.print(axisX, 2);
  Serial.print(" | y: ");
  Serial.print(axisY, 2);
  Serial.print(" | sqrt(x^2+y^2): ");
  Serial.print(magnitude, 3);
  Serial.print(" | pwm: ");
  Serial.println(pwmValue);

  delay(700);
}

Example Code Explanation

  1. axisX and axisY are incremented each loop to simulate 2D movement.
  2. Reset conditions keep values in a repeating range for consistent observation.
  3. axisX*axisX + axisY*axisY computes squared magnitude components.
  4. sqrt(...) converts squared sum into linear magnitude value.
  5. Magnitude is scaled into PWM range and clamped for output safety.
  6. analogWrite() drives LED brightness according to calculated magnitude.
  7. Serial logs show full formula stages for verification.

What Happens Inside

  1. Squared terms are calculated first from each axis component.
  2. Sum of squares is passed to sqrt() math routine.
  3. Floating-point root result is computed and returned.
  4. Result is transformed into control output and written to hardware.
  5. Serial output exposes intermediate values to debug math pipeline behavior.

Common Mistakes with sqrt()

  • Passing negative values to sqrt() without validating input domain.
  • Assuming integer precision for results that are floating-point.
  • Not scaling/constraining root result before hardware output.
  • Mixing units in x/y components before applying sqrt() distance formula.
  • Ignoring computational cost in very high-frequency loops.

Best Practices for sqrt()

  • Validate or clamp input before sqrt() if negatives are possible.
  • Use float variables for intermediate root calculations.
  • Log intermediate math values while tuning formulas.
  • Keep formula terms in consistent units before applying sqrt().
  • Use update intervals/delays to balance math cost and responsiveness.

Try it now

Open the simulator workspace and observe sqrt() magnitude mapping from x/y components.

Run in Simulator