当前位置:网站首页>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);
}边栏推荐
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- 视频标准二三事
- Vision Transformer (ViT)
- Tensor attribute statistics
- audiopolicy
- VOT toolkit environment configuration and use
- VIM tail head intercept file import
- Getting started stm32--gpio (running lantern) (nanny level)
- Douban scoring applet Part-2
- Media query: importing resources
猜你喜欢

第十七周作业

MoCo: Momentum Contrast for Unsupervised Visual Representation Learning

First, redis summarizes the installation types

透彻理解JVM类加载子系统

Codeforces Global Round 19

Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)

Ieventsystemhandler event interface

分布式解决方案之TCC

Negative sampling

TypeError: this. getOptions is not a function
随机推荐
一文搞定JVM的内存结构
SPSS analysis of employment problems of college graduates
openresty ngx_ Lua request response
分布式解决方案之TCC
一文搞定垃圾回收器
Common model making instructions
APK加固技术的演变,APK加固技术和不足之处
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Expectation, variance and covariance
Masked Autoencoders Are Scalable Vision Learners (MAE)
Element positioning of Web Automation
我对新中台模型的一些经验思考总结
Exponential weighted average and its deviation elimination
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
VOT toolkit environment configuration and use
Distributed resource management and task scheduling framework yarn
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Simple and beautiful method of PPT color matching
派对的最大快乐值
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)