Other types of mining POW functions
{
double sum = 1;
br/>if (n <
return 0;< br/>for (unsigned int i = 1; i <= = n; i++)
sum *= x--;< <
}
double pow_1(double x, int n)/ > {
double sum = 1;
br/>for (int i = 1; i <= = n; i++)
sum *= x;< < br />}
double pow_2(double x, double a)///first channel of calculation from Thailand
{
int MAX = 100; // Grade
double sum = 1, k1, k2;< br/>if (x > 2)/x is larger than 2 required (x divided 1) a value
{
return 1/pow(1/x, a);
}
if (x == 0)
{
return 0;
} x--;< br/>for (int i = 1; i < MAX; i++)//Television request and
{
k1 = pow_1(x, i);
k2 = Factorial(a, i);< br/>sum += k1 * k2 / Factorial(i, i);<
}
return sum;< br/>}
int main()
{
Pow() function is used to find the power of y of X. x, y and the value of function are all double type. Its prototype is double pow (double x, double y)
the example code is as follows:
? Include & lt; stdio.h>
#include< math.h>
void main()
{
double x = 2, y = 10;
printf("% f ", pow(x, y));
return 0;
}
extended data :
when calling POW function, it may cause error:
if base x is negative and Index Y is not an integer, it will cause domain error
if both the base X and the index y are 0, the domain error? Error, or maybe not; It's about the implementation of the library
if the base x is 0 and the Index Y is negative, it may lead to? Domain error or pole error, or maybe not; It's about the implementation of the library
if the return value RET is too large or too small, it will cause range error
error code:
if domain error occurs, the global variable errno will be set to Edom
If pole error or range error occurs, the global variable errno will be set to ERANGE
POW function:
mathematical function in C / C +
pow() function is used to calculate the power of y of X. the values of X, y and the function are all double
pow() is used to calculate the power of Y with X as the base, and then return the result. Let RET be the return value, then RET = XY
may cause errors:
-
if the base x is negative and the exponent y is not an integer, it will cause domain error
-
if both base X and Index Y are 0, domain error may or may not occur; It's about the implementation of the library
-
if the base x is 0 and the Index Y is negative, domain error or pole error may or may not be caused; It's about the implementation of the library
-
if the return value RET is too large or too small, it will cause range error
< / OL > -
if domain error occurs, the global variable errno will be set to Edom
-
If pole error or range error occurs, the global variable errno will be set to ERANGE
< / OL >
error code:
{rrrrrrr}
development materials:
prototype: in TC2.0, the prototype is external float pool (float x, float y), In VC6.0, the prototype is double pow (double x, double y)
header file: math.h/cmath (in C + +)
function: calculate the Y power of X
return value: X cannot be negative and Y is a decimal, or X is 0 and Y is less than or equal to 0, return the result of power exponent
return type: double, int, float will give warning! Network -- POW function
Usage:
? Include & lt; math.h>// This one. In fact, it's OK to have none
double x,y,z;// You can assign values on demand 1
z=pow(x,y);
printf(“%lf 2”,z3);// You can adjust it according to how many digits you want to output, for example, one decimal%. 1lf
(1) when "X" and "Y" are defined as int, note 3 also holds(2) when defining "Z" as int, you can also change the type of printf to "d"
[2]% lf
because the type of Z is now double, so use% LF. If you use% D, the total output is zero
[3] "Z"
it's OK to change "Z" into "pow (x, y)" here
extended data
type conversion problems for pow:
when "Z" is defined as int, or the type of printf is changed to% D, a conversion problem from double to int occurs. Here, the conversion process is very rough, will directly remove the decimal, resulting in errors
in order to avoid this problem and output integers, we can take the following steps:
1) printf (& quot;%) 0lf", pow(x,y));
2 printf("% d",( int)(pow(x,y)+0.5));// Artificial rounding
for example, int and float types can be implicitly converted to double types, and because they are upward extended, they will not cause any sequelae
function value types have never heard that you should say the return value of a function, which depends on the return value type of the function, For example, the return value of pow here is also double type
-- supplement
the pow function is a library function, and the parameter type has been determined, so you can't change it
you can pass in int type, but when it is actually passed in, it is converted once, and when it is passed out, it is still double type
you can change it to int type again (you don't need to consider this very often, Because 5.00000 and 5 won't make any difference to your later operation)
-- add again
Yes, the return value type of the function won't change because of the change of the value you pass in, unless it's a function template of C + +, POW is just an ordinary function
Use law: #include < math.h > < br/>
Functions: yth of calculator 24130;
indicate: x should be larger than 0, return 24130;
Example:
/ / / / / / pow.c
& #include < math.h > < br/> & #include < Conio.h > < br/> < void main()
< printf(" 4^5=%f ", Pow(4.5.);< br/> < getchar();< br/ > }
Relevant communications: pow10
scanf("% lf",& y);
the format specifier corresponding to double type is% LF, note that it is lowercase letter L
#include<stdio.h>
#include <math.h>
int main ( void )
{
double a = 2;
double x;
x = pow ( 10, a );
printf ( "%lf\n", x );
system ( "pause" );
return 0;
}
无论是断点调试还是直接查看程序运行结果, 都是100.000000