Floating point in both
Fortran and C++ implement the same IEEE 754 arithmetic, so a fair comparison of the two languages on floating point should produce the same digits. It does. What differs is what the compilers tell you about it, and what a single optimisation flag is allowed to do to the answers, and both of those are worth measuring rather than assuming.
Eleven lines
One line per question: single and double precision for the literal 0.1, the product 0.1 * 3 against 0.3, ten tenths added in single precision, a value that is added to 1e20 and subtracted again, the two epsilons, and the comparisons of a NaN and an infinity with zero. Both programs print each answer with seventeen significant digits, so a difference in the last bit is visible.
The Fortran program
The file fp.f90:
program fp_f
use, intrinsic :: iso_fortran_env, only: sp => real32, dp => real64
implicit none
real(sp) :: a, b, s
real(dp) :: x, y, zero
integer :: i
zero = 0.0_dp
a = 0.1_sp
b = 0.3_sp
x = 0.1_dp
y = 0.3_dp
print '(a18,es25.16e2)', 'single 0.1', a
print '(a18,es25.16e2)', 'single 0.1*3', a * 3.0_sp
print '(a18,es25.16e2)', 'double 0.1', x
print '(a18,es25.16e2)', 'double 0.1*3', x * 3.0_dp
print '(a18,l1)', '0.1*3 == 0.3', x * 3.0_dp == y
s = 0.0_sp
do i = 1, 10
s = s + a
end do
print '(a18,es25.16e2)', 'single sum of 10', s
x = 1.0e20_dp
print '(a18,es25.16e2)', '(1e20+1)-1e20', (x + 1.0_dp) - x
print '(a18,es25.16e2)', 'epsilon single', epsilon(1.0_sp)
print '(a18,es25.16e2)', 'epsilon double', epsilon(1.0_dp)
print '(a18,l1)', 'nan > 0', (zero / zero) > 0.0_dp
print '(a18,l1)', 'inf > 0', (1.0_dp / zero) > 0.0_dp
end program fp_f
gfortran -Wall -Wextra -std=f2018 fp.f90 -o fp_f
./fp_f
The C++ program
The file fp.cpp, with the same eleven statements and a format chosen to match Fortran's A18 and ES25.16E2 descriptors:
#include <cstdio>
int main() {
const float a = 0.1f;
const double x = 0.1;
const double y = 0.3;
const double zero = 0.0;
std::printf("%18s%25.16E\n", "single 0.1", static_cast<double>(a));
std::printf("%18s%25.16E\n", "single 0.1*3", static_cast<double>(a * 3.0f));
std::printf("%18s%25.16E\n", "double 0.1", x);
std::printf("%18s%25.16E\n", "double 0.1*3", x * 3.0);
std::printf("%18s%s\n", "0.1*3 == 0.3", (x * 3.0 == y) ? "T" : "F");
float s = 0.0f;
for (int i = 0; i < 10; ++i) {
s += a;
}
std::printf("%18s%25.16E\n", "single sum of 10", static_cast<double>(s));
std::printf("%18s%25.16E\n", "(1e20+1)-1e20", (1.0e20 + 1.0) - 1.0e20);
std::printf("%18s%25.16E\n", "epsilon single", static_cast<double>(1.1920928955078125e-07f));
std::printf("%18s%25.16E\n", "epsilon double", 2.2204460492503131e-16);
std::printf("%18s%s\n", "nan > 0", ((zero / zero) > 0.0) ? "T" : "F");
std::printf("%18s%s\n", "inf > 0", ((1.0 / zero) > 0.0) ? "T" : "F");
return 0;
}
g++ -std=c++23 -Wall -Wextra fp.cpp -o fp_cpp
./fp_cpp
The output
./fp_f > f.txt
./fp_cpp > c.txt
diff f.txt c.txt
(no output: the two files are identical)
single 0.1 1.0000000149011612E-01
single 0.1*3 3.0000001192092896E-01
double 0.1 1.0000000000000001E-01
double 0.1*3 3.0000000000000004E-01
0.1*3 == 0.3F
single sum of 10 1.0000001192092896E+00
(1e20+1)-1e20 0.0000000000000000E+00
epsilon single 1.1920928955078125E-07
epsilon double 2.2204460492503131E-16
nan > 0F
inf > 0T
Every line is the same in both languages, down to the last digit: 0.1*3 is not 0.3 in either, ten tenths in single precision come to 1.0000001192092896, adding one to 1e20 and subtracting it back gives zero, a NaN compares false with zero and an infinity true. Fortran's real32 and C++'s float are the same type, and so are real64 and double.
A warning only one compiler gives
The line that compares 0.1*3 with 0.3 earns a warning from gfortran and nothing at all from g++:
fp.f90:17:35:
17 | print '(a18,l1)', '0.1*3 == 0.3', x * 3.0_dp == y
| 1
Warning: Equality comparison for REAL(8) at (1) [-Wcompare-reals]
-Wcompare-reals is part of -Wall, and it fires on every == and /= between reals. g++ has the same warning as -Wfloat-equal, which is not in -Wall or -Wextra, so a C++ project that has not added it by hand compiles the same mistake in silence. The Fortran compiler warning about the language's own most common numerical mistake, without being asked, is a real advantage of the Fortran toolchain and a one-line change in the C++ one.
The flag that changes the answers
Both compilers accept -Ofast, which is -O3 plus -ffast-math: reassociate, assume no NaN, assume no signed zero, ignore the IEEE rules. The same two sources, rebuilt with that one flag, do not behave the same way:
gfortran -Ofast -std=f2018 fp.f90 -o fp_fast
g++ -std=c++23 -Ofast fp.cpp -o fp_cfast
./fp_fast > ff.txt
./fp_cfast > cf.txt
diff f.txt ff.txt
diff c.txt cf.txt
6c6
< single sum of 10 1.0000001192092896E+00
---
> single sum of 10 1.0000000000000000E+00
7c7
< (1e20+1)-1e20 0.0000000000000000E+00
---
> (1e20+1)-1e20 1.0000000000000000E+00
10,11c10,11
< nan > 0F
< inf > 0T
---
> nan > 0T
> inf > 0F
Three answers changed in the Fortran build. The ten tenths add up to exactly one because the compiler reassociated the additions into a multiplication, the 1e20 line returns one because the two operations were cancelled algebraically, and the NaN now compares true with zero while the infinity compares false, because the flag lets the compiler assume that neither value exists. The second diff printed nothing: the C++ build produced exactly the same answer as before.
That is the finding worth keeping. -ffast-math is not a language rule in either language; it is a permission, and each front end uses a different part of it. A program whose results depend on it is a program that cannot be moved to another compiler without re-measuring, which is exactly the argument for the test build of the testing group: run it without the flag, and let the release build use whatever is fast.
What to do about it
For Fortran: use real64 for anything that is compared, add -Wcompare-reals to the build (it is in -Wall), and never put -Ofast on the build that produces the numbers a test checks. For C++: add -Wfloat-equal by hand, because -Wall does not, and treat -Ofast the same way. For both: compare with the combined tolerance of the tolerance page, and print with enough digits that a difference in the last bit is visible, which is how this page found its three changed lines.
Exercises
- Add -Wfloat-equal to the g++ command and rebuild. What does the C++ compiler say about the comparison line?
- Change the single-precision sum of ten tenths to a thousand tenths. Which language gives which answer, and how far from 100 is it?
- Build both with -O3 without -ffast-math and run the diff again. Does either compiler change an answer?
- Add a line that prints -0.0 and a line that compares it with zero. What do the two programs print, and what does -Ofast do to it?
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and G++ (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev