[Post] C/C ++ mathematical functions

Source: Internet
Author: User
Tags acos asin hypot mathematical functions natural logarithm

ABS

 

Prototype: extern int ABS (int x );

Usage: # include <math. h>

Function: calculates the absolute value of integer x.

Description: calculation | x |. If X is not negative, X is returned. Otherwise,-X is returned.

Example:

// Abs. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Int X;

Clrscr (); // clear screen

X =-5;
Printf ("| % d | = % d/N", X, ABS (x ));
X = 0;
Printf ("| % d | = % d/N", X, ABS (x ));
X = + 5;
Printf ("| % d | = % d/N", X, ABS (x ));

Getchar ();
Return 0;
}

Related functions: FABS

 

ACOs

 

Prototype: extern float ACOs (float X );

Usage: # include <math. h>

Function: returns the arc cosine of X (expressed in radians ).

Note: The value range of X is [-1.0, 1.0], and the value range is [0, π].

Example:

// ACOs. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 0.32;
Printf ("ACOs (%. 2f) = %. 4f", X, ACOs (x ));

Getchar ();
Return 0;
}

Related functions: asin, atan, atan2, sin, cos, Tan

 

Asin

 

Prototype: extern float asin (float X );

Usage: # include <math. h>

Function: returns the arc sine of X (in radians ).

Note: The definition field of X is [-1.0, 1.0], and the value range is [-π/2, + π/2].

Example:

// Asin. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 0.32;
Printf ("asin (%. 2f) = %. 4f", X, asin (x ));

Getchar ();
Return 0;
}

Related functions: ACOs, atan, atan2, sin, cos, Tan

 

 

Atan

 

Prototype: extern float atan (float X );

Usage: # include <math. h>

Function: returns the arc tangent of X (expressed in radians ).

Note: The value range is (-π/2, + π/2 ).

Example:

// Atan. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 0.32;
Printf ("atan (%. 2f) = %. 4f", X, atan (x ));

Getchar ();
Return 0;
}

Related functions: asin, ACOs, atan2, sin, cos, Tan

 

Atan2

 

Prototype: extern float atan2 (float y, float X );

Usage: # include <math. h>

Function: Calculate the arc tangent value of Y/X (expressed in radians ).

Note: The value range is (-π/2, + π/2 ).

Example:

// Atan2.c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X, Y;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 0.064;
Y = 0.2;
Printf ("atan2 (%. 3f, %. 2f) = %. 4f", Y, X, atan2 (Y, x ));

Getchar ();
Return 0;
}

Related functions: asin, ACOs, atan, sin, cos, Tan

 

Ceil

 

Prototype: extern float Ceil (float X );

Usage: # include <math. h>

Function: minimum integer not less than X

Note: The upper limit of X is returned. For example, the upper limit of 74.12 is 75, and the upper limit of-74.12 is-74. The returned value is of the float type.

Example:

// Ceil. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 74.12;
Printf ("Ceil (%. 2f) = %. 0f/N", X, Ceil (x ));
X =-74.12;
Printf ("Ceil (%. 2f) = %. 0f/N", X, Ceil (x ));

Getchar ();
Return 0;
}

Related functions: Floor

 

Cos

 

Prototype: extern float cos (float X );

Usage: # include <math. h>

Function: returns the cosine of X in radians.

Note: The returned values are between [-1.0, 1.0.

Example:

// Cos. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = PI/4 .;
Printf ("cos (%. 4f) = %. 4f/N", X, cos (x ));

Getchar ();
Return 0;
}

Related functions: asin, ACOs, atan, atan2, sin, Tan

 

Cosh

 

Prototype: extern float cosh (float X );

Usage: # include <math. h>

Function: returns the hyperbolic cosine of X.

Note: cosh (x) = (E ^ x + e ^ (-x)/2

Example:

// Cosh. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = PI/4 .;
Printf ("cosh (%. 4f) = %. 4f/N", X, cosh (x ));

Getchar ();
Return 0;
}

Related functions: Sinh, tanh

 

Exp

 

Prototype: extern float exp (float X );

Usage: # include <math. h>

Function: Calculate the X power of E.

Note: E = 2. 718281828...

Example:

// Exp. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("e = % F/N", exp (1.0 ));

Getchar ();
Return 0;
}

Related functions: None

 

FABS

 

Prototype: extern float FABS (float X );

Usage: # include <math. h>

Function: calculates the absolute value of floating point X.

Description: calculation | x |. If X is not negative, X is returned. Otherwise,-X is returned.

Example:

// FABS. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X =-74.12;
Printf ("| % F | = % F/N", X, FABS (x ));
X = 0;
Printf ("| % F | = % F/N", X, FABS (x ));
X = 74.12;
Printf ("| % F | = % F/N", X, FABS (x ));

Getchar ();
Return 0;
}

Related functions: ABS

 

Floor

 

Prototype: extern float floor (float X );

Usage: # include <math. h>

Function: calculates the maximum integer not greater than X.

Note: return the lower limit of X. For example, the lower limit of 74.12 is 74 and the lower limit of-74.12 is-75. The returned value is of the float type.

Example:

// Floor. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 74.12;
Printf ("floor (%. 2f) = %. 0f/N", X, floor (x ));
X =-74.12;
Printf ("floor (%. 2f) = %. 0f/N", X, floor (x ));

Getchar ();
Return 0;
}

Related functions: ceil

 

Fmod

 

Prototype: extern float fmod (float X, float y );

Usage: # include <math. h>

Function: calculates the remainder of x/y.

Returns X-N * y, with the same symbol as Y. N = [x/y] (rounded to the left zero)

Example:

// Fmod. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X, Y;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = 74.12;
Y = 6.4;
Printf ("74.12/6.4: % F/N", fmod (x, y ));
X = 74.12;
Y =-6.4;
Printf ("74.12/(-6.4): % F/N", fmod (x, y ));

Getchar ();
Return 0;
}

Related functions: None

 

Frexp

 

Prototype: extern float frexp (float X, int * exp );

Usage: # include <math. h>

Function: splits floating point X into the ending number and the index.

Note: x = m * 2 ^ exp, M is a normalized decimal. Returns m of the ending number and stores the exponent in Exp.

Example:

// Frexp. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;
Int exp;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = frexp (64.0, & exp );
Printf ("64 = %. 2f * 2 ^ % d", X, exp );

Getchar ();
Return 0;
}

Related functions: ldexp and MODF

 

Hypot

 

Prototype: extern float hypot (float X, float y );

Usage: # include <math. h>

Function: determine the length of the oblique side for the two straight corner edges of a given right triangle.

Description: return the oblique edge value.

Example:

// Hypot. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("3 ^ 2 + 4 ^ 2 = %. 0f ^ 2/N", hypot (3., 4 .));
Printf ("3.2 ^ 2 + 4.3 ^ 2 = %. 2f ^ 2", hypot (x, y ));

Getchar ();
Return 0;
}

Related functions: frexp and ldexp

 

Ldexp

 

Prototype: extern float ldexp (float X, int exp );

Usage: # include <math. h>

Function: loads floating point numbers.

Returns x * 2 ^ exp.

Example:

// Ldexp. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = ldexp (1.0, 6); // 1.0*2 ^ 6
Printf ("2 ^ 6 = %. 2f", X );

Getchar ();
Return 0;
}

Related functions: frexp and MODF

 

Log

 

Prototype: extern float log (float X );

Usage: # include <math. h>

Function: Calculate the natural logarithm of X.

Note: The value of X must be greater than zero.

Example:

// Log. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("ln (e) = % F/N", log (m_e); // m_e is 2. 71828..., defined in math. h

Getchar ();
Return 0;
}

Related functions: log10

 

