当前位置:网站首页>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加固技术和不足之处
- 二叉树(三)——堆排序优化、TOP K问题
- Element positioning of Web Automation
- Expectation, variance and covariance
- Roman numeral to integer
- Codeforces Global Round 19
- Distributed resource management and task scheduling framework yarn
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- 第一讲:蛇形矩阵
- 透彻理解JVM类加载子系统
猜你喜欢

Getting started stm32--gpio (running lantern) (nanny level)

Selenium+pytest automated test framework practice

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

The method and principle of viewing the last modification time of the web page

Arduino 测量交流电流

鏈錶之雙指針(快慢指針,先後指針,首尾指針)

513. Find the value in the lower left corner of the tree

Exponential weighted average and its deviation elimination

基于STM32的ADC采样序列频谱分析

Three.JS VR看房
随机推荐
Arduino measures AC current
CJ mccullem autograph: to dear Portland
如何快速理解复杂业务,系统思考问题?
查看网页最后修改时间方法以及原理简介
透彻理解JVM类加载子系统
openresty ngx_lua正则表达式
Selenium+pytest automated test framework practice
Boring boring
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
两数之和、三数之和(排序+双指针)
视频标准二三事
Element operation and element waiting in Web Automation
All expansion and collapse of a-tree
Usage Summary of scriptable object in unity
Error when LabVIEW opens Ni instance finder
The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
Editor extensions in unity
Distributed solution selection
openresty ngx_lua请求响应
【Note17】PECI(Platform Environment Control Interface)