Common pitfalls
This page talks about things to keep in mind when modelling in ERS, these can range from commonly known quirks in programming to things specific to ERS. When working with ERS it is important to be aware of them and ensure you don't accidentally introduce bugs into your model by doing something unexpected. Below are common pitfalls and how to prevent them.
Floating-point inaccuracies
Floating-point numbers cannot represent every real number exactly. Values are instead stored using a finite number of binary digits. A float is a 32-bit floating-point number, while a double is a 64-bit floating-point number. These types can cause issues when performing arithmetic operations.
For example, a calculation such as:
double value = 0.1 + 0.2;
may produce a value slightly different from 0.3, like 0.30000000000000004.
These small inaccuracies are inherent to floating-point arithmetic and are not specific to ERS. More information about how floating-point numbers are represented can be found in the IEEE 754 standard.
In most simulations these differences are insignificant. However, they can become relevant when:
- Values are repeatedly accumulated over many calculations.
- Exact equality comparisons are performed.
- Very large and very small values are used together.
- The simulation requires exact decimal values.
There are several approaches that can be used to reduce or avoid problems caused by floating-point inaccuracies.
ERS design note
Because of the inaccuracies of floating-point arithmetic, ERS itself uses fixed-point arithmetic Some components, such as the visualization, still use floating-point arithmetic because the inaccuracies are insignificant for computer graphics.
Use an epsilon
For general floating-point arithmetic, combine the machine epsilon to perform accurate comparisons. When comparing floating-point values, exact equality should generally be avoided when small rounding differences are acceptable.
Instead of:
if (value == expectedValue)
{
// ...
}
compare the difference against a small tolerance, commonly called an epsilon:
double epsilon = 1e-9;
if (Math.Abs(value - expectedValue) < epsilon)
{
// The values are considered equal.
}
The appropriate epsilon depends on the scale and required accuracy of the values being compared. A single fixed epsilon is therefore not suitable for every calculation.
For values with significantly different magnitudes, a relative comparison can also be used:
double epsilon = 1e-9;
double difference = Math.Abs(value - expectedValue);
double scale = Math.Max(Math.Abs(value), Math.Abs(expectedValue));
if (difference <= epsilon * Math.Max(1.0, scale))
{
// The values are considered equal.
}
Increase floating-point precision
Increasing the precision of floating-point numbers can be done by using a larger data type. For example, a double can be replaced with a decimal in C#, or a long double in C++. But increasing the precision of floating-point numbers is a trade-off, as it can increase the computational cost and memory usage of the simulation. Furthermore, any libraries you use may need to support the larger data type.
You can read more about C#'s types at the floating-point numeric types.
Fixed-point arithmetic
In ERS we primarily use fixed-point arithmetic for numerical calculations to avoid the pitfalls of floating-point arithmetic. it can be a good choice to escape the problem of floating-point arithmetic altogether.
Fixed-point arithmetic is a way of representing numbers using a fixed number of digits, with the integer and fractional parts separated by a fixed-point. For example, a 20 digit fixed-point number could have 10 digits for the integer part and 10 digits for the fractional part. This is where a value an be stored in a 1/100 scale. Like 1 is stored as 100 or 2.5 is stored as 250. and 0.5 is stored as 50. You can use a data type with a fixed number of bits to represent fixed-point numbers with enough room for the desired precision. If you know the range of values you need to represent, you can choose the number of bits and precision accordingly.