c++ sin,cos,tan,atan,atan2
2016-04-18 16:45
507 查看
三角函数
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.csdn.net/goodchoes/article/details/46481669 //http://blog.csdn.net/kingstar158/article/details/6859379/
C/C++中算法运行时间的三种计算方式
//http://pubs.opengroup.org/onlinepubs/7908799/xsh/systimeb.h.htmlCode 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; }
相关文章推荐
- C++类的常成员函数
- OpenGL: Configuring GLFW and GLEW in Visual C++ Express
- [C++]函数对象(一)
- 编写高质量的C++代码--01 减少文件之间的编译依赖
- C++多重继承带来的问题
- C/C++内存泄漏检测
- 【c/c++】list
- c++ 字符串连接问题
- Effective C++笔记(三):资源管理
- terminate called after throwing an instance of 'std::bad_alloc
- 基于C语言实现简单的走迷宫游戏
- 输入流对象cin读取输入流的三种方式
- C++虚析构函数
- C++ STL中vector的内存机制和性能分析
- C语言结构体的字节对齐原则
- C++时间,文件及字符常用函数总结
- c++实现mysql数据库数据缓存
- 贪吃蛇C语言代码
- 【C++】日期类+日期万年历+日期计算器
- 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)