头文件<math.h>中声明了一些数学函数和宏。 代码: 输出: 代码: 输出: 代码: 输出: 代码: 输出:
c语言标准库详解(八):数学公式<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)
sin−1(x),值域为
[−π/2,π/2],其中
x∈[−1,1]
acos(x)
cos−1(x),值域为
[0,π/2],其中
x∈[−1,1]
atan(x)
tan−1(x),值域为
[−π/2,π/2]
atan2(y,x)
tan−1(y/x),值域为
[−π,π]
sinh(x)
x的双曲正弦值
cosh(x)
x的双曲余弦值
tanh(x)
x的双曲正切值
exp(x)
幂函数
ex
log(x)
自然对数ln(x),其中x>0
log10(x)
以10为底的对数
log10(x),其中x>0
pow(x,y)
xy。如果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×2n的值
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("%fn",sin(pi/2.0)); printf("%fn",cos(pi)); printf("%fn",tan(pi/4.0)); printf("%fn",asin(0.5)); printf("%fn",acos(0)); printf("%fn",atan(1)); printf("%fn",atan2(1,2)); printf("%fn",sinh(1.14)); printf("%fn",cosh(5.14)); printf("%fn",tanh(1)); printf("%fn",exp(1)); printf("%fn",log(2)); printf("%fn",log10(100)); printf("%fn",pow(2,3)); printf("%fn",sqrt(1024)); printf("%fn",ceil(114.514)); printf("%fn",floor(1919.810)); printf("%fn",fabs(-114)); printf("%fn",ldexp(1.1,2)); }
PS G:CSAPP> & 'c:Usersswy.vscodeextensionsms-vscode.cpptools-0.27.1debugAdaptersbinWindowsDebugLauncher.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-rev0mingw64bingdb.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^%dn", x, fraction, e); return(0); }
PS G:CSAPP> & 'c:Usersswy.vscodeextensionsms-vscode.cpptools-0.27.1debugAdaptersbinWindowsDebugLauncher.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-rev0mingw64bingdb.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("整数部分 = %lfn", intpart); printf("小数部分 = %lf n", fractpart); return(0); }
PS G:CSAPP> & 'c:Usersswy.vscodeextensionsms-vscode.cpptools-0.27.1debugAdaptersbinWindowsDebugLauncher.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-rev0mingw64bingdb.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 的余数是 %lfn", a, c, fmod(a,c)); printf("%f / %f 的余数是 %lfn", a, b, fmod(a,b)); return(0); }
PS G:CSAPP> & 'c:Usersswy.vscodeextensionsms-vscode.cpptools-0.27.1debugAdaptersbinWindowsDebugLauncher.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-rev0mingw64bingdb.exe' '--interpreter=mi' 19.190001 / 1 的余数是 0.190001 19.190001 / 5.140000 的余数是 3.770001 PS G:CSAPP>
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算