Using logarithm in SAP ABAP

Yes, there is a log function in SAP ABAP. You can use it to implement logarithmic calculations for your requirements.

The LOG function in ABAP calculates the natural logarithm (base e) of a number. To calculate logarithms with different bases, you can use the mathematical formula: logbase(number) = ln(number) / ln(base).

Example

Here is a code snippet which demonstrates how to calculate logarithm with a custom base −

DATA: NUMBER TYPE INT,
      BASE   TYPE INT,
      RESULT TYPE FLOAT.

NUMBER = 16.
BASE = 4.
RESULT = log( NUMBER ) / log( BASE ).

WRITE: 'Number:', NUMBER,
     / 'Base:', BASE,  
     / 'Result:', RESULT.

The output of the above code is −

Number: 16
Base: 4
Result: 2.0000000000000000E+00

In this example, we calculate log4(16) which equals 2, since 42 = 16. The ABAP LOG function returns the natural logarithm, so we divide log(16) by log(4) to get the logarithm base 4.

Additional Examples

You can also calculate natural logarithm directly −

DATA: NUMBER TYPE FLOAT,
      RESULT TYPE FLOAT.

NUMBER = 2.718281828.
RESULT = log( NUMBER ).

WRITE: 'Natural log of', NUMBER, 'is', RESULT.

This will give approximately 1, since the natural logarithm of e (?2.718) equals 1.

The LOG function is useful for mathematical calculations, data analysis, and scientific computations in SAP ABAP programs.

Updated on: 2026-03-13T20:46:58+05:30

814 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements