Skip to Content
Technical Articles
Author's profile photo Horst Keller

ABAP News for Release 7.51 – Floating Points in Open SQL and ABAP CDS

All database platforms support binary floating point numbers. The corresponding built-in data of the ABAP Dictionary is FLTP. The problem is that the handling of binary floating point numbers is platform specific. Operations that are carried out on the database with binary floating point numbers can yield different results. In former times this mainly affected the aggregate function AVG of Open SQL. Nowadays, in the times of “code push down”, there are more situations, where FLTP numbers are not merely stored in database fields but are operands or results of operations, as e.g. SQL expressions or SQL functions in Open SQL and ABAP CDS. For all such operations involving binary Floating point numbers you must take the platform dependency into account.

There’s one exception. The CAST operator of the Open SQL and ABAP CDS CAST expression should have a platform  independent behavior. Therefore, it is not allowed to cast from FLTP to any other numeric type because there would be platform dependent results for types where you might not expect it.

On the other hand, there are enough use cases where you want to convert FLTP to other numeric types on the database. Therefore, with release 7.51, ABAP offers the following special conversion options for  binary floating point numbers:

 

  • An new addition AS dtype to aggregate function AVG allows you to define the result type of this function in Open SQL and in ABAP CDS. The result type can be DEC, CURR, QUAN with given length or decimals. You can also specify FLTP explicitly. And the addition also allows you to calculate averages of arguments with data type INT8, which is not possible in the basic form.
  • An new built-in function FLTP_TO_DEC allows you to convert an argument of type FLTP to DEC, CURR, QUAN with given length or decimals. This function is available in ABAP CDS but not in Open SQL yet.

The result of both new features can be platform dependent. The function FLTP_TO_DEC of ABAP CDS can be especially used to convert numeric literals with a decimal point to DEC. Unfortunately, ABAP CDS treats such a literal as type FLTP otherwise.

Open SQL Example

SELECT FROM sbook
       FIELDS fldate,
              AVG( luggweight ) AS av_luggweight_fltp,
              AVG( luggweight AS DEC( 8,4 ) ) AS av_luggweight_dec
       WHERE carrid = 'LH' AND
             connid = '0400'
       GROUP BY fldate
       INTO TABLE @DATA(result).

cl_demo_output=>display( result ).

The example demonstrates the aggregate function AVG without and with an explicit type specification. The output is something like:

ABAP CDS Example

@AbapCatalog.sqlViewName: 'KELLERHDEMO'
define view Kellerh_Demo
  as select from
    demo_expressions
    {
      1.234                                  as literal,
      fltp_to_dec( 1.234 as abap.dec(10,3) ) as dec_literal,
      fltp_to_dec( fltp1 as abap.dec(10,3) ) as dec1_10_3,
      fltp_to_dec( fltp2 as abap.dec(10,3) ) as dec2_10_3
    }
    where
      id = 'X'

The result can be something like:

Note that decimals are cut but not rounded here.

And what about DECFLOATS?

This blog is about binary floating point numbers and their platform dependency. Now you might ask where are the decimal floating point numbers that were introduced into the ABAP language as built-in data types decfloat16 and decfloat34 with release 7.02? Well, although they follow a standard (IEEE-754-2008) and overcome many of the difficulties of the binary floating point numbers, they are not natively supported by all platforms, neither for application server nor for database. And that’s why the ABAP Dictionary cannot offer real built-in DECFLOAT types. The existing types DF16_DEC, DF34_DEC, DF16_RAW and DF34_RAW are mainly for storing and reading decimal floating point numbers. Limited operations are possible with DF16_DEC and DF34_DEC in Open SQL but none in ABAP CDS (besides simple reads, of course). The hope that decimal floating point numbers sooner ar later will replace the binary floating  point numbers has not come true yet. And therefore we continue to live with workarounds.

For more information, see:

 

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Shakul Jugran
      Shakul Jugran

      Thanks for the information Horst. The bit about decfloat types was especially very helpful.

      Regards,
      Shakul.

      Author's profile photo Yuki Yu
      Yuki Yu

      Hi,

      Thanks for the information, but I have a problem with the function.

      I copy the code above, then it shows there are "TypeSyntax Errors" with the first "as" and "dec",  if I try to change the parameters, It shows the function is unknown.

      should I add annotations or if there are some other prepare and configuration that I haven't do?

      Thanks for your attention.

      Best Regards.

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      You need AS ABAP 7.51, ABAP Kernel 7.49 at least, and the database version that is prescribed for that ABAP release.

      Author's profile photo Former Member
      Former Member

      Thanks Horst for valuable info above.

      The DF16_DEC and DF34_DEC ,getting shrotdump while inserting the values with +ve Exponennt though they are valid values within allowed range. However it works fine for -ve Exponents.

      Please provide inputs any.

      Category ABAP Programming Error
      Runtime Errors DBIF_RSQL_INVALID_RSQL

      Error analysis
      An exception has occurred which is explained in more detail below. The
      exception, which is assigned to class 'CX_SY_OPEN_SQL_DB' was not caught and
      therefore caused a runtime error. The reason for the exception is:
      During a SELECT access, the read data could not be placed in the target
      field.
      Either the conversion is not supported for the target field type, the
      target field is too small to take the value, or the data does not have
      the required format for the target field.