# Divide and Remainder

**URL:** <https://techcommunity.softwareag.com/t/divide-and-remainder/60528>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [January 31, 2006, 12:30pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528 "2006-01-31T12:30:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Venkatesan\_Devaraj1](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@Venkatesan\_Devaraj1](https://techcommunity.softwareag.com/u/Venkatesan_Devaraj1)\
**Post date:** [January 31, 2006, 12:30pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/1 "2006-01-31T12:30:39Z")

</div>

Hi all,

Initially we coded with bleow syntax where divide statement used to return the remainder in #WS-TEMP-REM. However we got always 0 in Remainder field and it looks like #WS-TEMP-INT defined as N4.2 causing problem.

1 #WS-TEMP-CCYY (N4)  
1 #WS-TEMP-INT (N4.2)  
1 #WS-TEMP-REM (N4.2)

DIVIDE 4 INTO #WS-TEMP-CCYY GIVING #WS-TEMP-INT REMAINDER #WS-TEMP-REM

When we corrected Local variable definition as shown below then it worked as expected.  
1 #WS-TEMP-CCYY (N4)  
1 #WS-TEMP-INT (N4)  
1 #WS-TEMP-REM (N4)

DIVIDE 4 INTO #WS-TEMP-CCYY GIVING #WS-TEMP-INT REMAINDER #WS-TEMP-REM

Clarification:  
As such Natural does not give any error on both the syntax but it behaved differently when job run .Does this mean that giving statement operand must be Numeric field with out decimal ?

---

<div class="post-metadata">

