Thread View: comp.os.os2.programmer.misc
2 messages
2 total messages
Started by bretz
Sat, 09 Dec 1995 20:14
BCD arithmetic algorithms needed
Author: bretz
Date: Sat, 09 Dec 1995 20:14
Date: Sat, 09 Dec 1995 20:14
23 lines
1109 bytes
1109 bytes
Hello, I am planing to implement a c++ class for bcd arithmetic and for accurate representation of monetary and the like. Currently I have an idea for adding packed or unpacked bcds by adding each digit and saving the decimal carry. I plan to use the Intel 80x86 bcd instructions (AAD, AAM, AAS, AAD, DAA and DAS) to implement the arithmetic. but I have no good idea for subtracting, multiplying and deviding packed or unpacked bcds. Can somebody help??? Thank you Thomas _____________________________________________________________________ bretz@ibm.net Thomas Bretz-Rieck Hamburg, Germany _____________________________________________________________________
Re: BCD arithmetic algorithms needed
Author: Terje Mathisen
Date: Sat, 09 Dec 1995 22:36
Date: Sat, 09 Dec 1995 22:36
34 lines
1891 bytes
1891 bytes
bretz@ibm.net wrote: > > Hello, > > I am planing to implement a c++ class for bcd arithmetic > and for accurate representation of monetary and the like. > > Currently I have an idea for adding packed or unpacked > bcds by adding each digit and saving the decimal carry. > I plan to use the Intel 80x86 bcd instructions (AAD, AAM, > AAS, AAD, DAA and DAS) to implement the arithmetic. > > but I have no good idea for subtracting, multiplying and > deviding packed or unpacked bcds. Unless you're going to do _very_ little arithmetic on your numbers, I suggest you use a more efficient way to store your numbers, using a higher power of ten as your base. If you store 9 digits in each 32bit unsigned long, addition and subtraction is easy, and you don't have to do nearly as much work to implement multiplication either. Division is a bit harder, but not too bad, you just use the same approach you learned in grade school: Use the first few digits of divisor and dividend to guess the next digit, multiply back and compare, adjusting your guess if needed. Since each digit is going to be on the order of 1G, this works out very well. -- -Terje Mathisen (include std disclaimer) <Terje.Mathisen@hda.hydro.com> "almost all programming can be viewed as an exercise in caching"
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads