VOOZH about

URL: https://www.geeksforgeeks.org/cpp/floating-point-values-as-keys-in-std-map/

⇱ Floating Point Values as Keys in std:map - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Floating Point Values as Keys in std:map

Last Updated : 23 Jul, 2025

The std::map in C++ is a sorted associative container that stores key-value pairs, where each key is unique. When working with std::map, keys are typically of a type that supports ordering such as the integers or strings. However, using floating point numbers as keys introduces some considerations and challenges due to the precision issues.

In this article, we will explore the considerations, challenges, and best practices of using floating-point keys in std::map.

Challenges with Floating Point Keys in std::map

The floating-point numbers in computers are represented with the limited precision due to their binary nature. This can lead to issues when using them as keys in the std::map. The two major issues that may occur while using them are:

  • Precision Errors: The small differences in floating point numbers that should be equal might not compare equal due to the precision issues.
  • Comparison Issues: Directly comparing floating point numbers using the operators like < or > can lead to the unexpected results due to rounding errors.

Due to the above issues, it is recommended to avoid the use of floating-point keys in std::map. In the case where you have to absolutely use the floating point as keys, you can use the following methods:

How to Properly use Floating Point as Keys?

There are two major ways using which we can use the floating point as keys somewhat safely:

  1. Floating Point as Keys Using Custom Comparison Function
  2. Floating Point as Keys Using Rounding and Normalization

1. Floating Point as Keys Using Custom Comparison Function

To handle precision issues, we can define a custom comparison function that considers a tolerance level for the floating-point keys. This ensures that keys are considered equal if they are within the small tolerance of each other.

Example


Output
Value for key 1.0: 30
Value for key 1.000000001: 30

As you may already deduced, the disadvantage of this method is that if the two floating point keys difference is within the given tolerance limit, then it will be considered as equal.

2. Floating Point as Keys Using Rounding and Normalization

If you must use floating-point keys, ensure that the values are rounded or normalized to a fixed precision before insertion.

Example


Output
0.1: A
0.2: B

Conclusion

Using floating point keys in std::map requires careful consideration of the precision issues. Implementing a custom comparison function with the tolerance level can help mitigate these issues ensuring the correct behaviour when storing and accessing the elements with the floating point keys in C++. Although, it is still recommended to use different type as keys due to the limitations of the above methods.

Comment
Article Tags:
Article Tags: