Concepts / Mathematical Functions in C++

Mathematical Functions in C++

Mathematical functions are one of the parts of the library that serve numerics, and they are a natural first topic because they are the everyday building blocks that many numeric methods rely on.

  • Programming

A Risk Figure Nobody Can Act On

Imagine a desktop application that shows a trader the current risk of a portfolio. The program has to read prices from a database, receive live quotes over a network connection, run a calculation that estimates how much the portfolio could lose on a bad day, and draw a chart on the screen. Only one of those four jobs is the arithmetic itself. The other three handle data access, communication, and display.

This article is about the arithmetic step, and specifically the ready-made mathematical functions that C++ provides for it, such as absolute value, rounding, square roots, trigonometry, exponentials, and logarithms. You will trace how each family of function behaves on real inputs, learn which inputs are valid, and practice predicting a result before you run the code. The point of all this, as later sections explain, is not the number itself but the understanding it supports.

Where C++ Shows Up in Numeric Work

C++ is widely used for scientific, engineering, and financial computation that involves sophisticated numerics. The source also notes that the same is true for other fields with similar needs. In each of these areas, the numbers are rarely the end product. They feed models, reports, decisions, and displays.

Numeric computation usually happens in the middle of other work, such as database access, networking, instrument control, graphics, simulation, and financial analysis. The calculation is one component of a bigger machine. When the calculation is part of a larger system, the language used to write that system matters, because the numeric step has to fit comfortably alongside everything around it. This is the setting in which C++ becomes an attractive choice.

It may seem surprising that C++ is so widely used for numerics, because it was not designed primarily with numeric computation in mind. A language does not need to be created for a task in order to be useful for it. What matters is whether its strengths match the demands of the work. When numeric computation is embedded in a larger system, those demands include handling several kinds of work in one program, and that is where the language's strengths become relevant.

Why Numeric Methods Outgrew Simple Loops

A common picture of numeric code is a loop that walks through a vector of floating-point numbers and adds or multiplies them. The source explains that numeric methods have moved well beyond that picture. Many modern methods need richer organization of their data, and once a computation requires more complex data structures, the strengths of C++ become relevant.

In the real world

Suppose a simulation tracks many objects, each with its own position, history, and set of neighboring objects. The arithmetic for each object is simple, but the data describing the whole collection is not a single flat list. When the data becomes this structured, the choice of language affects how naturally the computation can be expressed. (This is an illustrative example written for this article, not a case taken from the source.)

Why the Result Matters More Than the Number

Before looking at how computation is organized, it helps to ask what computation is for. The source quotes R. W. Hamming, who wrote that the purpose of computing is insight, not numbers. A numeric result is valuable only when it helps someone understand something: whether a bridge design is safe, whether a price is fair, or whether a simulated weather pattern matches observations.

Returning to the portfolio example, a risk figure of a particular size is only a number until the trader can see what it means for the decision at hand. This is why the chart in that program matters as much as the calculation behind it. Keep this in mind as you work through the functions below: each call produces a value that a program stores, compares, or displays, and the value earns its place by what it lets someone conclude.

The Standard Library's Math Toolkit

Because numeric computation is so common, facilities and techniques supporting it have emerged, and the standard library is part of that support. A programmer does not have to build every numeric tool from nothing. Mathematical functions are one of the parts of the library that serve numerics, and they are a natural first topic because they are the everyday building blocks that many numeric methods rely on.

Definition

The <cmath> library provides standard mathematical functions such as sqrt, log, and sin. These functions accept float, double, and long double arguments. You call them by name with a value in parentheses, and each call produces a result that your program can store, compare, or print.

The functions in the reference fall into families. Absolute value and the two rounding functions form one group. The square root is a function of its own. Sine, cosine, and tangent, along with their inverses, form the trigonometric family. Sinh, cosh, and tanh form the hyperbolic family. Exp, log, and log10 form the exponential and logarithmic family. Deciding which family your problem belongs to is the first step in finding the right name.

cpp
Output
4
Notes on the code
  • The <cmath> header brings the math functions into scope.
  • std::sqrt is called by name, with the value in parentheses.
  • The returned value is stored in side, just as any other value would be.

Tracing Rounding with floor and ceil

Absolute value and the two rounding functions are the simplest members of the table, but they are easy to misread. 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, and floor(x) returns the largest integer that is less than or equal to x. Written with notation, these are $\lceil x \rceil$ for ceil and $\lfloor x \rfloor$ for floor.

What do you think happens?

Before reading on, predict the value of floor(-2.5). Is it -2 or -3?

  • -2

  • -3

Reveal answer
Answer:

-3

floor(x) returns the largest integer that is less than or equal to x. For -2.5, the integers that are less than or equal to -2.5 are -3, -4, and so on, and the largest of them is -3. Moving left on the number line from -2.5 reaches -3.

-3floor(-2.5)-2.5input-2ceil(-2.5)floorceil
Starting from the input -2.5, floor moves to the largest integer not above it, which is -3, while ceil moves to the smallest integer not below it, which is -2.
Worked example

Tracing floor and ceil on a negative and a positive input

