× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Re: Really big numbers
  • From: Jim Langston <jimlangston@xxxxxxxxxxxxxxxx>
  • Date: Fri, 05 Jan 2001 11:44:03 -0800
  • Organization: Pacer International

David,

Someone had already suggested Java and you had thought of Rexx,
and I was thinking it could be done in RPG, but it all depends
on what you want to do with this huge number.

The easiest way to store it is in a string, of course, but then
you're talking about a lot of bytes to store your number.  But,
you can with manipulation move these strings directly into zoned
number fields to work with sections of the numbers.

Another option is a slight modification to the type of BCD 
(binary coded decimal) that RPG uses.  BCD will basically store
2 digits in every byte, with one nibble (half a byte) used
for the sign.

The largest number we can store in a packed (BCD) field is
15 digits.  Which takes up 8 bytes.  I would suggest, however,
that you use 7 byte chunks to store them.

So what would you do if you wanted to store a 30 digit number?
Use 2 packed fields.  

Either way you do it, you're going to have fun with math.  If you
want to add two of these numbers together it's not too difficult,
you just have to handle your own overflow.  This is why you use
7 byte chunks, leaving one byte free for overflow.  Consider the
following all untested:

Call this function, passing first huge number string Huge1, second number
string Huge2, and blank string for result Huge3.

D        DS
D FirstNumber             2   7A
D FirstNumberPack         1   8P

D        DS
D SecondNumber            2   7A
D SecondNumberPack        1   8P

D        DS
D Result                  2   7A
D ResultPack              1   8P

D Carry                       1P
D I                           1P

D Carry                   
C      Eval    Current = 1
C      Eval    Chunks = 7 / %Length(Huge1)
C      Eval    Carry = 0

C      For     I = 1 to Chucks
C      Eval    FirstNumberPack = *Zero
C      Eval    FirstNumber  = %SubSt(Huge1: Chunks - I + 1: 7)
C      Eval    SecondNumberPack = *Zero
C      Eval    SecondNumber = %SubSt(Huge2: Chunks - I + 1: 7)

C      Eval    ResultPack = FirstNumberPack + SecondNumberPack + Carry
C      If      ResultPack > 9999999999999 
C      Eval    Carry = 1
C      Else
C      Eval    Carry = 0
C      EndIf

C      Eval    %SubSt(Huge3: Chunks - I + 1: 7) = Result

C      Next

Regards,

Jim Langston

Buck Calabro wrote:
> 
> David Morris wrote:
> 
> >Math enabled RPGIV'ers,
> >
> >What are some of the options for storing and manipulating
> >very large numbers where long float is just not large enough?
> >In this case to use a number greater than 170!.
> 
> I had to use Knuth and roll my own back in S/3 days.
> 
> If you use Java, you can't pass it back as a parameter because RPG can't
> handle a number of that size natively.  I assume you will toString() it, but
> then what can you do with a string of 230-odd digits in RPG besides print it
> out?  Will you write a Java "service program" to do all the work for you and
> pass strings back and forth?
> 
> I love the stuff you work on!  :-)
> 
> Buck Calabro
> Aptis; Albany, NY
> "Nothing is so firmly believed as
>  that which we least know" -- Michel Montaigne
> Visit the Midrange archives at http://www.midrange.com
> +---
> | This is the RPG/400 Mailing List!
> | To submit a new message, send your mail to RPG400-L@midrange.com.
> | To subscribe to this list send email to RPG400-L-SUB@midrange.com.
> | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
> | Questions should be directed to the list owner/operator: david@midrange.com
> +---
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.