hypot function

dynamic hypot(
  1. dynamic x,
  2. dynamic y
)

Returns the hypotenuse or Euclidean norm, sqrt(xx + yy).

Example:

print(hypot(3, 4));  // Output: 5.0
print(hypot(Complex(3, 0), Complex(0, 4))); // Output: 5.0

Implementation

dynamic hypot(dynamic x, dynamic y) {
  if (x is num && y is num) {
    return math.sqrt(x * x + y * y);
  } else {
    // Convert to Complex if needed
    Complex cx = x is Complex ? x : Complex(x, 0);
    Complex cy = y is Complex ? y : Complex(y, 0);

    // Calculate x² + y²
    Complex sumOfSquares = (cx * cx) + (cy * cy);

    // Take the square root and simplify
    return sqrt(sumOfSquares).simplify();
  }
}