在c ++中,atan2函数将在什么时候达到域错误值为零?

问题描述

如果我具有与此相似的功能:

c=atan2( a,0 )

我试图找出a值应限制在什么范围内,以确保atan2()不会导致任何域错误。因此,问题是在引发域错误之前,可以获得多近于零的值。这个atan2函数可以计算出小至0.0000001的数字吗?在什么时候读为零?

解决方法

找出答案的最佳方法是亲自尝试!

#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,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;
}

输出:

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

对于最小可能的正整数,效果很好。

它甚至适用于5E-324,这是最小的正反常态两倍。

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

输出:

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...