Continuing the discussion from 33. Methods:
We should consider some of the goals. One of them would be to enable calculations that utilize π
and Τ
that preserve them as symbols when either of them enter into a calculation. For example the result of an expression such as …
π * 2 ** 2
… would be preserved as …
4 * π
… or something similar to that, as opposed to a float
.
Here’s some raw material for experimentation and refinement, tested in Python 3.6 …
class PiType(object):
def __init__(self, magnitude):
self.magnitude = magnitude
def __add__(self, other):
# PiType + PiType
return PiType(self.magnitude + other.magnitude)
def __sub__(self, other):
# PiType - PiType
return PiType(self.magnitude - other.magnitude)
def __mul__(self, other):
# PiType * int or PiType * float
return PiType(self.magnitude * other)
def __rmul__(self, other):
# int * PiType or float * PiType
return PiType(self.magnitude * other)
def __str__(self):
# str representation
return "{:s}π".format(str(self.magnitude))
def circle_area(radius):
return PiType(radius * radius)
num1 = PiType(4)
num2 = PiType(5)
num3 = PiType(2.7)
print(num1)
print(num2)
print(num3)
print(num1 * 7)
print(8 * num1)
print(num1 + num2)
print(num1 - num2)
print(num2 - num1)
print(circle_area(4.0))
Output …
4π
5π
2.7π
28π
32π
9π
-1π
1π
16.0π
I set up a GitHub repository for us to experiment with.
Hi @aquaphoenix17,
This quote from the post that initiated this discussion in another topic captures the essence of the current problem …
While the SCT can deal with floating point numbers, it does not handle irrational numbers in a … um … rational manner. No matter how precise a numerical representation of τ it has, it is never equal to τ. It checks for
6.283185307179586
, but that is not quite τ.
It appears that what we need is a programming package that handles symbolic math. For Python, there is SymPy.
For some examples of how SymPy can be used, see 3.2. Sympy : Symbolic Mathematics in Python. That document includes this …
>>> pi**2
2
pi
>>> pi.evalf()
3.14159265358979
>>> (pi + exp(1)).evalf()
5.85987448204884
Note in the above that pi
can be maintained as a symbol in the result of a computation, or it can be converted to a float
, with some loss of precision, of course.
The page also include this, where oo
represents infinity …
>>> oo > 99999
True
>>> oo + 1
oo
For JavaScript there are the following, but I have not taken a detailed look at them yet …