# C++ Floating-Point Number Comparison

## Introduction

Floating-point number comparison has always been a problem in computer applications. Because a real number cannot often be precisely represented by floating-point numbers and floating-pointer computations have rounding errors, the order of floating point calculation matters for bitwise equality. For example, a + b + c might not be bitwise equal to c + b + a. Because the floating-point errors could accumulate and propagate, solving a problem using different but correct mathematical recipes might result in different outcomes. Therefore, we would often need to compare floating-point numbers to determine if our certain algorithm implementation produces correct results.

In this blog post, I would like to discuss how to compare floating-point numbers in C++ empirically.

## C++ Floating-Point Number Comparison

It is common to see that a absolute tolerance value and a relative tolerance value were used to compare floating-point numbers, such as the numpy.allclose function. However, the problem is that the relative error between two small close floating-point numbers might be larger than the relative error between two large close floating-point numbers. Similarly, the absolute error between two small close floating-point numbers might be smaller than the absolute error between two large close floating-point numbers.

Therefore, we could use different absolute tolerance value and relative tolerance value for floating numbers in different ranges.

Lei Mao

01-08-2024

01-08-2024