Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

D.2 The math.h Module

#include <math.h>

This module defines a collection of standard mathematical functions, which use floating-point values, and associated constants.

Constants

double M_PI

An approximation for the value of π .

double M_PI_2

The value of π divided by 2.

double M_PI_4

The value of π divided by 4.

double M_E

The value e , which is the base of the natural logarithms.

Functions

Power Functions
double sqrt(double x);
Computes and returns the square root of x.
double pow(double x, double y);
Computes and returns the power xy .
double cbrt(double x);
Computes and returns the cube root of x.
double hypot(double x, double y);
Computes and returns the hypotenuse of a right-angled triangle with sides of length x and y .
Trigonometric Functions
double sin(double x);
Computes and returns the sine of an angle given in radians.
double cos(double x);
Computes and returns the cosine of an angle given in radians.
double tan(double x);
Computes and returns the tangent of an angle given in radians.
double asin(double x);
Computes and returns the angle with the given sine.
double acos(double x);
Computes and returns the angle with the given cosine.
double atan(double x);
Computes and returns the angle with the given tangent.
double atan2(double y, double x);
Computes and returns the angle with the given tangent, y/x .
Rounding and Remainder Functions
double ceil(double x);
Returns the smallest integer x (as a floating-point value).
double floor(double x);
Returns the largest integer x (as a floating-point value).
double trunc(double x);
Rounds x toward zero.
double round(double x);
Rounds x to the nearest integer, away from zero.
Exponential and Logarithmic Functions
double log(double x);
double log10(double x);
double log2(double x);
double exp(double x);
double exp2(double x);
Absolute Functions
double fabs(double x);

Detailed Description

double sqrt(double x);
Computes and returns the square root of x.
Parameters:
x
The numerical value for which the square root is to be computed.
Returns:
The square root of x.
double pow(double x, double y);
Computes and returns the power xy . If x>0 , y can be any value. If x is zero, y must be >0. If x<0, y must be an integer.
Parameters:
x
The numerical value of the base that raised to a power.
y
The numerical value of the exponent to which the based is raised.
Returns:
The value of xy .
double cbrt(double x);
Computes and returns the cube root of x.
Parameters:
x
The numerical value whose cubic root is computed.
Returns:
The cube root of x or x 3 .
double hypot(double x, double y);
Computes and returns the hypotenuse of a right-angled triangle with sides of length x and y .
Parameters:
x, y
The numerical values corresponding to the legs of a right-angled triangle for which the hypotenuse is computed.
Returns:
The square root of ( x 2 + y 2 ) .
double sin(double x);
Computes and returns the sine of an angle given in radians.
Parameters:
x
An angle in radians.
Returns:
The sine of the argument.
double cos(double x);
Computes and returns the cosine of an angle given in radians.
Parameters:
x
An angle in radians.
Returns:
The cosine of the argument.
double tan(double x);
Computes and returns the tangent of an angle given in radians.
Parameters:
x
An angle in radians.
Returns:
The tangent of the argument.
double asin(double x);
Computes and returns the angle with the given sine.
Parameters:
x
A floating-point value between -1 and 1.
Returns:
The arc sine of the argument in radians; the value is in the range [ - π / 2 π / 2 ] .
double acos(double x);
Computes and returns the angle with the given cosine.
Parameters:
x
A floating-point value between -1 and 1.
Returns:
The arc cosine of the argument in radians; the value is in the range [ 0 π ] .
double atan(double x);
Computes and returns the angle with the given tangent.
Parameters:
x
A floating-point value.
Returns:
The arc tangent of the argument in radians; the value is in the range [ - π / 2 π / 2 ] .
double atan2(double y, double x);
Computes and returns the angle with the given tangent, y/x . The signs of the two arguments are used to determine the quadrant of the result. If x can equal zero, or if it is necessary to distinguish "northwest" from "southeast" and "northeast" from "southwest", use this function instead of atan(y/x).
Parameters:
x, y
Two floating-point values.
Returns:
The angle, in radians, in the range [ - π π ] .
double ceil(double x);
Returns the smallest integer x (as a floating-point value).
Parameters:
x
The floating-point value to round up.
Returns:
The smallest integral value that is not less than x (as a floating-point value).
double floor(double x);
Returns the largest integer x (as a floating-point value).
Parameters:
x
The floating-point value to round down.
Returns:
The largest integral value less than or equal to x (as a floating-point value).
double trunc(double x);
Rounds x toward zero.
Parameters:
x
The floating-point value to truncate.
Returns:
The nearest integer value that is not larger in magnitude than x (as a floating-point value).
double round(double x);
Rounds x to the nearest integer, away from zero.
Parameters:
x
The floating-point value to round.
Returns:
The value of x rounded to the nearest integer (as a floating-point value).
double log(double x);
Parameters:
x
double log10(double x);
Parameters:
x
double log2(double x);
Parameters:
x
double exp(double x);
Parameters:
x
double exp2(double x);
Parameters:
x
double fabs(double x);
Parameters:
x