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).