Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Calculate mathamatical expression

Former Member
0 Likes
1,591

Hi All,

I am creating a FM in which i need to incorporate the below logic:

Will be getting the data in the format 3 + (1 + MAX( 5, (5-4))). i need to calculate it as 3 + (1 + MAX (5, 1 )) = 3 + 1 + 5 = 8.

MAX is get biger number comparing two. We'll have MAX, MIN, RUP (round up), and RDN (round down) 4 types of Function in formula.

Please let me know the logic how to acheive this.

Regards,

Santosh

17 REPLIES 17
Read only

Former Member
0 Likes
1,539

hi

these digits in the expression are importing parameters or simpley digits?

Read only

0 Likes
1,539

It is a importing parameter value.

Local Interface:

*" IMPORTING

*" REFERENCE(I_FORMULA) TYPE ZPS_FORMULA_TT

*" EXPORTING

*" REFERENCE(O_RESULT) TYPE P

*" EXCEPTIONS

*" WRONG_EXPRESSION

Read only

Former Member
0 Likes
1,539

Hi,

Could you tell us the purpose of the FM, where it is used.

And in my opinion, evaluating/computing arithmetic expression is a cumbersome process which requires deep knowledge in Regular Expressions.

If all your expression come as the one you mentioned just pass the values( 3, 5, 4 etc) and operaion ( MAX MIN etc) so that you can easily calculate the output.

Regards

Karthik D

Read only

0 Likes
1,539

Hi Karthik,

The requirement is like that. We need to calculate the values by that form of values only. Is there any other solution where we can calculate that??

So i didnt get your second option what you are saying. Can you please give some logic for that method.

"If all your expression come as the one you mentioned just pass the values( 3, 5, 4 etc) and operaion ( MAX MIN etc) so that you can easily calculate the output."

Regards,

Santosh

Read only

0 Likes
1,539

Hi,

Assume you are always going to have the same expression 3 + (1 + MAX( 5, (5-4))) but different numbers and operations.

You have to just pass 5 numbers and 1 mode to get it done.

Say if you give input parameters as n1...n5 type i and mode type char03.

You have to pass like;

n1 = 4. n2 = 5. n3 = 2. n4 = 9. n5 = 3. mode = 'MIN'.

according to mode passed perform the operation and pass the value back.

Check whether this satisfies your requirement.

Regards

Karthik D

Read only

0 Likes
1,539

Karthik,

Thnks for your reply.

I am not getting your solution. If you can provide me some sample code on this it will be of great help.

Regards,

Santosh

Read only

0 Likes
1,539

Hi,

Just write a FM to get 5 numbers and one mode. Then write code inside it to calculate;

N1 + (N2 + MODE(N3, (N4-N5)))

For Roundup and Rounddown the Fifth number N5 may not be necessary so you can declare that as optional.

Got it?

Regards

Karthik D

Read only

0 Likes
1,539

Hi Karthik,

Everytime i will be not getting the same format value. Sometime will be getting as 3+3 also.

So at that time i think your logic will not work.

Plz clarify...

Regards,

Santosh

Read only

0 Likes
1,539

In that case i fear it cannot be done.

I think it can be possible by using regular expressions but as i said its a very difficult for you to validate the expression, breakup into operands and operators and doing the calculation.

Where are you going to implement this FM, and which program is going to supply values for this?

Regards

Karthik D

Read only

0 Likes
1,539

Still it is not confirmed that where i will be using this FM, and which program will be supplying the values for this.

Regards,

Santosh

Read only

0 Likes
1,539

Hi,

Even In the case you write code to split numbers, operators and functions using FIND and REGEX, its not that much easy as your user can pass value of any number of digits. So i feel like we cannot easily get a possible solution for your requirement.

Regards

Karthik D

Read only

Former Member
0 Likes
1,539

hi

Karthik mean if you have only one expression to calculate

so you dont need to pass that expression

just pass the values in the importing parameters and the method(min , max)

and then calculate the value for them

Read only

Former Member
0 Likes
1,539

ok Santosh ,

you are saying that every time your expression will change

for that you need to use offset

declear some variable in function module

than check the expression char. by char.

and store the values of digits, expression(+,-,*,/) and function(min, max) in variables

and than you can perform operation on that variables

like - for 3+3

first you have to store 3 in v1 than '+' in ex1 and than 3 in v2

than based on ex1 you can perform your operation on v1 and v2

if ex1 eq '+'.

v3 = v1 + v2.

Read only

0 Likes
1,539

Just to add: 3 + 1 + 5 = 9 and not 8...

Read only

Former Member
0 Likes
1,539

i got the solution

Read only

0 Likes
1,539

Which is what?

Note that you may use formulas with EVAL_FORMULA function module:


DATA d(255) TYPE c.
DATA l_prog TYPE syrepid.
DATA l_sr TYPE syrepid.
DATA a TYPE f.
DATA b TYPE I.
d = `2.5 * ( PIX + abs( -1 ) )`.
l_prog = sy-repid.
l_sr = 'MYROUTINE'.
CALL FUNCTION 'EVAL_FORMULA'
  EXPORTING
    formula                 = d
    program                 = l_prog
    routine                 = l_sr
  IMPORTING
    value                   = a
  EXCEPTIONS
    division_by_zero        = 1
    exp_error               = 2
    formula_table_not_valid = 3
    invalid_expression      = 4
    invalid_value           = 5
    log_error               = 6
    parameter_error         = 7
    sqrt_error              = 8
    units_not_valid         = 9
    missing_parameter       = 10
    OTHERS                  = 11.
b = a.
ASSERT B = 10.

FORM myroutine
    USING
        i_var     TYPE c
    CHANGING
        e_value   TYPE f
        e_subrc   TYPE p.
  CASE i_var.
    WHEN `PIX`. e_value = 3.
    WHEN OTHERS. e_subrc = 1.
  ENDCASE.
ENDFORM.

Read only

0 Likes
1,539

Hi Santhosh,

I also got the same requirement , can you please share your solution with me.

Thanks From,

Pratap Y.