C algorithm - Getting Started 2.2

Common math functions

1 #include <stdio.h>
2 #include <math.h>
3 
4 /*绝对值*/
5 int main(){
6     double db=-12.56;
7     printf("%.2f\n",fabs(db));
8     return 0;
9 } 
fabs (dhouble x) the absolute value of
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 /*向下(floor)向上(ceil)取整*/
 5 int main(){
 6     double db1=-5.2,db2=5.2;
 7     printf("%.0f %.0f\n",floor(db1),ceil(db1));
 8     printf("%.0f %.0f\n",floor(db2),ceil(db2));
 9     return 0;
10 } 
floor / ceil rounding
1 #include <stdio.h>
2 #include <math.h>
3 
4 /*2的三次方*/
5 int main(){
6     double db =pow(2.0,3.0);
7     printf("%f\n",db);
8     return 0;
9 }
pow power
. 1 #include <stdio.h>
 2 #include <math.h>
 . 3  
. 4  / * square root * / 
. 5  int main () {
 . 6      Double DB = sqrt ( 2.0 );
 . 7      the printf ( " % F \ n- " , DB );
 . 8      return  0 ;
 . 9 }
square root sqrt
#include <stdio.h> 
#include <math.h> / * return type double natural logarithm with base number * / int main () {
     double DB = log ( 1.0 ); 
    the printf ( " % F \ n- " , DB);
     return 0 ;     
}


 
log

Note: There is no demand for any function of the number of C in the bottom, with the formula soled logzb = logeb / logez

. 1 #include <stdio.h>
 2 #include <math.h>
 . 3  
. 4  / * radians * / 
. 5  / * ACOS is a function of its cosine function is negated. ACOS (-1.0) -1.0 cosine is negated, and then assigned to a variable of type double often PI * / 
. 6  const  double PI = ACOS (- 1.0 );
 . 7  
. 8  int main () {
 . 9      / *    the PI * one rotation angle degrees / 180 [   * / 
10      Double DB1 = SiN (PI * 45 / 180 [ );
 . 11      Double DB2 = COS (PI * 45 / 180 [ );
 12 is      Double DB3 = Tan (PI * 45/180);
13     printf("%f,%f,%f\n",db1,db2,db3);
14     return 0;
15 } 
Trigonometric functions in radians
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 /*反三角函数*/
 5 int main(){
 6     double db1=asin(1);
 7     double db2=acos(-1.0);
 8     double db3=atan(0);
 9     printf("%f,%f,%f\n ",db1,db2,db3);
10     return 0;
11 } 
Inverse trigonometric functions
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main(){
 5     double db1=round(3.40);
 6     double db2=round(3.45);
 7     double db3=round(3.50);
 8     double db4=round(3.55);
 9     double db5=round(3.60);
10     printf("%d, %d, %d, %d ,%d\n",(int)db1,(int)db2,(int)db3,(int)db4,(int)db5);
11     return 0;
12 } 
round rounding

 

Guess you like

Origin www.cnblogs.com/Catherinezhilin/p/11131446.html