当前位置:网站首页>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);
}
边栏推荐
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- I closed the open source project alinesno cloud service
- Alibaba Tianchi SQL training camp task4 learning notes
- Record several frequently asked questions (202207)
- Usage Summary of scriptable object in unity
- Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
- Three. JS VR house viewing
- Simple and beautiful method of PPT color matching
- Ultrasonic sensor flash | LEGO eV3 Teaching
- 【Note17】PECI(Platform Environment Control Interface)
猜你喜欢
[untitled]
Error when LabVIEW opens Ni instance finder
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
我把开源项目alinesno-cloud-service关闭了
第一讲:蛇形矩阵
openresty ngx_lua请求响应
Ieventsystemhandler event interface
利用LNMP实现wordpress站点搭建
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
[untitled]
随机推荐
派对的最大快乐值
First, redis summarizes the installation types
openresty ngx_ Lua regular expression
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
Common JVM tools and optimization strategies
3 find the greatest common divisor and the least common multiple
SPSS analysis of employment problems of college graduates
Ieventsystemhandler event interface
Binary tree (II) -- code implementation of heap
All expansion and collapse of a-tree
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
分布式解决方案选型
东南亚电商指南,卖家如何布局东南亚市场?
我把开源项目alinesno-cloud-service关闭了
Getting started stm32--gpio (running lantern) (nanny level)
利用LNMP实现wordpress站点搭建
2022.02.13 - SX10-30. Home raiding II
Yiwen gets rid of the garbage collector
VIM tail head intercept file import