jscience
  1. jscience
  2. JSCIENCE-122

FloatingPoint.hashCode() slightly broken

    Details

    • Type: Task Task
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: current
    • Fix Version/s: Version 6.0
    • Component/s: www
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      122

      Description

      The current implementation of FloatingPoint.hashCode() does not conform to the
      requirement that objects that are equal() have the same hashcode. For instance
      FloatingPoint.valueOf("0.723") has a different hashCode than
      FloatingPoint.valueOf("0.723000").

      To fix this, I suggest:

      1. change the LargeInteger.hashCode Implementation such that it returns this.mod(p)
      2. change the FloatingPoint.hashCode Implementation such that it returns
      _significand.mod(p).times(10.pow(-exp) mod p)

      where p = 1327144033. (This is just some random prime < Integer.MAX_VALUE.)

      This would be:
      LargeInteger:
      public int hashCode() {
      long code = 0;
      // 1327144033 is just an appropriately large prime;
      // 1050537101 is 263 mod 1327144033 . The result is this.mod(1327144033) .
      for (int i = _size - 1; i >= 0; i--)

      { code = (code * 1050537101 + _words[i]) % 1327144033; }

      return _isNegative ? -(int) code : (int) code;
      }

      FloatingPoint:
      public int hashCode() {
      if (isZero()) return 0;
      if (isNaN()) return 483929293; // some random number
      // This is a random prime - the same as in LargeInteger.hashCode()
      // We return _significand.mod(p).times(10.pow(-exp) mod p)
      final long p = 1327144033;
      long code = _significand.hashCode();
      int exp = _exponent;
      long mult;
      if (0 > exp)

      { mult = 398143210; // modInverse of 10 mod p exp = -exp; }

      else

      { mult = 10; }

      while (0 != exp) {
      if (1 == exp % 2)

      { code = (code * mult) % p; }

      mult = (mult * mult) % p;
      exp = exp / 2;
      }
      return (int) code;
      }

      A test is in my upcoming test-suite.

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            jscience-issues
            Reporter:
            hstoerr
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: