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

Functionality of MOD operator with packed varialbles.

aaruljothi
Participant
0 Likes
2,774

Hi,

I have given a code bit below. Please explain me how day_p

is calculated.

data: DATE LIKE SCAL-DATE.

DATA: DAY_P TYPE P.

data: n type i.

n = 7.

date = sy-datum.

day_p = DATE MOD n.

write: / n , date , day_p.

Results: for

n = 7 Date: 12.09.2005 day_p: 2 how?

n = 3 Date: 12.09.2005 day_p: 1 how?

n = 5 Date: 12.09.2005 day_p: 2 how?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,400

Hi,

Before a math operation is performed with a date value, SAP converts the date to an integer. The integer value is equal to the count of days from 00.00.0000. The integer vale of 12.09.2005 is 732202. After the coversion, the math operation is performed.

732202 (12.09.2005) mod 7 = 2

This is the whole logic.

Svetlin

Hi,

I have given a code bit below. Please explain me how day_p

is calculated.

data: DATE LIKE SCAL-DATE.

DATA: DAY_P TYPE P.

data: n type i.

n = 7.

date = sy-datum.

day_p = DATE MOD n.

write: / n , date , day_p.

Results: for

n = 7 Date: 12.09.2005 day_p: 2 how?

n = 3 Date: 12.09.2005 day_p: 1 how?

n = 5 Date: 12.09.2005 day_p: 2 how?

8 REPLIES 8
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,400

MOD is for remainders.

F1 help.

<i>

DIV and MOD

The integer division operators DIV and MOD are defined as follows:

ndiv = n1 DIV n2

nmod = n1 MOD n2

so that:

n1 = ndiv * n2 + nmod

ndiv is an integer

0 <= nmod < |n2|

If n2 = 1 and n1 <> 0, a runtime error occurs.

Example

DATA: D1 TYPE I, D2 TYPE I, D3 TYPE I, D4 TYPE I,

M1 TYPE P DECIMALS 1, M2 TYPE P DECIMALS 1,

M3 TYPE P DECIMALS 1, M4 TYPE P DECIMALS 1,

PF1 TYPE F VALUE '+7.3',

PF2 TYPE F VALUE '+2.4',

NF1 TYPE F VALUE '-7.3',

NF2 TYPE F VALUE '-2.4'.

D1 = PF1 DIV PF2. M1 = PF1 MOD PF2.

D2 = NF1 DIV PF2. M2 = NF1 MOD PF2.

D3 = PF1 DIV NF2. M3 = PF1 MOD NF2.

D4 = NF1 DIV NF2. M4 = NF1 MOD NF2.

</i>

Regards,

Rich Heilman

Read only

0 Likes
1,400

Please remember to award points for helpful answers. Thanks.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,400

Can you please tell what you want to achieve with this code?

Read only

guillaume-hrc
Active Contributor
0 Likes
1,400

Hi,

Let's do some Maths !

20050912 / 7 = 284416

So, the remainder (day_p) should be 0 and not 2 !!

It tried this code :

DATA: DATE LIKE SCAL-DATE.
DATA: DAY_P(8) TYPE p.
DATA: DAY_P1(8) TYPE p.

DATA : date_bis(8) TYPE n.
data: n type i.
n = 7.

date = sy-datum.
day_p = DATE MOD n.

date_bis = sy-datum.
day_p1 = DATE_bis MOD n.

write: / n , date, day_p.
write: / n , date_bis, day_p1.

and day_p1 is giving the good result.

For what I have tested, it seems that everything behaves as if the date variable has the value 12092005 (although we know that it's 20050912 in its internal format)

Strange behaviour !

Read only

0 Likes
1,400

I was looking at this too and got the same result. I think it's worthwhile remembering that although we express dates as numbers for convenience, they're not really numbers (at least not rational numbers). You can add and subtract them, but multiplication and division make no sense. Even when adding and subtracting, you can get different answers depending on the order you perform the operations.

Rob

Read only

Former Member
0 Likes
1,401

Hi,

Before a math operation is performed with a date value, SAP converts the date to an integer. The integer value is equal to the count of days from 00.00.0000. The integer vale of 12.09.2005 is 732202. After the coversion, the math operation is performed.

732202 (12.09.2005) mod 7 = 2

This is the whole logic.

Svetlin

Read only

0 Likes
1,400

You made me think Svetlin. Thanks for that insight. Yes, SAP actually treats the date as an integer number which is equal to the number of days since the begining(2nd January 0001 = 1). I did a quick math and yes 365 days times 2005 years is 731825. Add to that the corrections and leap year additional days etc, we have 732202 for the date 12.09.2005.

Srinivas

Read only

0 Likes
1,400

Arul,

If answered, please reward and close.

Thanks,

Srinivas