c++ sin,cos,tan,atan,atan2

本文介绍C语言中三角函数的应用实例,包括正弦、余弦、正切及反正切函数,并给出不同场景下的使用示例。此外,还探讨了C/C++中三种不同的算法运行时间测量方法,帮助开发者选择合适的计时方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三角函数

sin(角度*M_PI/180);

#include <math.h>

double cos(double x)
double sin(double x)
double tan(double x)//Tangent function
double atan(double x)
所有x为弧度值(radians),
/* tan example */
#include <stdio.h>      /* printf */
#include <math.h>       /* tan */

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 45.0;
  result = tan ( param * PI / 180.0 );
  printf ("The tangent of %f degrees is %f.\n", param, result );
  return 0;
}

atan2

double atan2 (double y , double x);
float atan2 (float y , float x);
long double atan2 (long double y, long double x);
double atan2 (Type1 y , Type2 x); // additional overloads

在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x) 他们返回的值是弧度 要转化为角度再自己处理下。
前者接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。
第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了

y
Value representing the proportion of the y-coordinate.
x
Value representing the proportion of the x-coordinate.

If both arguments passed are zero, a domain error occurs.

Principal arc tangent of y/x, in the interval [-pi,+pi] radians.
One radian is equivalent to 180/PI degrees.

例如:

1:斜率是1的直线的夹角

cout<<atan(1.0)*180/PI;//45°

cout<<atan2(1.0,1.0)*180/PI;//45° 第一象限,Quadrant

cout<<atan2(-1.0,-1.0)*180/PI;//-135°第三象限

后两个斜率都是1 但是atan只能求出一个45°

例2:斜率是-1的直线的角度

cout<<atan(-1.0)*180/PI;//-45°

cout<<atan2(-1.0,1.0)*180/PI;//-45° y为负 在第四象限

cout<<atan2(1.0,-1.0)*180/PI;//135° x为负 在第二象限
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main(void){
  cout<<atan2(1,sqrt(3))*180/M_PI<<endl;// 30度 第一象限
  cout<<atan2(1,-sqrt(3))*180/M_PI<<endl;//150度 第二象限
  cout<<atan2(-1,-sqrt(3))*180/M_PI<<endl;//-150度 第三象限
  cout<<atan2(-1,sqrt(3))*180/M_PI<<endl;//-30度 第四象限
  return   0;
}

gcvt是把浮点数转换成字符串,同时返回一个指向字符串的存储位置的指针的函数。

float to string

#include <iostream>
//#include <boost/version.hpp>
#include<sstream>
using namespace std;
int main()
{
  float num=123.6987;
  ostringstream oss;
  oss<<num;
  string str(oss.str());
  cout<<str<<endl;
  oss.str("");//清空数据
  cout<<oss.str()<<endl;
  oss<<" ";
  oss<<0.236985;
  cout<<oss.str()<<endl;
}
//http://blog.youkuaiyun.com/goodchoes/article/details/46481669
//http://blog.youkuaiyun.com/kingstar158/article/details/6859379/

C/C++中算法运行时间的三种计算方式

//http://pubs.opengroup.org/onlinepubs/7908799/xsh/systimeb.h.html

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->#include <stdio.h>
#include <tchar.h>
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <ctime>
#include <climits>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //计时方式一
    time_t start = 0,end = 0;
    time(&start);
    for(int i=0; i < numeric_limits<int>::max(); i++)
    {
        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数
    }
    time(&end);
    cout << "采用计时方式一(精确到秒):循环语句运行了:" << (end-start) << "秒" << endl;
      //计时方式二
    struct timeb startTime , endTime;
    ftime(&startTime);
    for(int i=0; i < numeric_limits<int>::max(); i++)
    {
        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数
    }
    ftime(&endTime);
    cout << "采用计时方式二(精确到毫秒):循环语句运行了:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;
    //计时方式三
    clock_t startCTime , endCTime;  
    startCTime = clock();   //clock函数返回CPU时钟计时单元(clock tick)数,还有一个常量表示一秒钟有多少个时钟计时单元,可以用clock()/CLOCKS_PER_SEC来求取时间
    for(int i=0; i < numeric_limits<int>::max(); i++)
    {
        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数
    }
    endCTime = clock();
    cout << "采用计时方式三(好像有些延迟,精确到秒):循环语句运行了:" << double((endCTime-startCTime)/CLOCKS_PER_SEC) << "秒" << endl;
    cout << "综合比较上述三种种计时方式,方式二能够精确到毫秒级别,比方式一和三都较好。此外在Windows API中还有其他的计时函数,用法都大同小异,在此就不做介绍了。" << endl;
     system("pause");
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值