Simple Floating Point Equality Check in C++

TL;DR: Here you can see a handmade comparison function for floating point values.

While working on a C++ project using templates, I had to perform an equality to see if two values are identical. While the == is OK for integer values, it is not really OK for floating point values. At that point, you have several options:

  • Write inline code depending on instantiation (type conditions)
  • Write a macro (I’m not fond of macros really)
  • Write a (global) function

I’m sure there are others…


I’ve chose to write a comparison function myself, just to do a bit of exercise. The header is:

and the source:

This is not rocket science, just a simple comparison function, with a shortcut for the default tolerance. These days you can add variable arguments to C++ functions, but I’ve elected not to do that :)


A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top