Log10

 

Prototype: extern float log10 (float X );

Usage: # include <math. h>

Function: Calculate the common logarithm of X.

Note: The value of X must be greater than zero.

Example:

// Log10.c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("lg (5) = % F/N", log10 (5.0 ));

Getchar ();
Return 0;
}

Related functions: Log

 

 

MODF

 

Prototype: extern float MODF (float num, float * I );

Usage: # include <math. h>

Function: splits the floating point num into integer and decimal parts.

Description: returns the fractional part, which is stored in the memory indicated by * I.

Example:

// MODF. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X, I;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = MODF (-74.12, & I );
Printf ("-74.12 = %. 0f + (%. 2f)", I, X );

Getchar ();
Return 0;
}

Related functions: frexp and ldexp

 

Pow10

 

Prototype: extern float pow10 (float X );

Usage: # include <math. h>

Function: Calculate the X power of 10.

Note: It is equivalent to POW (10.0, X ).

Example:

// Pow10.c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("10 ^ 3.2 = % F/N", power10 (3.2 ));
Printf ("10 ^ 3.2 = % F", POW (10, 3.2 ));

Getchar ();
Return 0;
}

Related functions: pow

 

POW

 

Prototype: extern float POW (float X, float y );

Usage: # include <math. h>

Function: Calculate the Power Y of X.

NOTE: If X is greater than zero, the result of the power index is returned.

Example:

// Pow. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("4 ^ 5 = % F", POW (4., 5 .));

Getchar ();
Return 0;
}

Related functions: pow10

 

Sin

 

Prototype: extern float sin (float X );

Usage: # include <math. h>

Function: Calculate the sine of X (expressed in radians.

Note: The value range of X is [-1.0, 1.0].

Example:

// Sin. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = m_pi/2; // m_pi = Pi = 3. 14159265..., defined in math. h
Printf ("Sin (PI/2) = % F", sin (x ));

Getchar ();
Return 0;
}

Related functions: asin, ACOs, atan, atan2, cos, Tan

 

 

Sinh

 

Prototype: extern float sinh (float X );

Usage: # include <math. h>

Function: calculates the hyperbolic sine of X (expressed in radians.

Note: sinh (x) = (E ^ X-e ^ (-x)/2.

Example:

// Sinh. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = PI/4 .;
Printf ("sinh (%. 4f) = %. 4f/N", X, sinh (x ));

Getchar ();
Return 0;
}

Related functions: cosh, tanh

 

SQRT

 

Prototype: extern float SQRT (float X );

Usage: # include <math. h>

Function: calculates the square root of X.

Note: X must be greater than or equal to zero.

Example:

// SQRT. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

Printf ("SQRT (2000) = % F", SQRT (2000.0 ));

Getchar ();
Return 0;
}

Related functions: None

 

Tan

 

Prototype: extern float Tan (float X );

Usage: # include <math. h>

Function: calculates the tangent of X (expressed in radians.

Returns the tangent of X.

Example:

// Tan. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;
Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = m_pi/4; // m_pi = Pi = 3. 14159265..., defined in math. h
Printf ("Tan (PI/4) = % F", Tan (x ));

Getchar ();
Return 0;
}

Related functions: asin, ACOs, atan, atan2, sin, cos

 

Tanh

 

Prototype: extern float Tanh (float X );

Usage: # include <math. h>

Function: returns the hyperbolic tangent of X.

Note: Tanh (x) = (E ^ X-e ^ (-x)/(E ^ 2 + e ^ (-x ))

Example:

// Tanh. c

# Include <syslib. h>
# Include <math. h>

Main ()
{
Float X;

Clrscr (); // clear screen
Textmode (0x00); // 6 lines per LCD screen

X = PI/4 .;
Printf ("Tanh (%. 4f) = %. 4f/N", X, Tanh (x ));

Getchar ();
Return 0;
}

Related functions: Sinh, cosh

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.