当前位置:网站首页>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);
}
边栏推荐
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
- Negative sampling
- Thinkphp5.1 cross domain problem solving
- Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
- SPSS analysis of employment problems of college graduates
- Un article traite de la microstructure et des instructions de la classe
- 如何快速理解复杂业务,系统思考问题?
- d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
- Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
猜你喜欢
Metaverse ape received $3.5 million in seed round financing from negentropy capital
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Marginal probability and conditional probability
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
Ultrasonic sensor flash | LEGO eV3 Teaching
MCU case -int0 and INT1 interrupt count
Google Maps case
两数之和、三数之和(排序+双指针)
基于STM32的ADC采样序列频谱分析
随机推荐
一文搞定垃圾回收器
Tensor attribute statistics
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
I closed the open source project alinesno cloud service
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Three. JS VR house viewing
[untitled]
The countdown to the launch of metaverse ape is hot
Ultrasonic sensor flash | LEGO eV3 Teaching
Three.JS VR看房
傅里叶分析概述
Starting from 1.5, build a micro Service Framework -- log tracking traceid
二叉树(二)——堆的代码实现
Negative sampling
实现反向代理客户端IP透传
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
Request preview display of binary data and Base64 format data
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share