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

Bug in BigDecimal

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      Java version "1.8.0_31"
      Java (TM) SE Runtime environment (build 1.8.0_31-b13)
      Java Hotspot(TM) 64-bit server VM (build 25.31-b07, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Home Premium, Service Pack 1

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Pentium Dual-core, E5700, 3GHz, 3GB RAM, 64-bit o/s
      Running on indivual PC, as Java Application, not applet, with main() program.
      Developed and running under DOS-type command window using javac.eve and java.exe


      A DESCRIPTION OF THE PROBLEM :
      Found a problem with the BigDecimal class.

      Previously reported this bug, heard nothing from Oracle. Test program was big but have been able to reduce it down to one simple test, included here.

      ADDITIONAL REGRESSION INFORMATION:
      dont know if it ever worked; it's a new occurrence during my work.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the source code in this message and the error wil show itself.

      The varialbe x will have the value of 2*PI to 1515 decimal places (hard coded).

      When decimal point is shifted right 'enough' decimal places, results become unpredictable. Last 2 lines of program will not be the x value. In fact, the wrong value is always a multiple of 2^n of the correct value.

      When variable SCALE is set to 1000 instead of 1500, the bug does not occur. I believe the threshold is 1415 decimal places... is that a hint?

      ACTUAL -
       TWO_PI = 6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904

               places = 70
                 x = TWO_PI = 6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904
       right(places).int( ) = 62831853071795864769252867665590057683943387987502116419498891846156328

        x.movePointRight(x) = 62831853071795864769252867665590057683943387987502116419498891846156328.12572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904
           String for above = 62831853071795864769252867665590057683943387987502116419498891846156328.12572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904

      -------------------------------------------

       TWO_PI = 6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904

               places = 595
                 x = TWO_PI = 6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135964296173026564613294187689219101164463450718816256962234900568205403877042211119289245897909860763928857621951331866892256951296467573566330542403818291297133846920697220908653296426787214520498282547449174013212631176349763041841925658508183430728735785180720022661061097640933042768293903883023218866114540731519183906184372234763865223586210237096148924759925499134703771505449782455876366023898259667346724881313286172042789892790449474381404359721887405541078434352586353504769349636935338810264001136254290527121655571542685515579218347274357442936881802449906860293099170742101584559378517847084039912224258043921728068836319627259549542619921037414422699999996745956099902119463465632192637190048918910693816605285044616506689370070523862376342020006275677505773175066416762841234355338294607196506980857510937462319125727764707575187503915563715561064342453613226003855753222391818432840397876190514402130971726557731872306763655936460603904070603705937991547245198827782499443550566958263031149714484908301391901659066233723455711778150196763509274929878638510120801855403342278019697648025716723207127415320209420363885911192397893535674898896510759549453694208095069292416093368518138982586627354057978304209504324113932048116076300387022506764860071175280494992946527828398545208539845593564709563272018683443282439849172630060572365949111413499677010989177173853991381854421595018605904
       right(places).int( ) = 62831853071795864769252867665590057683943387987502116419498891846156328125724179972560696506842341359642961730265646132941876892191011644634507188162569622349005682054038770422111192892458979098607639288576219513318668922569512964675735663305424038182912971338469206972209086532964267872145204982825474491740132126311763497630418419256585081834307287357851807200226610610976409330427682939038830232188661145407315191839061843722347638652235862102370961489247599254991347037715054497824558763660238982596673467248813132861720427898927904494743814043597218874055410784343525863535047693496369353388

        x.movePointRight(x) = 125663706143591729538505735331180115367886775975004232838997783692312656251448359945121393013684682719285923460531292265883753784382023289269014376325139244698011364108077540844222385784917958197215278577152439026637337845139025929351471326610848076365825942676938413944418173065928535744290409965650948983480264252623526995260836838513170163668614574715703614400453221221952818660855365878077660464377322290814630383678123687444695277304471724204741922978495198509982694075430108995649117527320477965193346934497626265723440855797855808989487628087194437748110821568687051727070095386992738706776.20528002272508581054243311143085371031158436694548714885873763604899813720586198341484203169118757035694168079824448516087843456137672639254519099085239842074828845399999993491912199804238926931264385274380097837821387633210570089233013378740141047724752684040012551355011546350132833525682468710676589214393013961715021874924638251455529415150375007831127431122128684907226452007711506444783636865680795752381028804261943453115463744613527311872921207808141207411875983094490397655564998887101133916526062299428969816602783803318132467446911423556300393527018549859757277020241603710806684556039395296051433446414254830640418840727771822384795787071349797793021519098907388416190138584832186737036277965173254708115956608419008648227864096232152600774045013529720142350560989985893055656797090417079691187129419126544037366886564879698345260121144731898222826999354021978354347707982763708843190037211808
           String for above = 125663706143591729538505735331180115367886775975004232838997783692312656251448359945121393013684682719285923460531292265883753784382023289269014376325139244698011364108077540844222385784917958197215278577152439026637337845139025929351471326610848076365825942676938413944418173065928535744290409965650948983480264252623526995260836838513170163668614574715703614400453221221952818660855365878077660464377322290814630383678123687444695277304471724204741922978495198509982694075430108995649117527320477965193346934497626265723440855797855808989487628087194437748110821568687051727070095386992738706776.20528002272508581054243311143085371031158436694548714885873763604899813720586198341484203169118757035694168079824448516087843456137672639254519099085239842074828845399999993491912199804238926931264385274380097837821387633210570089233013378740141047724752684040012551355011546350132833525682468710676589214393013961715021874924638251455529415150375007831127431122128684907226452007711506444783636865680795752381028804261943453115463744613527311872921207808141207411875983094490397655564998887101133916526062299428969816602783803318132467446911423556300393527018549859757277020241603710806684556039395296051433446414254830640418840727771822384795787071349797793021519098907388416190138584832186737036277965173254708115956608419008648227864096232152600774045013529720142350560989985893055656797090417079691187129419126544037366886564879698345260121144731898222826999354021978354347707982763708843190037211808



      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigDecimal;
      import java.math.BigInteger;

      public class testBigDecimal
      {
          public static void main(String args[])
          {
              new testBigDecimal().test();
              }

          private void test()
          {
              BigDecimal PI;
              BigDecimal TWO_PI;

      // int SCALE = 1000; // xxxxxxx THIS WORKS
              int SCALE = 1500; // xxxxxxx THIS FAILS

              PI = new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104752162056966024058038150193511253382430035587640247496473263914199272604269922796782354781636009341721641219924586315030286182974555706749838505494588586926995690927210797509302952311877082277576");
              TWO_PI = PI.multiply( new BigDecimal("2") ) .setScale(SCALE,BigDecimal.ROUND_DOWN);

              System.out.println("\r\n TWO_PI = " + TWO_PI);

              // -------------------------------------------
              // second try -- CORRECT RESULT for movePointRight()

              BigDecimal x = TWO_PI;

              int places = 70;
              BigInteger ix = x.movePointRight(places).toBigInteger();
              System.out.println("\r\n places = " + places);
              System.out.println( " x = TWO_PI = " + x);
              System.out.println( " right(places).int( ) = " + ix);
              System.out.println("");

              BigDecimal shiftX = x.movePointRight(places);
              String sx = x.movePointRight(places).toString();
              System.out.println(" x.movePointRight(x) = " + shiftX);
              System.out.println(" String for above = " + sx);


              // -------------------------------------------
              // second try -- INCORRECT RESULT for movePointRight()

              System.out.println("\r\n-------------------------------------------");
              System.out.println("\r\n TWO_PI = " + TWO_PI);

              x = TWO_PI;

              places = 595;
              ix = x.movePointRight(places).toBigInteger();
              System.out.println("\r\n places = " + places);
              System.out.println( " x = TWO_PI = " + x);
              System.out.println( " right(places).int( ) = " + ix);
              System.out.println("");

              // wrong:
              shiftX = x.movePointRight(places);
              sx = x.movePointRight(places).toString();
              System.out.println(" x.movePointRight(x) = " + shiftX);
              System.out.println(" String for above = " + sx);
          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I wrote my own version of BigDecimal (not as efficient), using BigInteger and a scale, and the problem disappeared.

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: