You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/floating-point-rounding-errors/how-to-1.md
+11-7Lines changed: 11 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,26 +6,30 @@ weight: 2
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Recap on Floating Point Numbers
9
+
## Review of floating point numbers
10
10
11
-
If you are unfamiliar with floating point representations, we recommend looking at this [introductory learning path](https://learn.arm.com/learning-paths/cross-platform/integer-vs-floats/introduction-integer-float-types/).
11
+
If you are unfamiliar with floating point number representation, you can review [Learn about integer and floating-point conversions](/learning-paths/cross-platform/integer-vs-floats/introduction-integer-float-types/). It covers different data types and explains data type conversions.
12
12
13
-
As a recap, floating-point numbers are a fundamental representation of real numbers in computer systems, enabling efficient storage and computation of decimal values with varying degrees of precision. In C/C++, floating point variables are created with keywords such as `float` or `double`. The IEEE 754 standard, established in 1985, is the most widely used format for floating-point arithmetic, ensuring consistency across different hardware and software implementations.
13
+
Floating-point numbers are a fundamental representation of real numbers in computer systems, enabling efficient storage and computation of decimal values with varying degrees of precision. In C/C++, floating point variables are created with keywords such as `float` or `double`. The IEEE 754 standard, established in 1985, is the most widely used format for floating-point arithmetic, ensuring consistency across different hardware and software implementations.
14
14
15
-
IEEE 754 defines two primary formats: single-precision (32-bit) and double-precision (64-bit). Each floating-point number consists of three components:
15
+
IEEE 754 defines two primary formats: single-precision (32-bit) and double-precision (64-bit).
16
+
17
+
Each floating-point number consists of three components:
16
18
-**sign bit**. (Determining positive or negative value)
17
19
-**exponent** (defining the scale or magnitude)
18
20
-**significand** (also called the mantissa, representing the significant digits of the number).
19
21
20
22
The standard uses a biased exponent to handle both large and small numbers efficiently, and it incorporates special values such as NaN (Not a Number), infinity, and subnormal numbers for robust numerical computation. A key feature of IEEE 754 is its support for rounding modes and exception handling, ensuring predictable behavior in mathematical operations. However, floating-point arithmetic is inherently imprecise due to limited precision, leading to small rounding errors.
21
23
22
-
The graphic below illustrates various forms of floating point representation supported by Arm, each with varying number of bits assigned to the exponent and matissa.
24
+
The graphic below illustrates various forms of floating point representation supported by Arm, each with varying number of bits assigned to the exponent and mantissa.
23
25
24
26

25
27
26
-
## Rounding Errors
28
+
## Rounding errors
29
+
30
+
Since computers use a finite number of bits to store a continuous range of numbers, rounding errors are introduced. The unit in last place (ULP) is the smallest difference between two consecutive floating-point numbers. It measures floating-point rounding error, which arises because not all real numbers can be exactly represented.
27
31
28
-
As mentioned above, since we are using a finite number of bits to store a continuous range of numbers, we introduce rounding error. The unit in last place (ULP) is the smallest difference between two consecutive floating-point numbers. It measures floating-point rounding error, which arises because not all real numbers can be exactly represented. When an operation is performed, the result is rounded to the nearest representable value, introducing a small error. This error, often measured in ULPs, indicates how close the computed value is to the exact result. For a simple example, if we construct a floating-point schema with 3 bits for the mantissa (precision) and an exponent in the range of -1 to 2. The possible values will look like the graph below.
32
+
When an operation is performed, the result is rounded to the nearest representable value, introducing a small error. This error, often measured in ULPs, indicates how close the computed value is to the exact result. For a simple example, if a floating-point schema with 3 bits for the mantissa (precision) and an exponent in the range of -1 to 2 is used, the possible values are represented in the graph below.
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/floating-point-rounding-errors/how-to-2.md
+25-10Lines changed: 25 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,16 @@ weight: 3
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Differences in behaviour between x86 and Arm.
9
+
## What are the differences in behavior between x86 and Arm floating point?
10
10
11
-
Architecture and standards define dealing with floating point overflows and truncations in difference ways. First, connect to a x86 and Arm-based machine. In this example I am connecting to an AWS `t2.2xlarge` and `t4g.xlarge` running Ubuntu 22.04 LTS.
11
+
Architecture and standards define floating point overflows and truncations in different ways.
12
12
13
+
You can see this by comparing an example application on an x86 and an Arm Linux system.
13
14
14
-
To demonstrate this, the C++ code snippet below casts floating point numbers to various data types. Copy and paste into a new file called `converting-float.cpp`.
15
+
You can use any Linux systems for this example. If you are using AWS, you can use EC2 instance types
16
+
`t3.micro` and `t4g.small` running Ubuntu 24.04.
17
+
18
+
To learn about floating point differences, use an editor to copy and paste the C++ code below into a new file named `converting-float.cpp`.
15
19
16
20
```cpp
17
21
#include<iostream>
@@ -57,15 +61,16 @@ int main() {
57
61
}
58
62
```
59
63
60
-
To demonstrate we will compile `converting-float.cpp` on an Arm64 and x86 machine. Install the `g++` compiler with the following command. I am using `G++` version 13.3 at the time of writing.
64
+
If you need to install the `g++` compiler, run the commands below.
61
65
62
66
```bash
63
67
sudo apt update
64
-
sudo apt install g++ gcc
68
+
sudo apt install g++ -y
65
69
```
66
70
71
+
Compile `converting-float.cpp` on an Arm and x86 machine.
67
72
68
-
Run the command below on both an Arm-based and x86-based system.
73
+
The compile command is the same on both systems.
69
74
70
75
```bash
71
76
g++ converting-float.cpp -o converting-float
@@ -75,11 +80,13 @@ For easy comparison, the image below shows the x86 output (left) and Arm output
75
80
76
81

77
82
78
-
As you can see, there are several cases where different behaviour is observed. For example when trying to convert a signed number to a unsigned number or dealing with out-of-bounds numbers.
83
+
As you can see, there are several cases where different behavior is observed. For example when trying to convert a signed number to a unsigned number or dealing with out-of-bounds numbers.
84
+
85
+
## Removing hardcoded values with macros
79
86
80
-
## Removing Hardcoded values with Macros
87
+
The above differences show that explicitly checking for specific values will lead to unportable code.
81
88
82
-
Clearly the above differences show that explictly checking for specific values will lead to unportable code. For example, consider the naively implemented function below. This checks if the value is 0, the value an x86 machine will convert a floating point number that exceeds the maximum 32-bit float value. This is different to AArch64 behaviour leading to unportable code.
89
+
For example, consider the function below. The code checks if the value is 0. The value an x86 machine will convert a floating point number that exceeds the maximum 32-bit float value. This is different from Arm behavior leading to unportable code.
This can simply be corrected by using the macro, `UINT32_MAX`.
96
-
{{% notice Note %}} To find out all the available compiler-defined macros, the `echo | <clang|gcc etc.> -dM -` will output them to the terminal{{% /notice %}}
103
+
104
+
{{% notice Note %}}
105
+
To find out all the available compiler-defined macros, you can output them using:
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/floating-point-rounding-errors/how-to-3.md
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,22 @@
1
1
---
2
-
title: Example
3
-
weight: 3
2
+
title: Error propagation
3
+
weight: 4
4
4
5
5
### FIXED, DO NOT MODIFY
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Error Propagation
9
+
## What is error propagation in x86 and Arm systems?
10
10
11
-
One cause of different outputs between x86 and Arm stems from the order of instructions and how errors are propagated. As a hypothetical example, an Arm architecture may decide to reorder the instructions that each has a different rounding error (described in the unit in last place section) so that subtle changes are observed. Alternatively, 2 functions that are mathematically equivalent will propagate errors differently on a computer.
11
+
One cause of different outputs between x86 and Arm stems from the order of instructions and how errors are propagated. As a hypothetical example, an Arm system may decide to reorder the instructions that each have a different rounding error so that subtle changes are observed.
12
12
13
-
Consider the example below. Function 1, `f1` and 2, `f2`are mathematically equivalent. Hence they should return the same value given the same input. If we input a very small number, `1e-8`, the error is different due to the loss in precision caused by different operations. Specifically, function `f2` avoids the subtraction of nearly equal number. The full reasoning is out of scope but for those interested should look into the topic of [numerical stability](https://en.wikipedia.org/wiki/Numerical_stability).
13
+
It is possible that 2 functions that are mathematically equivalent will propagate errors differently on a computer.
14
14
15
-
Copy and paste the C++ snippet below into a file named `error-propagation.cpp`.
15
+
Functions `f1` and `f2` are mathematically equivalent. You would expect them to return the same value given the same input.
16
+
17
+
If the input is a very small number, `1e-8`, the error is different due to the loss in precision caused by different operations. Specifically, `f2` avoids the subtraction of nearly equal number. For a full description look into the topic of [numerical stability](https://en.wikipedia.org/wiki/Numerical_stability).
16
18
19
+
Use an editor to copy and paste the C++ code below into a file named `error-propagation.cpp`.
17
20
18
21
```cpp
19
22
#include<stdio.h>
@@ -50,22 +53,24 @@ int main() {
50
53
}
51
54
```
52
55
53
-
Compile the source code on both x86 and Arm64 with the following command.
56
+
Compile the code on both x86 and Arm with the following command.
54
57
55
58
```bash
56
59
g++ -g error-propagation.cpp -o error-propagation
57
60
```
58
61
59
-
Running the 2 binaries shows that the second function, f2, has a small rounding error on both architectures. Additionally, there is a further rounding difference when run on x86 compared to Arm.
62
+
Running the 2 binaries shows that the second function, `f2`, has a small rounding error on both architectures. Additionally, there is a further rounding difference when run on x86 compared to Arm.
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/floating-point-rounding-errors/how-to-4.md
+19-9Lines changed: 19 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,22 @@
1
1
---
2
-
title: Minimising Variability across platforms
3
-
weight: 3
2
+
title: Minimizing variability across platforms
3
+
weight: 5
4
4
5
5
### FIXED, DO NOT MODIFY
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Minimising Variability across platforms
9
+
## How can I minimize variability across x86 and Arm?
10
10
11
-
The line `#pragma STDC FENV_ACCESS ON` is a directive that informs the compiler to enable access to the floating-point environment. This is part of the C++11 standard and is used to ensure that the program can properly handle floating-point exceptions and rounding modes enabling your program to continue running if an exception is thrown. For more information, refer to the [documentation in the C++11 standard](https://en.cppreference.com/w/cpp/numeric/fenv).
11
+
The line `#pragma STDC FENV_ACCESS ON` is a directive that informs the compiler to enable access to the floating-point environment.
12
12
13
-
In the context below, enabling floating-point environment access is crucial because the functions you are working with involve floating-point arithmetic, which can be prone to precision errors and exceptions such as overflow, underflow, division by zero, and invalid operations. However, in our example since we are hardcoding the inputs this is not strictly necessary but is included as it may be relevant for your own application.
13
+
This is part of the C++11 standard and is used to ensure that the program can properly handle floating-point exceptions and rounding modes enabling your program to continue running if an exception is thrown.
14
+
15
+
In the context below, enabling floating-point environment access is crucial because the functions you are working with involve floating-point arithmetic, which can be prone to precision errors and exceptions such as overflow, underflow, division by zero, and invalid operations. This is not necessary for this example, but is included because it may be relevant for your own application.
14
16
15
17
This directive is particularly important when performing operations that require high numerical stability and precision, such as the square root calculations in functions below. It allows the program to manage the floating-point state and handle any anomalies that might occur during these calculations, thereby improving the robustness and reliability of your numerical computations.
16
18
17
-
Save the C++ file below as`error-propagation-min.cpp`.
19
+
Use an editor to copy and paste the C++ file below into a file named`error-propagation-min.cpp`.
18
20
19
21
```cpp
20
22
#include<cstdio>
@@ -59,13 +61,17 @@ int main() {
59
61
}
60
62
```
61
63
62
-
Compile with the following command. In addition, we pass the C++ flag, `-frounding-math`. You should use use when your program dynamically changes the floating-point rounding mode or needs to run correctly under different rounding modes. In our example, it results in a predictable rounding mode on function `f1` across x86 and Arm64. For more information, please refer to the [G++ documentation](https://gcc.gnu.org/onlinedocs/gcc-13.3.0/gcc/Optimize-Options.html)
64
+
Compile on both computers, using the C++ flag, `-frounding-math`.
65
+
66
+
You should use this flat when your program dynamically changes the floating-point rounding mode or needs to run correctly under different rounding modes. In this example, it results in a predictable rounding mode on function `f1` across x86 and Arm.
Running the new binary on both platforms leads to function, `f1` having a similar value to `f2`. Further the difference is now identical across both Arm64 and x86.
72
+
Running the new binary on both systems leads to function, `f1` having a similar value to `f2`. Further the difference is now identical across both Arm64 and x86.
{{% notice Note %}} G++ provides several compiler flags to help balance accuracy and performance such as`-ffp-contract` which is useful when lossy, fused operations are used, for example, fused-multiple. As another example `-ffloat-store` which prevent floating point variables from being stored in registers which can have different levels of precision and rounding. **Please refer to your compiler documentation for more information on the available flags**{{% /notice %}}
84
+
G++ provides several compiler flags to help balance accuracy and performance such as`-ffp-contract` which is useful when lossy, fused operations are used, such as fused-multiple.
85
+
86
+
Another example is `-ffloat-store` which prevents floating point variables from being stored in registers which can have different levels of precision and rounding.
87
+
88
+
You can refer to compiler documentation for more information about the available flags.
0 commit comments