Concepts / Standard Mathematical Functions in C++

Standard Mathematical Functions in C++

C++ provides mathematical functions beyond the standard set, so programmers may need to look beyond the general reference.

  • Programming

A Function Name You Do Not Recognize

Most C++ programmers meet mathematical functions early. The functions they use first are usually the ones most people call the standard set, and they cover a lot of everyday work: the absolute value of a difference, the square root of an area, the sine of an angle, or the logarithm of a measurement. C++ does not ask you to write these calculations from scratch. The standard library supplies ready-made functions, and you call them by name with a value in parentheses. Each call takes an argument and produces a result that your program can store, compare, or print.

Once a program needs something more specialized, a natural question follows: is there more available, and if so, where does it come from? Reading a program and meeting a function you do not recognize is a common moment. This article treats the extra functions not as one long list but as a map with a few distinct regions. Knowing the region tells you where to find the function's specification and what kind of guarantees to expect from its documentation.

Three Regions of Mathematical Functions

The extra functions do not all live in one place. The first region is the standard set of mathematical functions, provided by the cmath library. This is the baseline the rest of the article builds on. The second region is a small group of additional functions found in the cstdlib header. The third region is the family of special mathematical functions, which C++ treats as a separate topic with its own ISO standard.

The diagram makes one distinction clear. The cstdlib functions are an addition to the standard picture, not a separate subject, so they remain part of the general mathematical toolkit. Special mathematical functions form a branch with their own governing document. That is why they are treated as a distinct topic rather than a simple extension of the header. Specifically, the special functions are covered by a separate ISO standard, cited in the source as C++Math, 2010. A programmer looking for them should not expect them to be described in the same place as the everyday standard functions.

Tracing a Name to Its Reference

When you meet an unfamiliar function, you do not need to search at random. You can narrow the search by asking three questions in sequence. Each answer sends you to a different reference, so the process works like a short trace through a decision path.

  1. 1
    Question 1: Is the function part of the standard mathematical functions in cmath? If yes, its description is in the standard reference table.
  2. 2
    Question 2: If not, is it one of the few additional functions provided through cstdlib? If yes, look in that header's documentation.
  3. 3
    Question 3: If not, does it belong to the special mathematical functions covered by the separate ISO standard? If yes, look in that standard, not in the everyday reference.
  4. 4
    Stop at the first yes. Each answer moves you to a different reference, so the three-part map lets you go from an unfamiliar name to the right document without guessing.

The Standard Set in cmath

The reference table lists sixteen standard functions with their syntax and a short description. They fall into families, which makes the table easier to scan. You can first decide which family your problem belongs to, and then look up the specific name inside it.

FamilyFunctions in the tableWhat the family is for
Absolute value and roundingabs(x), ceil(x), floor(x)Size of a value, and moving a value to a nearby integer
Square rootsqrt(x)A function of its own
Trigonometricsin(x), cos(x), tan(x), acos(x), asin(x), atan(x)Sine, cosine, and tangent, along with their inverses
Hyperbolicsinh(x), cosh(x), tanh(x)Hyperbolic sine, cosine, and tangent
Exponential and logarithmicexp(x), log(x), log10(x)Base e exponential, natural log, and base 10 log

The sixteen standard functions grouped into the families used in the reference table

The source states that the functions in the cmath library, such as sqrt, log, and sin, accept float, double, and long double arguments. When you read a function's description, check which argument types it lists and which conditions apply to its input.

Absolute Value and Rounding Direction

Absolute value and the two rounding functions are the simplest members of the table, but they are easy to misread. The function abs(x) returns the absolute value of x, so a negative input comes back as its positive counterpart. The rounding functions do not round to the nearest integer in the everyday sense. Instead, ceil(x) returns the smallest integer that is greater than or equal to x, written $\lceil x \rceil$, and floor(x) returns the largest integer that is less than or equal to x, written $\lfloor x \rfloor$.

What do you think happens?

What do you think floor(-2.5) returns?

Reveal answer
Answer:

-3

floor(x) returns the largest integer that is less than or equal to x. The integers at or below -2.5 are -3, -4, and so on, and the largest of them is -3. The result moves toward negative infinity, not toward zero.

Read the number line from left to right. The input -2.5 sits between -3 and -2. Ceiling moves to the integer on the right, -2, because that is the smallest integer not less than -2.5. Floor moves to the integer on the left, -3, because that is the largest integer not greater than -2.5. For negative values, ceil moves toward zero and floor moves away from zero, which is the direction the source describes as the largest integer below the input.

cpp
Output
7
-2
-3
4
3
Notes on the code
  • abs returns the positive counterpart of a negative input
  • ceil(-2.5) moves right to the smallest integer not less than -2.5
  • floor(-2.5) moves left to the largest integer not greater than -2.5
  • ceil(3.2) and floor(3.7) show the same rule for positive values

Square Root, Logarithms, and Valid Inputs

The square root function sqrt(x) requires x to be non-negative, so passing a negative value is outside its valid input range. The logarithm log(x) requires a positive x. These are domain rules: they describe which inputs a call can handle meaningfully. The source advises that the input condition should be checked before the call, so the check belongs in your code before the function runs.

Worked example

Tracing a call to sqrt with a negative input

A program computes sqrt(x) where x is -4.0. Should the call be made, and what does the description require?

  1. 1
    Identify the function

    sqrt is in the standard set in cmath, so the reference table is the right place to look.

  2. 2
    Read the input condition

    The description requires x to be non-negative, written $x \geq 0$.

  3. 3
    Compare the input to the condition

    The value -4.0 is negative, so it falls outside the valid input range.

  4. 4
    Decide the action

    Guard the call with a check, such as testing x >= 0.0 first, and handle the negative case in code rather than passing it to sqrt.

Result

The call is outside the valid domain. Check the input before calling sqrt, because a negative value does not produce a meaningful square root in this function's contract.

Trigonometric, Hyperbolic, and Exponential Families

The trigonometric family is sin(x), cos(x), and tan(x), which compute the sine, cosine, and tangent of the value you pass in. Their inverses are acos(x), asin(x), and atan(x). The reference table adds two notes about inverse results: acos(x) returns a non-negative result, and asin(x) returns the result nearest to 0. Read these notes as output rules, because they tell you which value the function will choose.

The hyperbolic functions sinh(x), cosh(x), and tanh(x) compute the hyperbolic sine, cosine, and tangent. They appear in the same table as the trigonometric functions, and you call them the same way, by passing a value inside the parentheses. The calling pattern is the same across these families, so the main thing to learn for each one is its description.

The exponential function exp(x) computes the base e exponential. The logarithm functions invert that idea in different bases. The function log(x) computes the natural logarithm, which is base e, while log10(x) computes the base 10 logarithm. Comparing them side by side makes the difference clear: both take a positive input, but they measure against different bases.

cpp
Output
1
0
3
0
Notes on the code
  • exp(0.0) computes the base e exponential of zero
  • log(1.0) is the natural logarithm of one
  • log10(1000.0) is the base 10 logarithm, which is 3 because 10 cubed is 1000
  • sin(0.0) is the sine of zero

Regions Beyond the Standard Set

The cstdlib functions are an addition to the standard picture. The source says a few additional mathematical functions are found in the cstdlib header, so they remain part of the general mathematical toolkit rather than a separate subject. The source does not list which functions these are or explain why they are placed there, so when you meet one, consult the cstdlib documentation directly instead of assuming its behavior.

Special mathematical functions form a different kind of extension. They are covered by a separate ISO standard, cited in the source as C++Math, 2010. The practical consequence is that these functions are specified in their own document. Looking for one in the everyday standard reference will not find its specification. Sorting a function into its region tells you where its specification lives, and that is the main reason to classify before you look.

Mistakes Beginners Make with These Functions

  • Common mistake

    Assuming ceil and floor round to the nearest integer

    What it looks like
    floor(-2.5) gives -3, not -2
    Why it fails

    ceil(x) returns the smallest integer greater than or equal to x, and floor(x) returns the largest integer less than or equal to x. Neither is nearest-integer rounding.

    Do this instead

    For a negative value, floor moves toward negative infinity and ceil moves toward zero.

  • Common mistake

    Calling sqrt or log without checking the input

    What it looks like

    sqrt(-4.0) passed straight through to the function

    Why it fails

    sqrt requires a non-negative x and log requires a positive x. Outside those domains the result is not meaningful.

    Do this instead

    Check the condition before the call, as the source advises.

  • Common mistake

    Confusing log with log10

    What it looks like
    Expecting log(1000.0) to return 3
    Why it fails

    log(x) is the natural logarithm, base e. log10(x) is the base 10 logarithm.

    Do this instead

    Use log10 when you need base 10, and log when you need base e.

  • Common mistake

    Looking for every mathematical function in the same reference

    Why it fails

    The standard set, the cstdlib additions, and the special functions each have their own source of specification.

    Do this instead

    Use the three-question trace to pick the right reference before reading a description.

Practice Predicting Results

EASY

Without running the code, predict the result of each call: ceil(4.1), floor(-0.5), abs(-9.0), and sqrt(0.0). For each one, state which description rule you used. Then check your answers by writing a short program that prints each result.

Hints
  • ceil returns the smallest integer greater than or equal to its input, so ceil(4.1) moves up to the next integer.
  • floor returns the largest integer less than or equal to its input. For -0.5, the integers at or below it are -1, -2, and so on.
  • abs removes the sign of a negative input.
  • sqrt requires a non-negative input, and 0.0 is allowed.

Key Takeaways

  • C++ mathematical functions come from three regions: the standard set in cmath, a few additional functions in cstdlib, and special mathematical functions specified by a separate ISO standard cited as C++Math, 2010.
  • abs(x) returns the absolute value, ceil(x) returns the smallest integer greater than or equal to x, and floor(x) returns the largest integer less than or equal to x, so rounding direction matters for negative values.
  • sqrt(x) requires a non-negative x and log(x) requires a positive x, so check the input condition before calling them.
  • Trigonometric, hyperbolic, and exponential functions are called the same way, and log(x) is base e while log10(x) is base 10.
  • Read a function's description for its domain and output rules, since those rules let you predict the result of a call.

Related concepts

More concepts starting with S