Comparison Algorithms

Comparing Floating Point Numbers

How do I compare ranges that contain floating-point numbers?

Abstract art representing computer programming

Comparing ranges that contain floating-point numbers requires careful handling due to potential precision issues. Floating-point arithmetic can introduce small errors, making direct comparisons unreliable. Instead, it's best to use a tolerance-based comparison.

Using a Custom Comparison Function

You can create a custom comparison function that checks if the absolute difference between two floating-point numbers is within a specified tolerance.

Here's an example:

#include <algorithm>
#include <cmath>  // Needed for std::abs
#include <iostream>
#include <vector>

bool almostEqual(double a, double b) {
  return std::abs(a - b) < 0.0001;
}

int main() {
  std::vector<double> A{1.0, 2.000001, 3.0};
  std::vector<double> B{1.0, 2.0, 3.0};

  if (std::ranges::equal(A, B, almostEqual)) {
    std::cout << "Ranges are equal";
  } else {
    std::cout << "Ranges are not equal";
  }
}
Ranges are equal

In this example, almostEqual() compares the floating-point numbers within a tolerance of 0.0001, ensuring that small differences due to precision issues are ignored.

Comparing with std::ranges::mismatch()

You can also use std::ranges::mismatch() to find where two ranges of floating-point numbers differ:

#include <algorithm>
#include <cmath>  // Needed for std::abs
#include <iostream>
#include <vector>

bool almostEqual(double a, double b) {
  return std::abs(a - b) < 0.0001;
}

int main() {
  std::vector<double> A{1.0, 2.00001, 3.0};
  std::vector<double> B{1.0, 2.0, 3.1};

  auto [itA, itB] = std::ranges::mismatch(
    A, B, almostEqual);

  if (itA != A.end() && itB != B.end()) {
    std::cout << "First mismatch - A: " << *itA
      << ", B: " << *itB;
  } else {
    std::cout
      << "Ranges are equal within tolerance";
  }
}
First mismatch - A: 3, B: 3.1

Practical Considerations

  • Tolerance Level: Choose an appropriate tolerance level for your application. The value may need adjustment based on the precision of your data.
  • Performance: Custom comparison functions may introduce a small performance overhead, but they are essential for accurate floating-point comparisons.
  • Consistency: Ensure consistent use of the tolerance level across all comparisons to avoid discrepancies.

Comparing floating-point numbers accurately requires accounting for precision issues, and using a custom comparison function with a defined tolerance is the most reliable method.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved