当前位置:网站首页>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);
}边栏推荐
- 【Note17】PECI(Platform Environment Control Interface)
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- 一文搞定JVM常见工具和优化策略
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- 东南亚电商指南,卖家如何布局东南亚市场?
- openresty ngx_ Lua regular expression
- My experience and summary of the new Zhongtai model
- 【Note17】PECI(Platform Environment Control Interface)
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- Nanjing: full use of electronic contracts for commercial housing sales
猜你喜欢

All expansion and collapse of a-tree

利用LNMP实现wordpress站点搭建

傅里叶分析概述

Arduino measures AC current

My experience and summary of the new Zhongtai model

Common model making instructions

Alibaba Tianchi SQL training camp task4 learning notes

Matlab smooth curve connection scatter diagram

Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem

The difference between MVVM and MVC
随机推荐
Un article traite de la microstructure et des instructions de la classe
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Binary tree (II) -- code implementation of heap
openresty ngx_lua正則錶達式
【Note17】PECI(Platform Environment Control Interface)
一文搞定JVM的内存结构
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
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Yiwen gets rid of the garbage collector
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Douban scoring applet Part-2
Common JVM tools and optimization strategies
Three. JS VR house viewing
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
谷歌地图案例
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
实现反向代理客户端IP透传
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
一文搞定class的微观结构和指令
Alibaba Tianchi SQL training camp task4 learning notes