Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8334483

MutableBigInteger's methods compare and compareShifted are broken

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      JDK 22.0.1

      A DESCRIPTION OF THE PROBLEM :
      The methods compare and compareShifted implicitly assume that the operands have no leading zeros, but the the method compare is invoked by subtract, in which there isn't this assuption. Indeed, this leaded to several failures in an algorithm I was trying to implement for square root of MutableBigIntegers.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      MutableBigInteger a = new MutableBigInteger(new int[] { 0, 0 });
      MutableBigInteger b = new MutableBigInteger(new int[] { 1 });
      int comp = a.compare(b);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      comp == -1
      ACTUAL -
      comp == 1

      ---------- BEGIN SOURCE ----------
      To execute this code, you must place this method in package java.math:

      public static void main(String[] args) {
              MutableBigInteger a = new MutableBigInteger(new int[] { 0, 0 });
              MutableBigInteger b = new MutableBigInteger(new int[] { 1 });
              
              if (a.compare(b) != a.toBigInteger().compareTo(b.toBigInteger()))
                  System.out.println("comparison failure");
      }
      ---------- END SOURCE ----------

      FREQUENCY : occasionally


            asajeev Anjana Sajeev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: