Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-45786

Inaccurate Decimal multiplication and division results

    XMLWordPrintableJSON

Details

    Description

      Decimal multiplication and division results may be inaccurate due to rounding issues.

      Multiplication:

      scala> sql("select  -14120025096157587712113961295153.858047 * -0.4652").show(truncate=false)
      +----------------------------------------------------+                          
      |(-14120025096157587712113961295153.858047 * -0.4652)|
      +----------------------------------------------------+
      |6568635674732509803675414794505.574764              |
      +----------------------------------------------------+
      

      The correct answer is

      6568635674732509803675414794505.574763

      Please note that the last digit is 3 instead of 4 as

       

      scala> java.math.BigDecimal("-14120025096157587712113961295153.858047").multiply(java.math.BigDecimal("-0.4652"))
      val res21: java.math.BigDecimal = 6568635674732509803675414794505.5747634644
      

      Since the factional part .574763 is followed by 4644, it should not be rounded up.

      Division:

      scala> sql("select -0.172787979 / 533704665545018957788294905796.5").show(truncate=false)
      +-------------------------------------------------+
      |(-0.172787979 / 533704665545018957788294905796.5)|
      +-------------------------------------------------+
      |-3.237521E-31                                    |
      +-------------------------------------------------+
      

      The correct answer is

      -3.237520E-31

      Please note that the last digit is 0 instead of 1 as

       

      scala> java.math.BigDecimal("-0.172787979").divide(java.math.BigDecimal("533704665545018957788294905796.5"), 100, java.math.RoundingMode.DOWN)
      val res22: java.math.BigDecimal = -3.237520489418037889998826491401059986665344697406144511563561222578738E-31
      

      Since the factional part .237520 is followed by 4894..., it should not be rounded up.

      Attachments

        Activity

          People

            kazuyukitanimura Kazuyuki Tanimura
            kazuyukitanimura Kazuyuki Tanimura
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: