0

I've run into an interesting conundrum while coding my own implementations for the basic sets of mathematical numbers (Natural, Integer, Rational, Irrational, Real, Complex). I'm doing this mostly for fun, but also because I want properly represented numbers in code.

It started off with RationalNumber. I added two fields to my class: Numerator and Denominator. I've overloaded all the basic operators that I am able to (I'm using C#) so that I can define what it means to add, subtract, etc.. for these objects.

From here, I thought that I'd extend my definitions upward and make a RealNumber class such that this type can extend from it so that it could fill in for any RealNumber elsewhere in code... Except intuitively, the number system operates contrary to this definition.

Illustrating what I mean, consider starting off with the basic natural numbers. We can "extend" the natural numbers to include negative numbers as well (and zero if you weren't already including it) and then we call this new group the integers. In code, this would imply that Integer inherits from NaturalNumber. If I wanted to write a method that accepts an integer parameter, logically one would assume you could use a natural number as well; but with the way inheritance works, this doesn't fly.

How can I properly model the relationship between the sets of numbers through inheritance so that I can put a natural number into?

5
  • 1
    Have you looked at it the other way around? Say, making NaturalNumber extend Integer instead.
    – cbojar
    Commented Aug 3, 2014 at 23:50
  • That's what I wanted to do, and I could make it work, but it just seemed backwards from how I understood the relationship between the sets. I was thinking about it from an object oriented perspective where subclasses extended the superclass.
    – Mirrana
    Commented Aug 4, 2014 at 0:03
  • You can think of extension as a "kind of" relationship. A dog is a kind of animal, so dog extends animal. In the same way NaturalNumber is a kind of Integer.
    – cbojar
    Commented Aug 4, 2014 at 0:18
  • I would keep in mind square-rectangle (aka circle-ellipse) problem
    – gnat
    Commented Aug 4, 2014 at 8:29
  • see also: What is the correct OOP relation between complex and real numbers?
    – gnat
    Commented Apr 22, 2016 at 12:31

1 Answer 1

1

You're thinking about it backwards. Integers aren't an extension of Natural Numbers in that sense, they are a superset. So Natural Numbers inherit from Integers with the proviso that they can't be negative or zero. An unsigned integer equivalent would be a natural fit.

Bear in mind that many of the machine versions of number types aren't quite the same as the way a mathematician might define a number type. Haskell has an interesting way of defining numbers. You might be able to implement something similar in C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.