?
Solved

Big Numbers

Posted on 1998-12-05
3
Medium Priority
?
202 Views
Last Modified: 2010-04-02
I am working on writing a calculator program.  Right now I'm trying to implement a class for numbers bigger than an int. Right now I have code for storing the number, but I can't figure out how to display it to the screen.  It is stored in X number of chars.  And X increses when the number increases.  If no one can figure out how to display a binary number in decimal, then I might have to store a digit in each char.
0
Comment
Question by:Mithander
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1179327
>>If no one can figure out how to display a binary number
>>   in decimal, then I might have to store a digit in each char.
That technique is called BCD (binary coded decimal) and is a good technique for storing numbers if the math you will be doing is minimal and you will be doing lots of inputing and ouput of the numbers.  This is because BCD is very easy to read and write in decimal.  However it is not as efficent for using in mathematical calculations.  (But it is not too bad, I use it myself.)

However, if you want greater computational speed, then stick with your mult-byte binary value.  To convert it to decimal, you need to have a modulos operator.  (remainder).  What you do is start with the initial number and take the modulos with the highest power of ten that "fits" in it  This gives the first digit, then you "remove" the value of that digit and continue with the next lower power of ten to get the next digit.  This continues until the power fo ten is 1.  

continues.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1179328
For example to convert a binary value storing 234 to decimal, you would start with 100 as you first power of ten.  since it is the highest power of ten that is less than the number.  Then do

234 % 100 = 2

Thus, the first digit is 2.  Then you would take this digit and multiply by the power of ten to get 200, you would subtract this from the value, thus you get  234-200 = 34.  Now you would continue with 34 and the next power of ten, 10.  So you would do

34 %10 = 3

The next digit is 3.  Then youw would subtract of 3*10 = 30 and get 4.  So now you would use the last power of 10, 1  (10 to the 0).  and get

4 % 1 = 4.

The last digit is 4.

All of this can be done in a single loop, the power of 10 is set outside of the loop and each iteration converts another digit until the final digit is reached (power of 10 is 1).

Let me know if you have questions.
0
 

Author Comment

by:Mithander
ID: 1179329
Thanks for your quick respose nietod!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question