简体   繁体   中英

At what point will the atan2 function reach a domain error for a zero value in c++?

If I have a function similar to this:

c=atan2( a, 0 )

I am trying to figure out what the a value should be limited to in order to ensure atan2() does not result in any domain errors. So the question is how close to zero can a get before a domain error is raised. Can this atan2 function compute a number as small as 0.0000001? At what point does it read as zero?

The best way to find out is to try it yourself!

#include <iostream>
#include <stdio.h>
#include <limits>
#include <math.h>

using namespace std;

int main()
{
    int powers[12] = {10, 11, 12, 13, 14, 15, 20, 22, 24, 26, 28, 30};
    
    for (auto i : powers) 
    {
        try 
        {
            double a = pow(10, -i);
            printf("a = 10^-%d; atan2(a, 0) = %.10e\n", i, atan2(a, 0));
            // cout << "a = 10^" << -i << "; atan2(a, 0) = " << atan2(a, 0) << endl;
        }
        catch (int e) 
        {
            cout << "Failed to compute atan2(10^" << -i << ", 0)" << endl;
        }
        
    }
    
    double minvalue = numeric_limits<double>::min();
    try 
    {
        printf("a = %.10e; atan2(a, 0) = %.10e\n", minvalue, atan2(minvalue, 0));
        // cout << "a = " << minvalue << "; atan2(a, 0) = " << atan2(minvalue, 0) << endl;
    }
    catch (int e) 
    {
        cout << "Failed to compute atan2(" << minvalue << ", 0)" << endl;
    }
    
    return 0;
}

Output:

a = 10^-10; atan2(a, 0) = 1.5707963268e+00
a = 10^-11; atan2(a, 0) = 1.5707963268e+00
a = 10^-12; atan2(a, 0) = 1.5707963268e+00
a = 10^-13; atan2(a, 0) = 1.5707963268e+00
a = 10^-14; atan2(a, 0) = 1.5707963268e+00
a = 10^-15; atan2(a, 0) = 1.5707963268e+00
a = 10^-20; atan2(a, 0) = 1.5707963268e+00
a = 10^-22; atan2(a, 0) = 1.5707963268e+00
a = 10^-24; atan2(a, 0) = 1.5707963268e+00
a = 10^-26; atan2(a, 0) = 1.5707963268e+00
a = 10^-28; atan2(a, 0) = 1.5707963268e+00
a = 10^-30; atan2(a, 0) = 1.5707963268e+00
a = 2.2250738585e-308; atan2(a, 0) = 1.5707963268e+00

Works perfectly fine for the smallest possible positive double.

It even works for 5E-324 , which is the smallest positive denormal double.

double minvalue = std::numeric_limits<double>::denorm_min();
printf("a = %.10e; atan2(a, 0) = %.10e\n", minvalue, atan2(minvalue, 0));

Output:

a = 4.9406564584e-324; atan2(a, 0) = 1.5707963268e+00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM