473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Implementing a Decimal class

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

Dec 17 '05 #1
4 1891
Look at the source for "dc" (Linux).
"dc is a reverse-polish desk calculator which supports unlimited
precision arithmetic."

Dec 17 '05 #2
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!

Dec 17 '05 #3
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 18 '05 #4
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

Dec 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
19
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Peter van der Zee | last post by:
L.S., I'm want to fill a Listbox with class Array "records" based on certain criteria. The idea is to be able to select one "record" at the time, modify the data, and display this "changed"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.