- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ cmath hypot() Function
The C++ cmath hypot() function is used to calculate the length of the hypotenuse of a right-angled triangle, using the Pythagorean theorem. This function takes two side lengths as arguments and returns the square root of the sum of their squares.
This function provides accurate calculations by avoiding overflow and underflow issues, which occurs when manually squaring and calculating the large or small floating-point numbers.
Syntax
Following is the syntax for C++ cmath hypot() function.
double hypot(double x, double y); or float hypot(float x, float y); or long double hypot(long double x, long double y);
Parameters
x - The length of one side of the right-angled triangle.
y - The length of another side of the right-angled triangle.
Return Value
The function returns the square root of (x2+y2)
Time Complexity
The time complexity of this function is constant, i.e.,O(1).
Example 1
The following example shows the basic usage of hypot(), by calculating hypotenuse length for a triangle with perpendicular sides
#include <iostream>
#include <cmath>
int main() {
double x = 3.0, y = 4.0;
double result = std::hypot(x, y);
std::cout << "Hypotenuse for sides 3 and 4 is: " << result << std::endl;
return 0;
}
Output
Output of the above code is as follows
Hypotenuse for sides 3 and 4 is: 5
Example 2
In this example, we are going to calculate the hypotenuse for sides having negative values.
#include <iostream>
#include <cmath>
int main() {
double x = -7.0, y = 24.0;
double result = std::hypot(x, y);
std::cout << "Hypotenuse for sides -7.0 and 24.0 is: " << result << std::endl;
return 0;
}
Output
Following is the output of the above code
Hypotenuse for sides -7.0 and 24.0 is: 25
Example 3
Let's calculate the distance between two points using hypot(), which computes the straight-line distance in 2D space.
#include <iostream>
#include <cmath>
int main() {
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
double distance = hypot(x2 - x1, y2 - y1);
std::cout << "The distance between points (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << distance << std::endl;
return 0;
}
Output
If we run the above code it will generate the following output
The distance between points (1, 2) and (4, 6) is 5