当前位置:   article > 正文

C++——cmath_c++ cmath

c++ cmath

写在前面

这个库就是简单的整理一下吧,主要包含了各种常用数学函数。简要整理一下。目前只会整理一下自己遇到的函数,所有的函数用法可以参考:< cmath > (math.h)

头文件包含

#include <math.h> 
  • 1

Trigonometric functions

这里写图片描述

1.cos

/* cos example */
#include <stdio.h>      /* printf */
#include <math.h>       /* cos */

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 60.0;
  result = cos ( param * PI / 180.0 );
  printf ("The cosine of %f degrees is %f.\n", param, result );
  return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Hyperbolic functions

这里写图片描述

Exponential and logarithmic functions

这里写图片描述

1.exp

/* exp example */
#include <stdio.h>      /* printf */
#include <math.h>       /* exp */

int main ()
{
  double param, result;
  param = 5.0;
  result = exp (param);
  printf ("The exponential value of %f is %f.\n", param, result );
  return 0;
}
//Output:
//The exponential value of 5.000000 is 148.413159.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.frexp
将一个浮点数转换成0.5~1.0之间的数和2的整数指数。

/* frexp example */
#include <stdio.h>      /* printf */
#include <math.h>       /* frexp */

int main ()
{
  double param, result;
  int n;

  param = 8.0;
  result = frexp (param , &n);
  printf ("%f = %f * 2^%d\n", param, result, n);
  return 0;
}
//Output:
//8.000000 = 0.500000 * 2^4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.ldexp
frexp函数逆运算,根据0.5~1.0之间的浮点数与n求出对应的浮点数。

/* ldexp example */
#include <stdio.h>      /* printf */
#include <math.h>       /* ldexp */

int main ()
{
  double param, result;
  int n;

  param = 0.95;
  n = 4;
  result = ldexp (param , n);
  printf ("%f * 2^%d = %f\n", param, n, result);
  return 0;
}
//Output:
//0.950000 * 2^4 = 15.200000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4.log
计算一个数的自然对数:log(x),以e为底。

/* log example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log */

int main ()
{
  double param, result;
  param = 5.5;
  result = log (param);
  printf ("log(%f) = %f\n", param, result );
  return 0;
}
//Output:
//log(5.500000) = 1.704748
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5.log10
计算一个数的以10为底的对数:log10(x)。

/* log10 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log10 */

int main ()
{
  double param, result;
  param = 1000.0;
  result = log10 (param);
  printf ("log10(%f) = %f\n", param, result );
  return 0;
}
//Output:
//log10(1000.000000) = 3.000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

6.modf
将一个数分为整数与小数部分。

/* modf example */
#include <stdio.h>      /* printf */
#include <math.h>       /* modf */

int main ()
{
  double param, fractpart, intpart;

  param = 3.14159265;
  fractpart = modf (param , &intpart);
  printf ("%f = %f + %f \n", param, intpart, fractpart);
  return 0;
}
//Output:
//3.141593 = 3.000000 + 0.14159
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

7.exp2
求一个数为指数,2为底数的幂:2^x。

/* exp2 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* exp2 */

int main ()
{
  double param, result;
  param = 8.0;
  result = exp2 (param);
  printf ("2 ^ %f = %f.\n", param, result );
  return 0;
}
//Output:
//2 ^ 8.000000 is 256.000000.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

8.expm1
计算函数:e^x - 1的值。对于较小值的x,e^x - 1 比 exp(x) - 1更精确。

/* expm1 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* expm1 */

int main ()
{
  double param, result;
  param = 1.0;
  result = expm1 (param);
  printf ("expm1 (%f) = %f.\n", param, result );
  return 0;
}
//Output:
//expm1 (1.000000) = 1.718282.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

9.log2
计算以2为底的对数。

/* log2 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log2 */

int main ()
{
  double param, result;
  param = 1024.0;
  result = log2 (param);
  printf ("log2 (%f) = %f.\n", param, result );
  return 0;
}
//Output:
//log2 (1024.000000) = 10.000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Power functions

这里写图片描述

1.pow
计算乘积值。

/* pow example */
#include <stdio.h>      /* printf */
#include <math.h>       /* pow */

int main ()
{
  printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
  printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) );
  printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) );
  return 0;
}
//Output:
//7 ^ 3 = 343.000000
//4.73 ^ 12 = 125410439.217423
//32.01 ^ 1.54 = 208.036691
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.sqrt

/* sqrt example */
#include <stdio.h>      /* printf */
#include <math.h>       /* sqrt */

int main ()
{
  double param, result;
  param = 1024.0;
  result = sqrt (param);
  printf ("sqrt(%f) = %f\n", param, result );
  return 0;
}
//Output:
//sqrt(1024.000000) = 32.000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.cbrt
计算一个数的立方根。

/* cbrt example */
#include <stdio.h>      /* printf */
#include <math.h>       /* cbrt */

int main ()
{
  double param, result;
  param = 27.0;
  result = cbrt (param);
  printf ("cbrt (%f) = %f\n", param, result);
  return 0;
}
//Output:
//cbrt (27.000000) = 3.000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.hypot
计算三角形的第三边。

/* hypot example */
#include <stdio.h>      /* printf */
#include <math.h>       /* hypot */

int main ()
{
  double leg_x, leg_y, result;
  leg_x = 3;
  leg_y = 4;
  result = hypot (leg_x, leg_y);
  printf ("%f, %f and %f form a right-angled triangle.\n",leg_x,leg_y,result);
  return 0;
}
//Output:
//3.000000, 4.000000 and 5.000000 form a right-angled triangle.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Error and gamma functions

这里写图片描述

Rounding and remainder functions

这里写图片描述

1.ceil
对一个小数向上取整数。

/* ceil example */
#include <stdio.h>      /* printf */
#include <math.h>       /* ceil */

int main ()
{
  printf ( "ceil of 2.3 is %.1f\n", ceil(2.3) );
  printf ( "ceil of 3.8 is %.1f\n", ceil(3.8) );
  printf ( "ceil of -2.3 is %.1f\n", ceil(-2.3) );
  printf ( "ceil of -3.8 is %.1f\n", ceil(-3.8) );
  return 0;
}
//Output:
//ceil of 2.3 is 3.0
//ceil of 3.8 is 4.0
//ceil of -2.3 is -2.0
//ceil of -3.8 is -3.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.floor
对一个小数向下取整数

/* floor example */
#include <stdio.h>      /* printf */
#include <math.h>       /* floor */

int main ()
{
  printf ( "floor of 2.3 is %.1lf\n", floor (2.3) );
  printf ( "floor of 3.8 is %.1lf\n", floor (3.8) );
  printf ( "floor of -2.3 is %.1lf\n", floor (-2.3) );
  printf ( "floor of -3.8 is %.1lf\n", floor (-3.8) );
  return 0;
}
//Output:
//floor of 2.3 is 2.0
//floor of 3.8 is 3.0
//floor of -2.3 is -3.0
//floor of -3.8 is -4.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Floating-point manipulation functions

这里写图片描述

Minimum, maximum, difference functions

这里写图片描述

Other functions

这里写图片描述

1.fabs
浮点数取绝对值。

/* fabs example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fabs */

int main ()
{
  printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) );
  printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) );
  return 0;
}
//Output:
//The absolute value of 3.1416 is 3.141600
//The absolute value of -10.6 is 10.600000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.abs
整数取绝对值。

Classification macro / functions

这里写图片描述

Comparison macro / functions

这里写图片描述

Macro constants

这里写图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/656791
推荐阅读
相关标签
  

闽ICP备14008679号