Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Implementing a Decimal class

Reply
Thread Tools

Implementing a Decimal class

 
 
tajmorton
Guest
Posts: n/a
 
      12-17-2005
Hello,
I've decided to try my hand at writing a decimal class that is suitable
for storing weights, money, and time, etc, because I haven't found a
*decimal* class (yes, I know that boost and gmp have rationals, but
that's not exactly easy to use for money and weights)

I'm storing the number in a signed long int, along with another int
called offset which contains where the decimal point goes. So far I
have implemented addition, subtraction, and multiplication. Those were
easy, but I need some tips on how to do division.

I need to calculate as many decimal places as I can without, without
running out of room in long int. I can store only 10 digits... For
example, 1000/362.33 would need to be rounded to 2.759914995, instead
of 618165760657813034839591637137[...]. Any ideas how to actually
implement this?

Also, I'd like some input on if the way I'm doing this is really the
right way, thoughts?

Thanks!
--
Taj

 
Reply With Quote
 
 
 
 
edward.birch@gmail.com
Guest
Posts: n/a
 
      12-17-2005
Look at the source for "dc" (Linux).
"dc is a reverse-polish desk calculator which supports unlimited
precision arithmetic."

 
Reply With Quote
 
 
 
 
Viktor Prehnal
Guest
Posts: n/a
 
      12-17-2005
I am not sure what you are asking for but for rounding numbers there are
floor() and ceil functions....


tajmorton wrote:
> Hello,
> I've decided to try my hand at writing a decimal class that is suitable
> for storing weights, money, and time, etc, because I haven't found a
> *decimal* class (yes, I know that boost and gmp have rationals, but
> that's not exactly easy to use for money and weights)
>
> I'm storing the number in a signed long int, along with another int
> called offset which contains where the decimal point goes. So far I
> have implemented addition, subtraction, and multiplication. Those were
> easy, but I need some tips on how to do division.
>
> I need to calculate as many decimal places as I can without, without
> running out of room in long int. I can store only 10 digits... For
> example, 1000/362.33 would need to be rounded to 2.759914995, instead
> of 618165760657813034839591637137[...]. Any ideas how to actually
> implement this?
>
> Also, I'd like some input on if the way I'm doing this is really the
> right way, thoughts?
>
> Thanks!

 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      12-18-2005
tajmorton wrote:
> I'm storing the number in a signed long int, along with another int
> called offset which contains where the decimal point goes.


I wouldn't use a limited representation for a decimal class. Instead,
I would use a variable size array of unsigned characters each holding
two decimal digits and a sign stored somewhere.

> So far I
> have implemented addition, subtraction, and multiplication. Those were
> easy, but I need some tips on how to do division.


At least a naive implementation is to use the usual approach to
division you also use when dividing manually. There are more tricky
approach to compute with arbitrary bases and if I remember correctly
Donald Knuth' "Art of Computer Programming" discusses these.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Mathias Waack
Guest
Posts: n/a
 
      12-18-2005
tajmorton wrote:

> Hello,
> I've decided to try my hand at writing a decimal class that is
> suitable for storing weights, money, and time, etc, because I
> haven't found a *decimal* class (yes, I know that boost and gmp
> have rationals, but that's not exactly easy to use for money and
> weights)
>
> I'm storing the number in a signed long int, along with another int
> called offset which contains where the decimal point goes. So far I
> have implemented addition, subtraction, and multiplication. Those
> were easy, but I need some tips on how to do division.
>
> I need to calculate as many decimal places as I can without,
> without running out of room in long int. I can store only 10
> digits... For example, 1000/362.33 would need to be rounded to
> 2.759914995, instead of 618165760657813034839591637137[...]. Any
> ideas how to actually implement this?


I'm not sure if you really wants this. I'd assume, if you calculate
an instance of you class as the result of 1000/362.33 (name it x),
what you want is x*362.33 == 1000 and 1000/x == 362.33.
Or suppose someone calculates x 10 times, calculates the sum of these
and divides it by 10. (Its a very common operation while playing
with money - consider invoices, different currencies and a customer
paying multiple invoices at once).

> Also, I'd like some input on if the way I'm doing this is really
> the right way, thoughts?


I think a better way is to store the amounts as fractionals (ie using
two longs for numerator/denominator). So you don't need any division
during an internal calculation. Your results are always correct.
Only the user visible output needs to be rounded.

Mathias

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal.Decimal formatting python@lists.fastmail.net Python 0 07-19-2010 12:32 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments