Common mathematical functions of C language and its usage

Source: Internet
Author: User
Tags abs acos asin base 10 logarithm cos natural log sin square root

Transferred from: http://blog.sina.com.cn/s/blog_8b5a0d0001011779.html

Trigonometric functions: (All parameters must be radians)

1.acos

function declaration: ACOs (double x);
Purpose: Used to return the inverse cosine function of a given X.

2.asin

Function declaration: ASIN (double x);
Purpose: Used to return the inverse sine function of a given X.

3.atan

function declaration: Atan (double x);
Purpose: Used to return the inverse tangent function of a given X.

4.sin

function declaration: Sin (double x);
Purpose: Used to return the sine value of the given X.

5.cos

function declaration: Cos (double x);
Purpose: Used to return the cosine of a given X.

6.tan

function declaration: Tan (double x);
Purpose: Used to return the tangent of a given X.

7.atan2

function declaration: atan2 (double y, double x);
Purpose: Returns the inverse tangent value of the given X and Y coordinate values

Other functions:

8.atof

Function Name: atof (const char *s);
Function: Converts a string to a floating-point number
Usage: Double atof (const char *nptr);
program Example:
#i nclude <stdlib.h>
#i nclude <stdio.h>

int main (void)
{

Float arg,*point=&arg;
float F;
Char *str = "12345.67";

f = atof (str);
printf ("string =%s float =%f\n", str, f);
return 0;
}

9. Ceil and Floor

Function Name: Ceil
Floor
Functions: Rounding up
Rounding down
Usage: Double ceil (double x);
Double floor (double x);
program Example:

#i nclude<math.h>

int main (void)
{
Double number = 123.54;
Double Down, up;

Down = floor (number);
up = ceil (number);

printf ("Original number%5.2lf\n", number);
printf ("Number rounded down%5.2lf\n", down);
printf ("Number rounded up%5.2lf\n", up);

return 0;
The program runs the result: original number 123.54
Number rounded down 123.00
Number rounded up 124.00

10.fabs

Function Name: fabs
Function: Find the absolute value of the floating-point number x.
Usage: fabs (double x);

11.fmod

Function Name: Fmod
Function: Calculates the X-to-y modulus, which is the remainder
Usage: Double fmod (Double x, double y);
program Example:

#i nclude <stdio.h>
#i nclude <math.h>

int main (void)
{
Double x = 5.0, y = 2.0;
Double result;

result = Fmod (x, y);
printf ("The remainder of (%LF/%LF) is \
%lf\n ", x, y, result);
return 0;
}

12.abs

Function Name: ABS
Function: Returns the absolute value of the integer number.
Usage: Abs (number)
The number parameter can be any valid numeric expression. Returns NULL if number contains NULL, or 0 if the variable is uninitialized.

Power Index:

13.exp

Function Name: EXP
Function: Returns the n power of E.
Usage: exp (double x);

14.frexp

Function Name: frexp
function: To decompose a double-precision number into the exponent of the mantissa
Usage: Double frexp (double value, int *eptr);
program Example:

#i nclude <math.h>
#i nclude <stdio.h>

int main (void)
{
Double mantissa, number;
int exponent;
Number = 8.0;
Mantissa = frexp (number, &exponent);
printf ("The number%LF is", number);
printf ("%lf Times to the", mantissa);
printf ("Power of%d\n", exponent);
return 0;
}

15.log

Function Name: Log
Function: Natural logarithmic function ln (x)
Usage: Double log (double x);
program Example:

#i nclude <math.h>
#i nclude <stdio.h>
int main (void)
{
Double result;
Double x = 8.6872;
result = log (x);
printf ("The natural log of%LF is%lf\n", x, result);
return 0;
}

Log (x, y) =ln (y)/ln (x)

16.ldexp

Function Name: Ldexp
Function: Calculates value* (exp power of 2).
Usage: Double ldexp (double value, int exp);
program Example:
#i nclude
#i nclude
int main (void)
{
Double value;
Double x = 2;

Value = Ldexp (x,3);
printf ("The Ldexp value is:%lf\n", value);
return 0;
The result of the operation is: 2*2^3=16.

17.log10

Function Name: log10
Function: Returns the base 10 logarithm.
Usage: log10 (double x);

18.sqrt

Function Name: sqrt
Function: Returns the square root of the specified number.
Usage: sqrt (double x);

19.modf

Function Name: MODF
function: Divide the number into exponent and mantissa
Usage: Double modf (Double value, double *iptr);
program Example:
#i nclude <math.h>
#i nclude <stdio.h>
int main (void)
{
Double fraction, Integer;
Double number = 100000.567;
fraction = MODF (number, &integer);
printf ("The whole and fractional parts of%LF is%lf and%lf\n", number, integer, fraction);
return 0;
}

20.pow

Name of function: POW
Function: Returns the specified number of times the specified power.
Usage: POW (double x, double y);(will return the Y power of X)

Hyperbolic functions:

21.cosh

Function Name: cosh
Function: Returns the hyperbolic cosine of the specified angle.
Usage: Double Cosh (double x (measured in radians));

22.sinh

Function Name: Sinh
Function: Returns the hyperbolic sine value of the specified angle.
Usage: Sinh (double x);(where parameter x must be Radian)

23.tanh

Function Name: Tanh
Function: Returns the hyperbolic tangent value of the specified angle.
Usage: Tanh (double x);

Common mathematical functions of C language and its usage

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.