Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-9642

"BigDecimal(double)" should not be used

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.6.0
    • Component/s: KafkaConnect
    • Labels:
      None

      Description

      I recommend not to use the BigDecimal(double) constructor. Because of floating point imprecision, we're unlikely to get the value we expect from that constructor.

      Instead, we should use BigDecimal.valueOf, which uses a string under the covers to eliminate floating-point rounding errors, or the constructor that takes a String argument.

       

      From JavaDocs

      The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                soontaek.lim Soontaek Lim
                Reporter:
                soontaek.lim Soontaek Lim
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: