C++
  Home arrow C++ arrow Page 2 - Large Numbers
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Large Numbers
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-07-15

    Table of Contents:
  • Large Numbers
  • The User Interface
  • File Input Lesson
  • Addition and Subtraction
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Large Numbers - The User Interface


    (Page 2 of 5 )

    The class we are creating is going to hold large numbers. That is, to be more specific, a really long sequence of one digit numbers accompanied by its sign, and obviously we also need to differentiate between the integer and real part of the number. The sign should be also known at all times.

    So the question is how to store the numbers - specifically, a one digit number. That's 10 options (0, 1, 2... 8, 9). The memory blocks in computers are divided into bytes. One byte equals eight bits. On one byte we can store in the binary file system 2 ^ 8 = 256. That's from 0 to 255.

    Therefore, we've just found out that a byte is entirely enough for us, though we are also wasting some memory if we choose to store them in a byte. Half of it (2^ 4 = 16) could be enough, but in C there exists no such variable that's stored in four bits.

    All in all, we'll stick with the eight bit plan following the principle of keeping it as simple as possible, especially to make the code easier to comprehend. However, if you're feeling brave then you should know that there exists a solution to this. This is accomplished by splitting the char into two section using bit fields. You will gain some extra memory usage improvement, but you will lose some speed and have more difficulty finding and working with a specific item. The bottom line is that I used the following design. 

    For the dynamic allocation issue in the memory I've used STL. The obvious option is std::vector<char> that is the same as std::string. We are signaling the place of the "." (or "," in some countries) with the whole member, so we don't need to store that character in the number, which will only contain all the number sequence.

    private:

    std::string number; // contains the number

    unsigned long int whole;// shows the whole part

    unsigned long int all; // the length of the number

    short int sign; // the sign of the number

    More C++ Articles
    More By Gabor Bernat


       · Learn, learn and than again learn. Finally learn from your mistakes and change your...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT