当前位置:网站首页>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);
}边栏推荐
- APK加固技术的演变,APK加固技术和不足之处
- 2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
- Three. JS VR house viewing
- openresty ngx_lua正则表达式
- Solve the problem of "no input file specified" when ThinkPHP starts
- 鏈錶之雙指針(快慢指針,先後指針,首尾指針)
- Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
- My experience and summary of the new Zhongtai model
- Google Maps case
- 两数之和、三数之和(排序+双指针)
猜你喜欢

一文搞定JVM常见工具和优化策略

Vision Transformer (ViT)
![[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]](/img/b4/af689abb3ad4e25988f2d17152406e.jpg)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]

Fix the memory structure of JVM in one article

How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?

All expansion and collapse of a-tree

My experience and summary of the new Zhongtai model

Event trigger requirements of the function called by the event trigger

东南亚电商指南,卖家如何布局东南亚市场?

openresty ngx_ Lua request response
随机推荐
Finally understand what dynamic planning is
Openresty ngx Lua regular expression
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
东南亚电商指南,卖家如何布局东南亚市场?
One article deals with the microstructure and instructions of class
TCC of distributed solutions
Un article traite de la microstructure et des instructions de la classe
audiopolicy
Vcomp110.dll download -vcomp110 What if DLL is lost
【Note17】PECI(Platform Environment Control Interface)
Binary tree (III) -- heap sort optimization, top k problem
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
leecode-学习笔记
Nail error code Encyclopedia
派对的最大快乐值
Arduino 测量交流电流
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
openresty ngx_lua请求响应
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
First, redis summarizes the installation types