**Author:** ![Peter\_Pag](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Pag](https://techcommunity.softwareag.com/u/Peter_Pag)\
**Post date:** [January 31, 2006, 1:29pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/2 "2006-01-31T13:29:04Z")

</div>

What remainder do you expect if you divide 4 into a number when the result contains 2 decimals? All results will have a precision of 2 decimals in this case. So the remainder is always 0. That’s not a problem with Natural but a problem of mathematics.

If you divide 3 into 1 you will always have a remainder (with any number of possible decimals). That’s also not a speciality of Natural but mathematics.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [January 31, 2006, 7:44pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/3 "2006-01-31T19:44:05Z")

</div>

The Natural Documentation says:

> [@](#):
>
> Internally, the remainder is computed as follows:
> 
> 1. The quotient of the division of operand1 into operand2 is computed.
> 2. The quotient is multiplied by operand1.
> 3. The product of this multiplication is subtracted from operand2.
> 4. The result of this subtraction is assigned to operand4.

Let’s say, that #WS-TEMP-CCYY has a value of9.0. Natural would do the following:

1. 9 / 4 = 2.25
2. 2.25 \* 4 = 9
3. 9 - 9 = 0
4. #WS-TEMP-REM is set to 0

So only the field #WS-TEMP-INT has to be N4 in your case.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [January 31, 2006, 8:24pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/4 "2006-01-31T20:24:43Z")

</div>

Btw, if possible (and there probably aren’t many reasons why it shouldn’t be) avoid using N-fields in computations, NATURAL will have to convert it back and forth as it isn’t possible to do any arithmetic operations on N (display format) fields.

---

<div class="post-metadata">

**Author:** ![Peter\_Pag](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Pag](https://techcommunity.softwareag.com/u/Peter_Pag)\
**Post date:** [January 31, 2006, 10:34pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/5 "2006-01-31T22:34:55Z")

</div>

> [@Matthias](#):
>
> 1. 9 / 4 = 2.25
> 2. 2.25 \* 4 = 9
> 3. 9 - 9 = 0

That’s exactly that what I have learned about dividing with remainder in primary shool! But it is much more sophisticated doing it with decimals! 😉

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 1, 2006, 2:19pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/6 "2006-02-01T14:19:06Z")

</div>

> [@wilfried123](#):
>
> That’s exactly that what I have learned about dividing with remainder in primary shool!

I guess it’s not that easy. Please see [Remainder - Wikipedia](http://en.wikipedia.org/wiki/Remainder#The_remainder_for_real_numbers)  
Maybe Venkatesan has to define his own definition of a remainder. May that way is suitable:

```auto
define data local
01 #operand1 (N10.2) init <9> /* input
01 #operand2 (N10.2) init <4.2> /* input
01 #operand3 (N10.2) /* output
end-define
*
#operand3 := #operand1
repeat while #operand3 > #operand2
  #operand3 := #operand3 - #operand2
end-repeat
display #operand3 
end
```

But beware of negative values in #operand2!!

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 3, 2006, 1:28pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/7 "2006-02-03T13:28:03Z")

</div>

Now I know: The result depends on the format of #operand3. Try this:

```auto
define data local
1 #operand3 (N1.0) /* and try (N1.1) and (N1.2) here
1 #operand4 (N1.1)
end-define
divide 1.2 into 4.3 giving #operand3 remainder #operand4
display #operand3 #operand4
end
```

---

<div class="post-metadata">

**Author:** ![Venkatesan\_Devaraj1](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@Venkatesan\_Devaraj1](https://techcommunity.softwareag.com/u/Venkatesan_Devaraj1)\
**Post date:** [February 7, 2006, 2:46pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/8 "2006-02-07T14:46:32Z")

</div>

Thanks for your reply. As you said the format of #Operand3 decideds the results of #operand4. It worked for N1.0 format alone.

Anyway we have used another syntax ‘DIVIDE 4 INTO #WS-TEMP-CCYY REMAINDER #WS-TEMP-REM-N’ and we got expected result here.  
In this case we don’t need to worry about format of #operand3 as we are not using.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 7, 2006, 10:04pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/9 "2006-02-07T22:04:58Z")

</div>

> [@](#):
>
> another syntax ‘DIVIDE 4 INTO #WS-TEMP-CCYY REMAINDER #WS-TEMP-REM-N’

But beware: the value of #WS-TEMP-CCYY is changed after the devide!

BTW: Now I realize, that you want to make a test for leap year…  
Just try this:

```auto
#n8 := #WS-TEMP-CCYY * 10000 + 0229 /* February 29th
if #n8 = MASK(YYYYMMDD)
   write "leap year"
else
   write "no leap year"
end-if
```

Because the rules for calculating a leap year are a bit complexer then “DIVIDE 4 into”. For example 2000 is a leap year, but 1900 isn’t …

---

<div class="post-metadata">

**Author:** ![djh](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@djh](https://techcommunity.softwareag.com/u/djh)\
**Post date:** [August 11, 2006, 8:23pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/10 "2006-08-11T20:23:03Z")

</div>

> [@Matthias](#):
>
> BTW: Now I realize, that you want to make a test for leap year…  
> Just try this:
> 
> ```auto
> #n8 := #WS-TEMP-CCYY * 10000 + 0229 /* February 29th
> if #n8 = MASK(YYYYMMDD)
> write "leap year"
> else
> write "no leap year"
> end-if
> ```
> 
> Because the rules for calculating a leap year are a bit complexer then “DIVIDE 4 into”. For example 2000 is a leap year, but 1900 isn’t …

Exactly, but the more comlex rules you mentioned were only introduced with the Gregorian Calendar ( i.e. years \>= 1582 or so). The above program classifies 600, 700, 1300, 1500 etc. as “no leap years”, when in fact they were: In Julian Calendar every year divisible by four without remainder is a leap year. So if one wants to be exact, one would have to check first if #ws-temp-ccyy is smaller than 1582, in which case the divisibility by four must be checked, or greater, in which case a test for 29 February can apply. 😉

---

<div class="post-metadata">

**Author:** ![thespin](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@thespin](https://techcommunity.softwareag.com/u/thespin)\
**Post date:** [August 11, 2006, 9:09pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/11 "2006-08-11T21:09:45Z")

</div>

And, please, when was the Julian Calendar implemented ?

EDIT: All you never wanted to know about the Julian Calendar:

> **[Julian calendar](https://en.wikipedia.org/wiki/Julian_calendar)**
>
> Pages for logged out editors learn more
> 
> 			 
> The Julian calendar, proposed by Roman consul Julius Caesar in 46 BC, was a reform of the Roman calendar. It took effect on 1 January 45 BC, by edict. It was designed with the aid of Greek mathematicians and astronomers such as Sosigenes of Alexandria.
> The calendar became the predominant calendar in the Roman Empire and subsequently most of the Western world for more than 1,600 years until 1582, when Pope Gregory XIII promulgated a minor modifi...

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:40pm UTC](https://techcommunity.softwareag.com/t/divide-and-remainder/60528/12 "2021-04-02T14:40:27Z")

</div>


