Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-18647

CASTing a float to decimal adds wrong digits

    XMLWordPrintableJSON

Details

    Description

      If I create a table with a float (32-bit) column, and cast it to the decimal type, the casting wrongly passes through the double (64-bit) type and picks up extra, wrong, digits. For example, if we have a column e of type "float", and run

      INSERT INTO tbl (p, e) VALUES (1, 5.2)

      SELECT CAST(e AS decimal) FROM tbl WHERE p=1

      The result is the "decimal" value 5.199999809265137, with all those extra wrong digits. It would have been better to get back the decimal value 5.2, with only two significant digits.

      It appears that this happens because Cassandra's implementation first converts the 32-bit float into a 64-bit double, and only then converts that - with all the silly extra digits it picked up in the first conversion - into a "decimal" value.

      Contrast this with CAST(e AS text) which works correctly - it returns the string "5.2" - only the actual digits of the 32-bit floating point value are converted to the string, without inventing additional digits in the process.

      Attachments

        Issue Links

          Activity

            People

              smiklosovic Stefan Miklosovic
              nyh Nadav Har'El
              Stefan Miklosovic
              Andres de la Peña
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m