Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-252

Fraction.comparTo returns 0 for some differente fractions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.2
    • 2.0
    • None
    • None

    Description

      If two different fractions evaluate to the same double due to limited precision,
      the compareTo methode returns 0 as if they were identical.

      // value is roughly PI - 3.07e-18
      Fraction pi1 = new Fraction(1068966896, 340262731);
      
      // value is roughly PI + 1.936e-17
      Fraction pi2 = new Fraction( 411557987, 131002976);
      
      System.out.println(pi1.doubleValue() - pi2.doubleValue()); // exactly 0.0 due to limited IEEE754 precision
      System.out.println(pi1.compareTo(pi2)); // display 0 instead of a negative value
      

      Attachments

        Activity

          People

            luc Luc Maisonobe
            luc Luc Maisonobe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: