‎2009 Jun 04 10:57 AM
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
‎2009 Jun 04 11:07 AM
hi
these digits in the expression are importing parameters or simpley digits?
‎2009 Jun 04 11:10 AM
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
‎2009 Jun 04 11:14 AM
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
‎2009 Jun 04 11:21 AM
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
‎2009 Jun 04 11:29 AM
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
‎2009 Jun 04 11:41 AM
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
‎2009 Jun 04 11:51 AM
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
‎2009 Jun 04 11:54 AM
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
‎2009 Jun 04 12:01 PM
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
‎2009 Jun 04 12:31 PM
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
‎2009 Jun 04 12:39 PM
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
‎2009 Jun 04 11:44 AM
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
‎2009 Jun 04 12:06 PM
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.
‎2009 Jun 04 12:14 PM
‎2009 Jul 12 9:18 AM
‎2009 Jul 13 2:49 PM
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.
‎2011 Jan 10 4:12 AM
Hi Santhosh,
I also got the same requirement , can you please share your solution with me.
Thanks From,
Pratap Y.