Version 2 of 2
Introduction
Generated Aksbel book section. · Working · Sep 28, 2026 12:12 · saved by @mujirin
Introduction
Mathematics often begins with an exact idea.
A line has a slope. A sequence has a limit. A matrix transforms a vector. A falling object follows an equation of motion. A population model predicts growth or decay. These ideas can be written with symbols, studied with logic, and sometimes solved exactly.
But many real problems do not stop at exact symbolic formulas. We often want numbers:
- How far does a projectile travel after 2.7 seconds?
- What is the root of an equation that cannot be solved neatly by algebra?
- What temperature will a body have after repeated cooling steps?
- What straight line best fits a set of measured data?
- How much error might be present in a computed result?
This book is about using C++ to answer questions like these carefully.
C++ is a programming language that can express high-level mathematical ideas while still giving the programmer detailed control over memory, data representation, and performance. That combination is one reason it has remained important in scientific and engineering software, where both clarity and speed can matter. Stroustrup describes C++ as a language designed to support both low-level systems programming and high-level abstraction, which is exactly the mixture that often appears in numerical work (Stroustrup, 2014).
The purpose of this book is not only to teach C++ syntax. It is to teach a way of thinking:
A mathematical problem becomes a computational problem only after we decide how to represent its quantities, which algorithm to use, how much error is acceptable, and how to check whether the answer is trustworthy.
That sentence contains several important ideas. We will unpack them slowly.
What mathematical computing means
Mathematical computing means using computation to explore, approximate, solve, or test mathematical problems.
Here is a simple example. Suppose we want to compute the sum
\[ 1 + 2 + 3 + \cdots + 100. \]
A mathematician may know the exact formula
\[ 1 + 2 + 3 + \cdots + n = \frac{n(n+1)}{2}. \]
So for \(n = 100\),
\[ \frac{100 \cdot 101}{2} = 5050. \]
A program could compute the same answer by adding one number at a time:
int sum = 0;
for (int k = 1; k <= 100; ++k) {
sum += k;
}
This small example already contains the heart of mathematical computing.
The formula is a mathematical statement. The loop is a computational procedure. Both can be correct, but they are not the same object. The formula describes the answer directly. The loop describes a process for obtaining the answer.
A step-by-step procedure for solving a problem is called an algorithm. In the example above, “start with zero, then add each integer from 1 to 100” is an algorithm. Algorithms are central to this book because computers do not understand mathematical intention by itself. They carry out precise instructions.
In simple problems, the algorithm may look almost obvious. In serious problems, the algorithm may determine whether the computation is fast or slow, accurate or inaccurate, reliable or misleading.
What scientific computing means
Scientific computing is the use of mathematical models, numerical algorithms, and computer programs to study scientific or engineering problems. Heath describes scientific computing as a field that combines mathematical modeling, numerical methods, and computer implementation to solve problems that often cannot be handled by exact symbolic calculation alone (Heath, 2002).
A model is a simplified mathematical description of something we want to study.
For example, suppose a cup of hot tea cools in a room. A beginner-friendly model might say:
\[ \frac{dT}{dt} = -k(T - T_{\text{room}}). \]
This equation says that the rate of change of the tea temperature \(T\) depends on the difference between the tea temperature and the room temperature. The constant \(k\) controls how quickly the tea cools.
This model is not the physical cup itself. It leaves out many details: air currents, cup shape, evaporation, measurement noise, and more. But it can still be useful. A good scientific model is not a perfect copy of reality. It is a carefully chosen simplification that helps us answer a particular question.
Scientific computing then asks:
- What variables should represent the system?
- What equations connect those variables?
- What numerical method will approximate the solution?
- How accurate is the approximation?
- How can we test the result against known facts, data, or simpler cases?
This is why scientific computing is more than programming. A program may run without crashing and still produce an answer that is scientifically wrong. In this book, we will treat correctness, accuracy, and interpretation as part of the work.
Exact mathematics and approximate computation
In school mathematics, we often write real numbers as if they can be stored exactly:
\[ \pi,\quad \sqrt{2},\quad \frac{1}{3},\quad 0.1. \]
A computer cannot store arbitrary real numbers with infinite precision. It stores numbers using a finite number of bits. Most scientific programs use floating-point arithmetic, a system for representing real-number approximations in a form similar to scientific notation. Modern floating-point arithmetic is largely shaped by the IEEE 754 standard (IEEE Computer Society, 2019), and Goldberg’s classic article explains why floating-point behavior is subtle but essential for programmers to understand (Goldberg, 1991).
For example, the decimal number \(0.1\) cannot be represented exactly in the usual binary floating-point format, just as \(1/3\) cannot be represented exactly with a finite decimal expansion:
\[ \frac{1}{3} = 0.333333\ldots \]
So a computer usually stores a nearby approximation to \(0.1\), not the exact mathematical value. This does not mean floating-point arithmetic is “bad.” It means it is finite. Once we understand that, we can use it responsibly.
A central theme of this book is:
Numerical answers should be treated as approximations unless there is a clear reason to know they are exact.
For example, if a program prints
0.30000000000000004
after adding 0.1 + 0.2, the computer is not being random or broken. It is showing a consequence of finite binary floating-point representation. Later, in Chapter 3, we will study this carefully and learn how to compare floating-point numbers using tolerances instead of expecting exact equality in every situation.
Approximation is not failure
Beginners sometimes think approximation means weakness. In scientific computing, approximation is often the main tool.
Suppose we want to compute the area under a curve:
\[ \int_0^1 e^{-x^2}\,dx. \]
There is no elementary antiderivative for \(e^{-x^2}\). That means we cannot express the integral using only the usual elementary functions in a simple exact formula. But we can approximate the area numerically by dividing the interval into small pieces and adding the areas of simple shapes.
For example, the trapezoidal rule approximates the area under a curve by using trapezoids. If the pieces are small enough and the function is well-behaved, the approximation can be very accurate.
Approximation appears in many forms:
- replacing a curve by small line segments;
- replacing a derivative by a finite difference;
- replacing a continuous process by discrete time steps;
- replacing a complicated model by a simpler one;
- replacing an exact solution by an iterative method that improves step by step.
The important question is not merely “Is this exact?” The better question is:
Is the approximation accurate enough for the purpose, and do we understand its error?
That question leads to numerical analysis, the study of algorithms for numerical problems, including their accuracy, stability, and efficiency. Higham’s work on accuracy and stability is a standard reference for understanding how numerical algorithms can succeed or fail when implemented with finite-precision arithmetic (Higham, 2002).
Error is information
In everyday speech, an error is just a mistake. In numerical computing, error has a more precise meaning. It is the difference between a computed or approximate value and a reference value, often an exact value when one is known.
If the exact answer is
\[ x = 2 \]
and our computed answer is
\[ \hat{x} = 1.99, \]
then the absolute error is
\[ |x - \hat{x}| = |2 - 1.99| = 0.01. \]
The symbol \(\hat{x}\), read “x-hat,” is often used for an approximate or computed value.
Error can come from several sources:
-
Modeling error
The mathematical model may not perfectly describe the real system. -
Measurement error
The input data may contain uncertainty. -
Truncation error
An infinite or continuous mathematical process may be cut into finitely many steps. -
Roundoff error
The computer may round numbers because it cannot store infinitely many digits. -
Programming error
The code may not implement the intended algorithm correctly.
A reliable computational scientist does not pretend these errors do not exist. Instead, they ask how large the errors might be, how they affect the conclusion, and how the computation can be checked.
Simulation: experimenting with a model
A simulation is a computational experiment performed on a model.
For example, imagine a simple population model:
\[ P_{n+1} = 1.05P_n. \]
This says that the next population value is \(1.05\) times the current value. If \(P_0 = 1000\), then
\[ P_1 = 1050, \]
\[ P_2 = 1102.5, \]
and so on.
A C++ program can repeat this update many times. That repetition is a simulation. We are not directly observing an actual population. We are exploring the consequences of a mathematical rule.
Simulation is powerful because it lets us ask “what if?” questions:
- What if the growth rate is \(5\%\)?
- What if the growth rate changes over time?
- What if the model includes limited resources?
- What if random variation is added?
But simulation also requires caution. A simulation can produce beautiful tables and plots even when its assumptions are poor. This book will repeatedly return to the difference between “the program produced numbers” and “the numbers answer the original question.”
Verification and validation
Two words are especially important in reliable computing: verification and validation.
Verification asks:
Did we solve the equations correctly?
For example, if we intended to compute
\[ f(x) = x^2 + 3x + 1, \]
but our code accidentally computes
return x * x + 3 + x + 1;
then the program does not implement the formula. Verification tries to catch such problems. It includes testing, comparing against known answers, checking special cases, and inspecting whether the algorithm is implemented correctly.
Validation asks:
Are these the right equations for the real problem?
For example, a tea-cooling model may be implemented perfectly, but if the model ignores an important heat source, it may not match measured data. That is a validation issue.
In short:
- Verification: Are we solving the model correctly?
- Validation: Is the model appropriate for the real situation?
Both matter. A verified program for an invalid model can give irrelevant results. A valid model implemented incorrectly can give wrong results. Reliable scientific computing needs both.
Why use C++ for this book?
Many languages can be used for mathematical and scientific computing. Python, Julia, MATLAB, R, Fortran, and C++ all have important roles. This book uses C++ because it teaches several valuable lessons at once.
First, C++ makes data representation visible. When you choose between int, double, std::vector<double>, or a custom matrix type, you are learning how mathematical objects are represented in memory.
Second, C++ encourages attention to performance. Scientific computations may involve millions or billions of arithmetic operations. Even when this book begins with small examples, the habits you learn will prepare you for larger simulations.
Third, C++ supports abstraction. An abstraction is a way to hide unnecessary detail while preserving the essential idea. For example, a function named dot_product can express the mathematical operation
\[ u \cdot v \]
without forcing the reader to think about every loop each time it is used. Good C++ code can be both efficient and expressive when written carefully.
Fourth, C++ is widely used in performance-sensitive software. Many numerical libraries and simulation tools use C++ directly or provide C++ interfaces. Learning C++ gives you a strong foundation for understanding how serious computational tools are built.
This does not mean C++ is always the easiest language for every task. It has details that require patience. But those details are also educational. They teach you what the computer is actually doing.
A small first example
Consider the problem of approximating the derivative of
\[ f(x) = x^2 \]
at \(x = 3\).
From calculus, the exact derivative is
\[ f'(x) = 2x, \]
so
\[ f'(3) = 6. \]
A numerical method can approximate this derivative by using a small number \(h\):
\[ f'(x) \approx \frac{f(x+h) - f(x)}{h}. \]
This is called a finite difference approximation because it replaces an infinitesimally small change from calculus with a finite, nonzero change \(h\).
For \(x = 3\) and \(h = 0.001\),
\[ \frac{(3.001)^2 - 3^2}{0.001} = \frac{9.006001 - 9}{0.001} = 6.001. \]
The approximation is close to \(6\), but not exactly \(6\). This small example shows several ideas that will return throughout the book:
- A mathematical quantity, \(f'(3)\), is being approximated.
- An algorithm, the finite difference formula, produces the approximation.
- A parameter, \(h\), affects the accuracy.
- The result has error.
- We can test the method because we know the exact answer in this case.
Good numerical learning often begins with problems where the answer is already known. That may seem unnecessary, but it is a powerful testing method. If a program fails on a problem with a known answer, we should not trust it on a problem where the answer is unknown.
What you will learn
This book begins with programming fundamentals and gradually builds toward reliable numerical simulation.
You will first learn the C++ essentials needed for mathematical work: variables, arithmetic, loops, functions, vectors, and program structure. Then you will study the numerical meaning of computer arithmetic, especially the difference between real-number mathematics and floating-point computation.
After that, the book develops key ideas from numerical analysis: error, stability, conditioning, algorithmic complexity, numerical differentiation, numerical integration, root-finding, linear algebra, least squares, interpolation, differential equations, random simulation, optimization, scientific data, and reproducibility.
The path is practical. You will see formulas, but you will also turn them into code. You will compute examples, but you will also ask whether the computed answers should be trusted.
By the end of the book, you should be able to:
- write clear C++ programs for mathematical calculations;
- represent vectors, matrices, functions, and data in code;
- understand common sources of numerical error;
- implement beginner-level numerical methods;
- test numerical code against known results;
- interpret computational results with appropriate caution;
- begin building small scientific simulations and numerical tools.
You will not learn every advanced technique in scientific computing from one beginner book. No single book can do that. But you will learn the foundation on which advanced techniques make sense.
How to think while reading
When you read this book, try to keep three questions in mind.
First:
What is the mathematical problem?
Before coding, identify the mathematical object. Is it a function, a sequence, a matrix, an equation, a dataset, or a model?
Second:
What is the computational representation?
A vector in mathematics may become std::vector<double> in C++. A real number may become double. A repeated process may become a loop. A formula may become a function.
Third:
How do I know the result is reasonable?
Check simple cases. Compare with exact answers when available. Estimate error. Print intermediate values when debugging. Use tests. Be suspicious of answers that look precise but have not been checked.
These habits are more important than memorizing syntax. Syntax tells the computer what to do. Mathematical and scientific judgment tells you whether what it did makes sense.
The central promise of the book
This book treats computation as a mathematical instrument.
Like any instrument, it must be calibrated, understood, and used with care. A calculator, a program, or a simulation can extend your ability to explore mathematics, but it does not remove the need for reasoning. In fact, serious computing requires more reasoning, not less.
You will learn to move between four forms of thought:
-
Mathematical idea
What is the concept, formula, equation, or model? -
Algorithm
What step-by-step method will produce an answer? -
C++ implementation
How is the method expressed in precise code? -
Numerical interpretation
How accurate, stable, efficient, and meaningful is the result?
That movement—from idea to algorithm to implementation to interpretation—is the core of mathematical and scientific computing in C++.
Let us begin.
References
Goldberg, David. “What Every Computer Scientist Should Know About Floating-Point Arithmetic.” ACM Computing Surveys, vol. 23, no. 1, 1991, pp. 5–48. https://doi.org/10.1145/103162.103163
Heath, Michael T. Scientific Computing: An Introductory Survey. 2nd ed., McGraw-Hill, 2002.
Higham, Nicholas J. Accuracy and Stability of Numerical Algorithms. 2nd ed., Society for Industrial and Applied Mathematics, 2002.
IEEE Computer Society. IEEE Standard for Floating-Point Arithmetic. IEEE Std 754-2019, 2019. https://doi.org/10.1109/IEEESTD.2019.8766229
Stroustrup, Bjarne. Programming: Principles and Practice Using C++. 2nd ed., Addison-Wesley, 2014.