c语言标准库详解(八):数学公式math.h

c语言标准库详解(八):数学公式<math.h>

概述

头文件<math.h>中声明了一些数学函数和宏。
宏 EDOM 和 ERANGE(在头文件<error.h>中声明)是两个非 0 整型常量,用于指示函数的定义域错误和值域错误;HUGE_VAL 是一个 double 类型的正数。当参数位于函数定义的作用域之外时,就会出现定义域错误。在发生定义域错误时,全局变量 errno 的值将被设置为 EDOM,函数的返回值与具体的实现相关。如果函数的结果不能用 double 类型表示,则会发生值域错误。当结果上溢时,函数返回 HUGE_VAL,并带有正确的正负号,errpo 的值将被设置为 ERANGE。当结果下溢时,函数返回 0,而 errno 是否设置为 ERANGE 要视具体的实现而定。
在下表中,x和y的类型为double,n的类型为int,所有函数的返回值的类型均为double。三角函数的角度用弧度表示。

函数名 功能
sin(x) x的正弦值
cos(x) x的余弦值
tan(x) x的正切值
asin(x) s i n 1 ( x ) sin^{-1}(x) ,值域为 [ π / 2 , π / 2 ] [-\pi /2,\pi /2] ,其中 x [ 1 , 1 ] x\in [-1,1]
acos(x) c o s 1 ( x ) cos^{-1}(x) ,值域为 [ 0 , π / 2 ] [0,\pi /2] ,其中 x [ 1 , 1 ] x\in [-1,1]
atan(x) t a n 1 ( x ) tan^{-1}(x) ,值域为 [ π / 2 , π / 2 ] [-\pi /2,\pi /2]
atan2(y,x) t a n 1 ( y / x ) tan^{-1}(y/x) ,值域为 [ π , π ] [-\pi,\pi]
sinh(x) x的双曲正弦值
cosh(x) x的双曲余弦值
tanh(x) x的双曲正切值
exp(x) 幂函数 e x e^x
log(x) 自然对数ln(x),其中x>0
log10(x) 以10为底的对数 l o g 10 ( x ) log_{10}(x) ,其中x>0
pow(x,y) x y x^y 。如果x=0且y<=10,或者x<0且y不是整型数,将产生定义域错误
sqrt(x) x的平方根,其中x>=0
ceil(x) 不小于x的最小整型数,其中x的类型为double
floor(x) 不大于x的最小整型数,其中x的类型为double
fabs(x) x的绝对值|x|
ldexp(x,n) 计算 x × 2 n x\times 2^n 的值
frexp(x,int *exp) 把 x 分成一个在[1/2, 1]区间内的真分数和一个 2 的幂数。结果将返回真分数部分,并将幂数保存在*exp 中。如果 x 为 0,则这两部分均为 0
modf(x,double *ip) 把 x 分成整数和小数两部分,两部分的正负号均与 x 相同。该函数返回小数部分,整数部分保存在*ip 中
fmod(x,y) 求 x除y 的浮点余数,符号与 x 相同。如果 y 为 0,则结果与具体的实现相关

示例

一般函数

代码:

#include <stdio.h>
#include <math.h>
int main(){
   double pi = 3.141592654;
   printf("%f\n",sin(pi/2.0));
   printf("%f\n",cos(pi));
   printf("%f\n",tan(pi/4.0));
   printf("%f\n",asin(0.5));
   printf("%f\n",acos(0));
   printf("%f\n",atan(1));
   printf("%f\n",atan2(1,2));
   printf("%f\n",sinh(1.14));
   printf("%f\n",cosh(5.14));
   printf("%f\n",tanh(1));
   printf("%f\n",exp(1));
   printf("%f\n",log(2));
   printf("%f\n",log10(100));
   printf("%f\n",pow(2,3));
   printf("%f\n",sqrt(1024));
   printf("%f\n",ceil(114.514));
   printf("%f\n",floor(1919.810));
   printf("%f\n",fabs(-114));
   printf("%f\n",ldexp(1.1,2));
}

输出:

PS G:\CSAPP>  & 'c:\Users\swy\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-4fkacns3.4n2' '--stdout=Microsoft-MIEngine-Out-vtf0dshg.kly' '--stderr=Microsoft-MIEngine-Error-kncwqgpb.2qz' '--pid=Microsoft-MIEngine-Pid-amsr1et1.2pt' '--dbgExe=G:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin\gdb.exe' '--interpreter=mi'
1.000000
-1.000000
1.000000 
0.523599
1.570796
0.785398
0.463648
1.403475
85.360813
0.761594
2.718282
0.693147
2.000000
8.000000
32.000000
115.000000
1919.000000
114.000000
4.400000
PS G:\CSAPP>

frexp

代码:

#include <stdio.h>
#include <math.h>
int main ()
{
   double x = 1024, fraction;
   int e;
   fraction = frexp(x, &e);
   printf("x = %.2lf = %.2lf * 2^%d\n", x, fraction, e);
   return(0);
}

输出:

PS G:\CSAPP>  & 'c:\Users\swy\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-h5rcncvy.5t4' '--stdout=Microsoft-MIEngine-Out-vqzpvdtb.g1q' '--stderr=Microsoft-MIEngine-Error-d4icaaa5.v0w' '--pid=Microsoft-MIEngine-Pid-50qt2c35.qtx' '--dbgExe=G:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin\gdb.exe' '--interpreter=mi' 
x = 1024.00 = 0.50 * 2^11
PS G:\CSAPP>

modf

代码:

/*这其实是一个把浮点数的整数部分与小数部分分离的实用程序*/
#include<stdio.h>
#include<math.h>
int main ()
{
   double x, fractpart, intpart;
   x = 114514.1919810;
   fractpart = modf(x, &intpart);     
   printf("整数部分 = %lf\n", intpart);
   printf("小数部分 = %lf \n", fractpart);
   return(0);
}

输出:

PS G:\CSAPP>  & 'c:\Users\swy\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-qt4optaz.cd1' '--stdout=Microsoft-MIEngine-Out-4k504r2l.rqk' '--stderr=Microsoft-MIEngine-Error-juwootva.ldp' '--pid=Microsoft-MIEngine-Pid-ghshwfhl.zty' '--dbgExe=G:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin\gdb.exe' '--interpreter=mi' 
整数部分 = 114514.000000
小数部分 = 0.191981
PS G:\CSAPP>

fmod

代码:

#include <stdio.h>
#include <math.h>
int main ()
{
   float a, b;
   int c;
   a = 19.19;
   b = 5.14;
   c = 1.14;
   printf("%f / %d 的余数是 %lf\n", a, c, fmod(a,c));
   printf("%f / %f 的余数是 %lf\n", a, b, fmod(a,b));
   return(0);
}

输出:

PS G:\CSAPP>  & 'c:\Users\swy\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-rh51wehg.khq' '--stdout=Microsoft-MIEngine-Out-evzssudj.sjg' '--stderr=Microsoft-MIEngine-Error-f4wajhcd.vmb' '--pid=Microsoft-MIEngine-Pid-3zrc1b30.dh1' '--dbgExe=G:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin\gdb.exe' '--interpreter=mi' 
19.190001 / 1 的余数是 0.190001
19.190001 / 5.140000 的余数是 3.770001
PS G:\CSAPP>
原创文章 327 获赞 212 访问量 40万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/105851744