当前位置:网站首页>C Primer Plus Chapter 9 question 9 POW function
C Primer Plus Chapter 9 question 9 POW function
2022-07-05 23:00:00 【multydoffer】
simulation <math.h> Medium pow Function to write a power function , Return to one double Positive integer power of type number . Improve the function , So that it can correctly calculate the negative power . in addition , Function to handle 0 Any power of is 0, Any number of 0 The power is 1( Function should warn 0 Of 0 Power undefined , Therefore, the value is treated as 1). To use recursive functions , And test the function in the program .
Here is the code :
#include <stdio.h>
double power(double x, int n);
int main(void)
{
double x;
int n;
printf("Enter a number:");
scanf("%lf", &x);
printf("Enter a integer:");
scanf("%d", &n);
printf("%.2lf^%d = %lf", x, n, power(x, n));
return 0;
}
double power(double x, int n)
{
double pow = x;
if(!x)
{
if(!n) //0 Of 0 Power
{
printf("For there is no definition for 0^0, so I return 1.\n");
return 1.0;
}
else
return 0.0;
}
if(n > 1)
pow *= power(x, n-1);
else if(!n)
return 1.0;
else if(n == 1) // Recursive termination
return pow;
else if(n < 0)
return 1.0 / pow / power(x, -n-1);
}
边栏推荐
- Three. Js-01 getting started
- Arduino measures AC current
- 如何快速理解复杂业务,系统思考问题?
- 利用LNMP实现wordpress站点搭建
- [speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
- My experience and summary of the new Zhongtai model
- Spectrum analysis of ADC sampling sequence based on stm32
- All expansion and collapse of a-tree
- openresty ngx_lua请求响应
- Ieventsystemhandler event interface
猜你喜欢
利用LNMP实现wordpress站点搭建
关于MySQL的30条优化技巧,超实用
audiopolicy
我把开源项目alinesno-cloud-service关闭了
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
一文搞定class的微觀結構和指令
Yiwen gets rid of the garbage collector
查看网页最后修改时间方法以及原理简介
TCC of distributed solutions
Simple and beautiful method of PPT color matching
随机推荐
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
Openresty ngx Lua regular expression
Function default parameters, function placeholder parameters, function overloading and precautions
Nanjing: full use of electronic contracts for commercial housing sales
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Exponential weighted average and its deviation elimination
All expansion and collapse of a-tree
MCU case -int0 and INT1 interrupt count
Media query: importing resources
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
APK加固技术的演变,APK加固技术和不足之处
透彻理解JVM类加载子系统
Overview of Fourier analysis
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
两数之和、三数之和(排序+双指针)
Hcip day 11 (BGP agreement)
Nangou Gili hard Kai font TTF Download with installation tutorial
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
[untitled]