The method of solving power function and exponential function with C language _c language

Source: Internet
Author: User
Tags pow square root

C Language Pow () function: To find X's y-square (Power)
header file:

#include <math.h>

The POW () function is used to find the Y power of X (the second side), and its prototype is:

  Double pow (double x, double y);

POW () is used to calculate Y-square values with x as the base, and then returns the result. Set the return value to RET, then ret = xy.

Conditions that may cause an error:

    • If base x is negative and exponent y is not an integer, it will cause a domain error error.
    • If both base x and exponent y are 0, it may or may not be the domain error error, which is related to the library implementation.
    • If base x is 0, exponential y is a negative number and may cause domain error or pole error errors, or not;
    • If the return value RET is too large or too small, it will cause a range error error.

Error code:

    • If a domain error error occurs, the global variable errno is set to EDOM;
    • If a pole error or range error error occurs, the global variable errno is set to Erange.

Note that when compiling with GCC, please join-LM.

"Instance" see the code below.

#include <stdio.h>
#include <math.h>
int main ()
{
  printf ("7 ^ 3 =%f\n", pow (7.0, 3.0)); 
   printf ("4.73 ^ =%f\n", pow (4.73, 12.0));
  printf ("32.01 ^ 1.54 =%f\n", pow (32.01, 1.54));
  return 0;
}

Output results:

7 ^ 3 = 343.000000
4.73 ^ a = 125410439.217423
32.01 ^ 1.54 = 208.036691

C language sqrt () function: To find the square root of a given value
header file:

#include <math.h>

sqrt () is used to find the square root of a given value, and its prototype is:

  Double sqrt (double x);

The "parameter" x is the value to compute the square root.

If x < 0, the domain error error is caused and the value of the global variable errno is set to EDOM.

return value returns the X square root.

Note that when compiling with GCC, please join-LM.

The instance calculates the square root of 200. 】

#include <math.h>
Main () {
  double root;
  root = sqrt (m);
  printf ("Answer is%f\n", root);
}

Output results:

Answer is 14.142136

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.