0

I've run into an interesting conundrum while coding my own implementations for the basic sets of mathematical numbers (Natural, Integer, Rational, Irrational, Imaginary, Complex). I want properly represented numbers in code but I don't have any idea. I want you from ideas, ways to make it. I think base class should be complex numbers. Real numbers and imaginary numbers derived from the complex numbers (as subset). Rational and irrational number derived from the real numbers. Integers derived from the rational numbers. What should be private members in the classes ? How can the irrational numbers represented? Is there any pseudo-code?

askque
  • 319
  • 2
  • 9

3 Answers3

1

This is a rather opinion-based question. However, I would say that the model you have proposed does not extend well to other algebras such as quaternions, bicomplex numbers, etc., many of which are mutually exclusive. For example, quaternions and tessarines are both four-dimensional, but are incompatible -- so where would they fit into a class hierarchy? Complex numbers are compatible with both, but it doesn't make sense to extend both of them. Clifford algebras of dimension > 4 are incompatible with Cayley-Dickson algebras of dimension > 4, and so forth.

You might want to look at how various standard libraries in C++ and other languages represent numbers. For example, look at java.lang.Number, NSNumber in Objective-C, etc. Typically, there is a base class along the following lines, that provides conversion methods:

class my_scalar {
public:
    virtual int as_int() = 0;
    virtual double as_double() = 0;
    virtual float as_float() = 0;
    virtual my_scalar operator+(const my_scalar &other) = 0;
    virtual my_scalar operator-(const my_scalar &other) = 0;
    // etc.
}

(Please excuse my probably faulty C++, it's very rusty....)

Then you can define your subclasses representing concrete types of scalar:

class my_double : public my_scalar {
private:
    double value;
    ...
}

class my_rational : public my_scalar {
private:
    int num, denom;
}

...

If you are interested in arbitrary precision numbers, you could represent them as subclasses of my_scalar as well (e.g. java.lang.BigDecimal or Python's Decimal).

Then, for non-scalar types, I would favor composition over inheritance, thus:

class my_complex {
private:
    double re, im;
    ...
}

You could even go whole hog and parameterize my_complex with a scalar type, e.g.:

template <class T>
class my_complex {
public:
    my_complex(const T& re, const T& im) { ... }
    T *modulus() { ... }
private:
    T *re, *im;
}

Then you could use either double, my_scalar, my_double, or whatever you want for the parameterized type.

You could define similar classes for quaternions, etc.

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
0

Take a look at how the language of your choice encodes number, maybe you can mimic it.

If you want to build it entirely yourself, you could start with natural numbers encoded as bit strings or bytes, then use those in a floating point representation for your real numbers (https://en.wikipedia.org/wiki/Floating_point). You should be able to use those two to implement the others (e.g. integer has a natural number and a sign bit, rational numbers have an integer as numerator and a natural number as denominator, imaginary numbers use irrational numbers, complex numbers use an rational number for the real part and an imaginary number for the imaginary part). This is of course only one possible implementation! There are many known improvements. If you really want to go all out, I suggest you take a look at this question: How are integers internally represented at a bit level in Java?

Community
  • 1
  • 1
delucasvb
  • 5,393
  • 4
  • 25
  • 35
0

There are basically two approaches

  1. in first you model real number as a special case of complex number and so on...
  2. or you model it from the simplest one to the more complex one similarly Eric Galluzzo described in his answer

whether you select first or second is up to you.

Problem in Java might be that real numbers are join or rational and irrational numbers, but in Java there is just one parent for each class...

Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110