Home > Article > Backend Development > Introduction to exponential function expressions in C language
Introduction to writing exponential function expressions in C language and code examples
#include <stdio.h> #include <math.h> int main() { double a = 2.0; // 底数 double x = 3.0; // 指数 double result = pow(a, x); // 计算指数函数 printf("指数函数 %lf^%lf 的结果为 %lf ", a, x, result); return 0; }
In the above code, we use the pow() function in the math.h header file to calculate the exponential function. The pow() function accepts two parameters. The first parameter is the base, and the second parameter is the exponent. The function returns the base raised to the power of the exponent.
In the sample program, we set the base to 2 and the exponent to 3, and then calculate the third power of 2 through the pow() function. The final result is stored in the result variable and passed through the printf() function. Print out the calculation results.
The following is a sample program that implements the simulation of exponential decay:
#include <stdio.h> #include <math.h> int main() { double initialAmount = 100.0; // 初始物质量 double decayRate = 0.5; // 衰变速率 double time = 1.0; // 经过的时间(单位:年) double amount = initialAmount * pow(decayRate, time); // 计算衰变后的物质量 printf("经过 %lf 年,物质的量为 %lf ", time, amount); return 0; }
In the above code, we first set the initial material mass to 100 and the decay rate to 0.5. The elapsed time is 1 year. The amount of matter after decay is then calculated using an exponential function. Finally, the calculation results are printed out through the printf() function.
By modifying the decay rate and elapsed time, we can simulate different growth or decay phenomena, such as exponential growth, exponential decay, etc.
To sum up, by using the pow() function, we can easily calculate and simulate exponential functions in C language, which has wide application value.
The above is the detailed content of Introduction to exponential function expressions in C language. For more information, please follow other related articles on the PHP Chinese website!