atan2(x,y)函数详解

首页 / C++入门教程 / atan2(x,y)函数详解

函数查找坐标的反正切。

假设坐标为(x,y):

atan2(y,x) = tan-1(y/x);

atan2 - 语法

假设坐标为(x,y)。语法为:

float atan2(float y, float x);
double atan2(double y, double x);
long double atan2(long double y, long double x);
Promoted atan2(Arithmetic1 y, Arithmetic x );

atan2 - 参数

y :代表y坐标值。

x :它表示x坐标值。

atan2 - 返回值

它返回范围为[-?,?]的值,如果x和y的值均为零,则返回零值。

  • 如果任何参数为整数类型,则将其强制转换为double。
  • 如果任何参数为long double类型,则将其强制转换为long double。

atan2 - 例子1

让我们看一个简单的示例,其中x和y均为零。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-atan2-function.html

来源:LearnFk无涯教程网

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    int x=0;  
    int y=0;  
    cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';  
    std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;  
    return 0;  
}  

输出:

Value of tan(y/x) is : 0
Value of tan-1(y/x) is : 0

在此示例中,当" x"和" y"均为零时,atan2()计算逆切线。

atan2 - 例子2

让我们看一个简单的示例,其中" x"和" y"都是不同的类型。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    int x=6;  
    float y=7.8;  
    cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';  
    std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;  
    return 0;  
}  

输出:

Value of tan(y/x) is : 3.6021
Value of tan1(y/x) is : 0.915101

在此示例中,当x为整数类型且y为浮点类型时,atan2()函数可找到切线的逆。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

小马哥讲Spring AOP编程思想 -〔小马哥〕

高楼的性能工程实战课 -〔高楼〕

Spring编程常见错误50例 -〔傅健〕

如何讲好一堂课 -〔薛雨〕

云计算的必修小课 -〔吕蕴偲〕

快速上手C++数据结构与算法 -〔王健伟〕

云时代的JVM原理与实战 -〔康杨〕

Rust 语言从入门到实战 -〔唐刚〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)