Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use `std::hypot(x,y)` over `std::sqrt(x*x + y*y)`

The documentation of std::hypot says that:

Computes the square root of the sum of the squares of x and y, without undue overflow or underflow at intermediate stages of the computation.

I struggle to conceive a test case where std::hypot should be used over the trivial sqrt(x*x + y*y).

The following test shows that std::hypot is roughly 20x slower than the naive calculation.

#include <iostream> #include <chrono> #include <random> #include <algorithm>  int main(int, char**) {     std::mt19937_64 mt;     const auto samples = 10000000;     std::vector<double> values(2 * samples);     std::uniform_real_distribution<double> urd(-100.0, 100.0);     std::generate_n(values.begin(), 2 * samples, [&]() {return urd(mt); });     std::cout.precision(15);      {         double sum = 0;         auto s = std::chrono::steady_clock::now();         for (auto i = 0; i < 2 * samples; i += 2) {             sum += std::hypot(values[i], values[i + 1]);         }         auto e = std::chrono::steady_clock::now();         std::cout << std::fixed <<std::chrono::duration_cast<std::chrono::microseconds>(e - s).count() << "us --- s:" << sum << std::endl;     }     {         double sum = 0;         auto s = std::chrono::steady_clock::now();         for (auto i = 0; i < 2 * samples; i += 2) {             sum += std::sqrt(values[i]* values[i] + values[i + 1]* values[i + 1]);         }         auto e = std::chrono::steady_clock::now();         std::cout << std::fixed << std::chrono::duration_cast<std::chrono::microseconds>(e - s).count() << "us --- s:" << sum << std::endl;     } } 

So I'm asking for guidance, when must I use std::hypot(x,y) to obtain correct results over the much faster std::sqrt(x*x + y*y).

Clarification: I'm looking for answers that apply when x and y are floating point numbers. I.e. compare:

double h = std::hypot(static_cast<double>(x),static_cast<double>(y)); 

to:

double xx = static_cast<double>(x); double yy = static_cast<double>(y); double h = std::sqrt(xx*xx + yy*yy); 
like image 971
Emily L. Avatar asked Sep 07 '15 09:09

Emily L.


People also ask

What does hypot mean in C++?

The hypot() function in C++ returns the square root of sum of square of arguments passed.

Can I use math hypot?

Because hypot() is a static method of Math , you always use it as Math. hypot() , rather than as a method of a Math object you created ( Math is not a constructor). If no arguments are given, the result is +0. If any of the arguments is ±Infinity, the result is Infinity.

What is JavaScript hypot?

hypot() function in JavaScript is used to calculate the square root of the sum of squares of numbers passed to it as arguments. It is basically used to find the hypotenuse of a right-angled triangle or the magnitude of a complex number. Math.


1 Answers

The answer is in the documentation you quoted

Computes the square root of the sum of the squares of x and y, without undue overflow or underflow at intermediate stages of the computation.

If x*x + y*y overflows, then if you carry out the calculation manually, you'll get the wrong answer. If you use std::hypot, however, it guarantees that the intermediate calculations will not overflow.

You can see an example of this disparity here.

If you are working with numbers which you know will not overflow the relevant representation for your platform, you can happily use the naive version.

like image 156
TartanLlama Avatar answered Sep 20 '22 14:09

TartanLlama