Hyperoperations Implementation in Python, Part 4. - Negative Numbers
from https://en.wikipedia.org/wiki/Negative_number

Hyperoperations Implementation in Python, Part 4. - Negative Numbers

If you missed earlier parts, you can start with Part 1 here:

or Part 3 here:

In this installment, we're expanding the system from Natural numbers to Integers by adding support for negative numbers.

As before, source can be found here: https://github.com/casten/number/

It's not the largest change compared to previous work. The support is accomplished by adding an element that tracks the sign of the number, positive or negative. Before this, the internal state of the number was just a unary number indicating the magnitude. A new type, NumberState has been added which is comprised of both a magnitude and sign. Additional related operations were added to support comparison and extraction of the values from the NumberState.

With the new state, the numeric operations were updated to handle interactions between positive and negative numbers. These follow the rules laid out some 1400 years ago in 7th Century India by Brahmagupta. These are:

A debt minus zero is a debt
A fortune minus zero is a fortune.
Zero minus zero is a zero.
A debt subtracted from zero is a fortune.
A fortune subtracted from zero is a debt.
The product of zero multiplied by a debt or fortune is zero.
The product of zero multipliedby zero is zero.
The product or quotient of two fortunes is one fortune.
The product or quotient of two debts is one fortune.
The product or quotient of a debt and a fortune is a debt.
The product or quotient of a fortune and a debt is a debt.        

Most of the new work above is handled in just a few lines for mul and div.

A lot of the work was handling "edge cases", i.e. zeros and ones, for the log and superlog operators. I tried to not punt with exceptions for cases that I thought could return a reasonable result. Examples include:

  • log base -1 of 1 = SpecialNumber(Any Even)
  • log base -1 of -1 = SpecialNumber(Any Odd)
  • log base -2 of 4 = 2, mantissa=0 (mantissa = remainder)

This one is arguable, but I've set 0^0=1 since anything to the 0 power is one. Inversely, log base 0 of 0 = 1 and for other logs base 0, it is undefined since you just can't get any other values. Could there be some special power that you could raise 0 to (other than 0) to get a non-zero number? Perhaps in some system you could have a case where the power's cardinality could trump the zero's none-ness in some meaningful way, e.g. to get a non-infinite result.

I also added the conservation of sign for zero. It doesn't seem particularly useful.

The rest of the work is largely test cases and structuring. You can check out the new tests starting here: https://github.com/casten/number/blob/main/Number_tests.py#L38


Conclusion?

I suppose the next target might be Rational numbers. These also might be interesting to add support in the Expressions for. Although the prospect of grinding through factors to find lowest common denominators doesn't sound super fun given the performance of the unaries and functional copying.

To view or add a comment, sign in

Others also viewed

Explore topics