Thread: mixed content class, how to

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question mixed content class, how to

    Hi! I have thought of making a math library with some different kind of variable types.

    The meaning is to keep the values exact, so I will have one class for integers, using a vector to store the digits, making the integer able to reach unlimited length. And when two integers is divided by each other a fraction is obtained, hence a new class will have to be used. The fraction class shall contains to integers. Irrational numbers is skipped since you'll need to have an expression for them, and I haven't planed to make an expression handler at the time. Also a decimal number class is going to be needed, cause decimal numbers is pretty common and can easily be written to the screen without using a fraction, just a decimal point. A real number is any of the three mentioned before. Then I shal have imaginary number class, which can contain any of the two classes mentioned before, the only difference is that when you ad an imaginary number with a real number for example, you get a complex number. This is what gives the fifth class, complex numbers. An operation can give different type of outputs, depending on the type of input it gets, and what values the contain. For example, every result that can be expressed as an integer shal be returned as an integer. Any decimal number times integer number 0 shall give integer number zero, but if you multiply it by integer number 1, it'll give the same decimal number back. And two integer divided by each other will return a fraction if necessary, possibly NAN (not a number) when dividing by 0, else an integer number shall be returned.

    This is somewhat like PHP's variable type. You can't define it to be either an integer or a float, or even a string. PHP converts wildely between the different type of variables. Does anyone know how it works? Is it a good idea to imitate PHP's way of storing the variables or is there some better way?

    I have thought of having a container, which contains a unary with the number in it, and one variable to tell which kind of number it is. Then every operation or function applied to the content shall always return a container, so the type can be choosen from within the function (for example the function real, which takes the real part from any number, maybe an integer or complex, and returns any real number necessary to represent the real part of the number).
    Come on, you can do it! b( ~_')

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your ideas sound about right. I would think that's roughly how PHP works too, it's got a structure that has storage for either of a string, float or int (using a type enum and a union, most likely). I don't THINK PHP supports really huge integer values, but that's perhaps beside the point. Along, of course, with suitable functions to convert between the different types, such as making a string into an int, or int to float.

    In your case, I'd suggest something along these lines:
    Code:
    struct integer {
       bool sign;    //  true => negative, false = positive.
       size_t length;
       char *number;
    };
    
    struct fraction {
       struct integer upper;
       struct integer lower; 
    };    // upper / lower 
    
    struct decimal {
       bool sign;
       size_t length;
       size_t decimalpoint;   // 12345.6789 -> decimalpoint = 5, 1.471 -> decimalpoint = 1
       char *number;
    }; 
    
    union unumber {
       integer  int;
       fraction frac;
       decimal dec;
    };
    
    class number {
    private:
       unumber num;
       ... 
    public:
       number operator+(number &a);
       .... 
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest using the GNU Multiple Precision Arithmetic Library and the std::complex class template as a base for your implementation. In fact, I suspect that you can just use them directly by implementing functions/operators that do what you want (give different type of outputs, depending on the type of input it gets, and what values the contain).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And if you really need proper variants, use Boost.Variant instead of rolling your own implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I guess variant is a template class defined in boost's libraries ... how do you use it? And how is it constructed, so I know what it really does? Thanks.
    Come on, you can do it! b( ~_')

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Variant has very good documentation, including a tutorial.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    What is the benefits with using a boost variant instead of a union? What's the drawbacks?
    Come on, you can do it! b( ~_')

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A union can't hold non-POD types (pretty much any class of reasonable complexity) and is not in any way type-safe. A variant can hold any type and is type-safe: statically for variant visitors and dynamically for variant casts. The downside is that it needs a little more space, to hold the type indicator.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM