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?

gnat
  • 21,442
  • 29
  • 112
  • 288
Mirrana
  • 1,037
  • 2
  • 12
  • 20
  • 1
    Have you looked at it the other way around? Say, making `NaturalNumber` extend `Integer` instead. – cbojar Aug 03 '14 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 Aug 04 '14 at 00: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 Aug 04 '14 at 00:18
  • I would keep in mind [square-rectangle](http://programmers.stackexchange.com/a/238389/31260) (aka [circle-ellipse](http://programmers.stackexchange.com/a/199332/31260)) problem – gnat Aug 04 '14 at 08:29
  • see also: [What is the correct OOP relation between complex and real numbers?](http://programmers.stackexchange.com/questions/316507/what-is-the-correct-oop-relation-between-complex-and-real-numbers) – gnat Apr 22 '16 at 12:31

1 Answers1

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