What do floor(-2.5), ceil(-2.5), floor(2.5), and ceil(2.5) return?

  1. 1
    Apply the floor rule to -2.5

    Floor needs the largest integer that is less than or equal to -2.5. Candidates are -2, -3, -4, and so on. Values above -2.5 such as -2 are too large, so the answer is -3.

  2. 2
    Apply the ceil rule to -2.5

    Ceil needs the smallest integer that is greater than or equal to -2.5. The integer -2 is greater than -2.5, and no smaller integer satisfies the rule, so the answer is -2.

  3. 3
    Apply the floor rule to 2.5

    The largest integer less than or equal to 2.5 is 2, so floor(2.5) is 2.

  4. 4
    Apply the ceil rule to 2.5

    The smallest integer greater than or equal to 2.5 is 3, so ceil(2.5) is 3.

Result

floor(-2.5) is -3, ceil(-2.5) is -2, floor(2.5) is 2, and ceil(2.5) is 3. For negative inputs, ceil moves toward zero and floor moves away from it.

cpp
Output
-3
-2
2
3
7.5
Notes on the code
  • Each call is a separate function call, so each result prints on its own line.
  • abs(-7.5) returns the positive counterpart, 7.5.
  • Compare the first two lines: the same input moves in opposite directions depending on which rounding function you choose.

Domain Rules: Inputs That Break a Call

Every math function accepts some inputs and not others. The description column of the reference includes conditions on the input, and those conditions decide whether a call produces a meaningful value. 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, and the reference is explicit about this condition, separate from the square root's own requirement.

abs(x)returns absolute value of xsqrt(x)requires x of zero or morelog(x)requires x greater thanzerolog10(x)base 10 logarithm of x
Each function is paired with the condition its input must meet. A call outside that condition is not a meaningful result, so the input should be checked before the call.

Trigonometric, Exponential, and Logarithmic Calls

Trigonometric, hyperbolic, exponential, and logarithmic functions are called the same way as the functions you have already seen: by name, with a value in parentheses. The difference lies in what each one computes and what its input must satisfy. Reading the description for each name is the reliable way to predict its result.

Tracing exp and log Back and Forth

The logarithm and the exponential are inverses of each other, which follows from their definitions. The exponential computes base e raised to a power, and the natural logarithm asks which power of e produces a given value. Applying one after the other therefore returns the original value. Following the value through the chain makes this visible.

xstarting valueexp(x)base e exponentialxreturned by logexplog
Starting from x, applying exp produces exp(x), and applying log to that result brings the value back to x, because log is the inverse of exp.
cpp
Output
2
Notes on the code
  • y holds exp(2.0), the base e exponential of 2.
  • back applies the natural logarithm to y, moving the value back to 2.
  • The printed result matches the starting value, as the inverse relationship predicts.

Common Mistakes with Math Functions

  • Common mistake

    Assuming ceil and floor round to the nearest integer

    What it looks like

    Expecting ceil(-2.5) to give -3 because it is the closer integer.

    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 one means nearest.

    Do this instead

    For -2.5, ceil gives -2 and floor gives -3. Read the definition, not the everyday meaning of the word round.

  • Common mistake

    Calling sqrt or log without checking the input

    What it looks like

    Calling sqrt on a negative balance, or log on a value that can be zero.

    Why it fails

    sqrt(x) requires x to be non-negative and log(x) requires x to be positive. Outside those conditions the result is not a meaningful value.

    Do this instead

    Check the condition before the call, and handle invalid inputs in the program.

  • Common mistake

    Mixing up log and log10

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

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

    Do this instead

    Use log10 when you need the base 10 logarithm. The example above gives 3 for log10(1000.0).

  • Common mistake

    Expecting abs to be needed only for integers, when the same idea applies to floating-point values such as abs(-7.5)

Practice Predicting Results

MEDIUM

Without running the code, predict the printed value of each expression: (a) std::ceil(-0.5), (b) std::floor(-0.5), (c) std::log10(100.0), (d) std::sqrt(-9.0). For each one, state whether the input satisfies the function's domain before you predict the value.

Hints
  • Ceil returns the smallest integer that is greater than or equal to the input. For -0.5, check which integers are greater than or equal to -0.5.
  • Floor returns the largest integer that is less than or equal to the input. For -0.5, check which integers are less than or equal to -0.5.
  • Log10 is base 10, so ask which power of 10 gives 100.
  • Sqrt requires a non-negative input. Decide whether -9.0 satisfies that condition before predicting anything.

Key Takeaways

  • C++ is widely used for scientific, engineering, financial, and related numeric work, especially when the calculation sits inside a larger system that also handles data access, networking, or display, even though the language was not designed primarily for numerics.
  • The <cmath> library supplies ready-made functions such as abs, ceil, floor, sqrt, trigonometric, hyperbolic, exponential, and logarithmic functions. They accept float, double, and long double arguments and are called by name with a value in parentheses.
  • 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. For negative values they move in opposite directions from the input, so floor(-2.5) is -3 and ceil(-2.5) is -2.
  • sqrt(x) requires a non-negative x and log(x) requires a positive x, so the input condition should be checked before the call. log(x) is base e and log10(x) is base 10, and exp and log are inverses of each other.
  • The purpose of computing is insight rather than numbers, so a function's result matters for the understanding and decisions it supports.

Related concepts

More concepts starting with M