Uploaded image for project: 'Commons Numbers'
  1. Commons Numbers
  2. NUMBERS-201

FP64 equals is not consistent with hashCode

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 1.1
    • 1.2
    • field
    • None
    • Easy

    Description

      The FP64 class wraps a double so that it can be used as a field. The implementation of equals uses Precision with an allowed difference of up to 2 ULP:

      public boolean equals(Object other) {
          if (other instanceof FP64) {
              final FP64 o = (FP64) other;
              return Precision.equals(value, o.value, 1);
          }
          return false;
      }

      The hashCode is generated using Double.hashCode(value).

      The result is that two objects that are equal can generate a different hash code. This breaks the contract of Object.equals where two objects that are equal have the same hash code.

      Possible fixes:

      1. Use the equivalent of ((Double)value).equals(o.value) for a binary equality check between values that is consistent with double.
      2. Document the class as having an equals method that is not consistent with hash code. 

      I do not understand the requirement for using Precision. If a user has their own tolerance for field equivalence then this should be left to them to set the threshold. So I would recommend option 1.

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            aherbert Alex Herbert
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: