Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Putting floats as keys of an associative container is sometimes a bad idea since the equality semantics are quite poor. But it depends on what you want to do. Bear in mind that NaN and infinities are usually not a problem. You can handle them with special comparator functions (I usually don't), and obviously the requirements of the standard are about the <strong>actual keys that will end up in the container</strong>, that you can see as a <strong>subset</strong> of the key type. A map implementation will never introduce key instances that you did not feed yourself into the map.</p> <p>I used once this predicate for a map where I could disallow two very close values:</p> <pre><code>struct FuzzyComparer { template &lt;typename T&gt; bool operator()(T x, T y) const { static const T oneMinusEps = (T)1. - 64 * std::numeric_limits&lt;T&gt;::epsilon(); return x / y &lt; oneMinusEps; } }; </code></pre> <p>This does not provide you with a good equivalence relation. This is only useful when you want to store discrete floating point values, and you are ready to tolerate some roundoff error in the computation which yields the key you want to retrieve. <strong>For the actual keys that will be inserted</strong>, it yields an equivalence relation.</p> <p>You <strong>will not</strong> be able to come up with a good equivalence relation on floating point numbers which is compatible with the arithmetic operations, ie. with makes addition and multiplication associative. </p> <p>You either have to throw away the "equivalence relation" part, which shouldn't be that a big deal in real world code (I doubt the transitivity of the eq. relation is used to an extent that would bother you in a typical map implementation) or throw away the compatibility with arithmetic operations. But what is the point of using floating point values as keys then ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload