ArticleslgStudy

mathematics

C and C++ mathematics facilities

C and C++ mathematics facilities is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C and C++ mathematics facilities rather than just read about it. In short: C and C++ mathematics facilities are a group of functions in the standard libraries of the C and C++ programming language. The C standard library provides the header <math.h>.

Key takeaways

  • C and C++ mathematics facilities belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C and C++ mathematics facilities to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C and C++ mathematics facilities from memory before moving on to harder problems.

Reference excerpt

C and C++ mathematics facilities are a group of functions in the standard libraries of the C and C++ programming language. The C standard library provides the header <math.h>. The C++ Standard Library provides <cmath>, which includes additional functionalities, as well as other headers such as <random> for random number generation.

General mathematics functions Most of the mathematical functions, which use floating-point numbers, are defined in <math.h> (<cmath> header in C++). The functions that operate on integers, such as abs(), labs(), div(), and ldiv(), are instead defined in the <stdlib.h> header (<cstdlib> header in C++). Any functions that operate on angles use radians as the unit of angle. Not all of these functions are available in the C89 version of the standard. For those that are, the functions accept only type double for the floating-point arguments, leading to expensive type conversions in code that otherwise used single-precision float values. In C99, this shortcoming was fixed by introducing new sets of functions that work on float and long double arguments. Those functions are identified by f and l suffixes respectively. In C++26, many of these functions were made constexpr.

Floating-point environment C99 adds several functions and types for fine-grained control of floating-point environment. These functions can be used to control a variety of settings that affect floating-point computations, for example, the rounding mode, on what conditions exceptions occur, when numbers are flushed to zero, etc. The floating-point environment functions and types are defined in <fenv.h> header (<cfenv> in C++).

Checked integer arithmetic C23 adds <stdckdint.h>, which provides checked integer arithmetic. In C23, these are macros; in C++ (since C++26), they are function templates. All functions take as a first parameter a pointer to the result, while the second and third parameters are the arguments themselves. The functions return false if the value assigned to *result correctly represents the mathematical result of the addition; otherwise true.

Complex numbers

C complex utilities C99 adds a new _Complex keyword (and complex convenience macro; only available if the <complex.h> header is included) that provides support for complex numbers. Any floating-point type can be modified with complex, and is then defined as a pair of floating-point numbers. Note that C and C++ do not implement complex numbers in a code-compatible way – the latter instead provides the class std::complex<T> (for a floating point type T), which provides methods with constexpr support. All operations on complex numbers are defined in the <complex.h> header. As with the real-valued functions, an f or l suffix denotes the float complex or long double complex variant of the function.

A few more complex functions are "reserved for future use in C99". Implementations are provided by open-source projects that are not part of the standard library.

C++ complex utilities

Types C++ has its own type for a complex number, std::complex. Unlike C's built-in complex types, C++'s complex numbers have built-in methods.

Functions

Type-generic functions

The header <tgmath.h> defines a type-generic macro for each mathematical function defined in <math.h> and <complex.h>. This adds a limited support for function overloading of the mathematical functions: the same function name can be used with different types of parameters; the actual function will be selected at compile time according to the types of the parameters. Each type-generic macro that corresponds to a function that is defined for both real and complex numbers encapsulates a total of 6 different functions: float, double and long double, and their complex variants. The type-generic macros that correspond to a function that is defined for only real numbers encapsulates a total of 3 different functions: float, double and long double variants of the function. The C++ language includes native support for function overloading and thus does not provide the <tgmath.h> header even as a compatibility feature.

Random-number generation

The header <stdlib.h> (<cstdlib> in C++) defines several functions that can be used for statistically random number generation.

The arc4random family of random number functions are not defined in POSIX standard, but is found in some common libc implementations. It used to refer to the keystream generator of a leaked version of RC4 cipher (hence "alleged RC4"), but different algorithms, usually from other ciphers like ChaCha20, have been implemented since using the same name. The quality of randomness from rand() are usually too weak to be even considered statistically random, and it requires explicit seeding. It is usually advised to use arc4random instead of rand when possible. Some C libraries implement rand using arc4random_uniform internally. The C++ standard library provides another header, <random>, for its own random number generation facilities, in particular random number engines and probability distributions.

Special functions Since C++17, C++ introduces special functions into the <cmath> header.

Algorithms These symbols are found in the <algorithm> header.

Minimum/maximum

Set operations

Comparisons

Permutation operations

Numeric algorithms All symbols in this subsection are found in the <numeric> header.

Factor operations C++17 introduces these factoring operations:

Interpolation operations C++20 introduces these interpolation operations:

Saturation arithmetic C++26 introduces these saturation arithmetic operations:

Carry-less product C++29 introduces the following carry-less product operations:

Ratios C++11 introduces the <ratio> header, which provides compile-time rational arithmetic (ratios) as a type. The std::ratio class represents an exact rational fraction. These are used in the std::chrono library, for example, to represent units of time.

Pre-defined ratios The provided metric prefixes are the following:

Functions

Linear algebra C++26 introduces linear algebra based on the Basic Linear Algebra Subprograms (BLAS) library in the <linalg> header, in namespace std::linalg. Note that here, the tensor type being referred to is std::mdspan.

Types

In-place transformations

BLAS 1 functions

BLAS 2 functions

BLAS 3 functions

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with C and C++ mathematics facilities

Start with the simplest possible case. Write down what C and C++ mathematics facilities claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C and C++ mathematics facilities before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C and C++ mathematics facilities ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C and C++ mathematics facilities

In research
C and C++ mathematics facilities appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C and C++ mathematics facilities in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C and C++ mathematics facilities is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++ Standard Library, C standard library, so understanding it makes those chapters shorter.
In everyday life
Look for C and C++ mathematics facilities outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C and C++ mathematics facilities” →

How to study C and C++ mathematics facilities in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C and C++ mathematics facilities means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C and C++ mathematics facilities out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C and C++ mathematics facilities in simple terms?

C and C++ mathematics facilities are a group of functions in the standard libraries of the C and C++ programming language. The C standard library provides the header <math.h>.

Why does C and C++ mathematics facilities matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C and C++ mathematics facilities?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C and C++ mathematics facilities.

Tags

  • C++ Standard Library
  • C standard library

Keep exploring