This one just for fun today. An AskTOM question came in about arbitrary length arithmetic because *“NUMBER(38) was not enough”. *After some back-and-forth discussions it turned out that the business need under the requirement was managing bit strings. The implementation was currently converting the bits to decimals, hence the need for potentially very large number handling.

The problem was ultimately tackled with using RAW datatypes and holding the bits as raw strings, but I thought it would be interesting to throw together an addition and subtraction facility where the boundaries could exceed NUMBER(38).

So using nested tables, I had some fun with the code below.

```
SQL> set serverout on
SQL> declare
2 type integer_array is table of number;
3 n1 integer_array :=
4 integer_array(
5 4,3,5,6,7,8,2,3,5,3,5,3,2,5,4,6,7,6,2,1,5,2,3,5,7,3,6,3,1,7,8,5,
6 2,3,4,5,2,3,4,5,2,3,5,6,8,7,3,9,4,8,5,7,3,9,8,4,7,5,9,3,8,4,7,5,
7 9,3,8,7,4,5,9,8,3,7,4,5
8 );
9 n2sign int := -1;
10 n2 integer_array :=
11 integer_array(
12 0,0,0,0,0,0,3,4,5,2,3,4,5,2,4,3,7,6,8,5,6,7,5,6,7,6,7,8,6,7,8,5,
13 4,5,6,3,4,5,7,4,5,6,7,4,5,6,7,5,8,5,6,7,8,5,6,7,9,8,9,3,8,4,7,5,
14 3,4,6,4,5,4,6,5,7,7,4,5
15 );
16
17 res integer_array := integer_array();
18
19 procedure add(a1 integer_array, a2 integer_array, r in out integer_array) is
20 carry pls_integer := 0;
21 tmp pls_integer;
22 begin
23 for i in reverse 1 .. a1.count
24 loop
25 tmp := a1(i)+a2(i)+carry;
26 if tmp > 9 then
27 carry := 1;
28 tmp := tmp-10;
29 else
30 carry := 0;
31 end if;
32 r(i) := tmp;
33 end loop;
34 end;
35
36 procedure sub(s1 integer_array, s2 integer_array, r in out integer_array) is
37 carry pls_integer := 0;
38 tmp pls_integer;
39 begin
40 for i in reverse 1 .. s1.count
41 loop
42 tmp := S1(i)-S2(i)+carry;
43 if tmp
```

Definitely not complete implementations, but since addition and subtraction are things we learn in school, in the great tradition of school teachers around the world, I’ll close off this blog post with: *“The rest of the implementation is left as an exercise”*

- September 2018 (38)
- August 2018 (89)
- July 2018 (58)
- June 2018 (61)
- May 2018 (66)
- April 2018 (40)
- March 2018 (61)
- February 2018 (67)
- January 2018 (61)
- December 2017 (37)
- November 2017 (47)
- October 2017 (57)
- September 2017 (46)
- August 2017 (61)
- July 2017 (56)
- June 2017 (42)
- May 2017 (53)
- April 2017 (52)
- March 2017 (68)
- February 2017 (38)
- January 2017 (66)
- December 2016 (53)
- November 2016 (79)
- October 2016 (71)
- September 2016 (56)
- August 2016 (71)
- July 2016 (41)
- June 2016 (60)
- May 2016 (68)
- April 2016 (74)

## Recent comments

3 years 5 weeks ago

3 years 17 weeks ago

3 years 21 weeks ago

3 years 22 weeks ago

3 years 27 weeks ago

3 years 48 weeks ago

4 years 16 weeks ago

4 years 46 weeks ago

5 years 30 weeks ago

5 years 31 weeks ago