Irrational representation of numbers in any programming language?

Does anyone know about the irrational representation of the number / object / class / any in any programming language?

All suggestions are welcome.

Simply put, if I have two irrational objects representing the square root of five, and I multiply these objects, I want to return the integer five, and not float 4 points of the lot o '9s.

In particular, I need the view to collect terms, and not just solve the integer / float each time. For example, if I want to add the square root of five to one, I don’t want it to return some integer / float approximation, I want it to return an object that I can add / propagate with another irrational object, so I can say that the object should resolve at the most recent time in order to minimize the approximation error of the float.

Thank you so much!

+8
java c ++ python ruby numbers
source share
6 answers

What you are looking for is called symbolic math. You might want to try a computer algebra system like Maxima, Maple, or Mathematica. Libraries also exist for this purpose, such as the SymPy library for Python.

+14
source share

You can try sympy , since you are after a symbolic calculation and lend themselves to using Python.

+7
source share

It seems that the already mentioned SymPy would be the most suitable way - how you can do what you need and not do require your software to be written in a special language, for example, on the mentioned mathematical products.

On the other hand, if you don’t want to introduce additional dependencies, and your irrational cases are limited to multiplying square roots, this is not an easy task in Python:

class Irrational(float): def __new__(cls, base, radix=1): self = float.__new__(cls, base ** (1.0/radix)) self.base = base self.radix = radix return self def __mul__(self, other): if isinstance(other, Irrational) and other.radix == self.radix: return Irrational(self.base * other.base, self.radix) return float.__mul__(self, other) 

Example:

 >>> a = Irrational(5,2) >>> a 2.2360679774997898 >>> a * Irrational(5,2) 5.0 

You can continue it and include ohter operations and corner cases. But for compes expressions, you'll soon realize that you still have to use symbolic math.

+3
source share

The fundamental number type in Matlab is a matrix of complex floats. In particular, if you type x = 1 , then what you really assign x is a 1x1 matrix with an element [0,0] equal to 1 + 0i.

0
source share

In the ruby http://flt.rubyforge.org/ , which gives you what you want, I believe.

0
source share

I remember that MathMorph in Smalltalk has a representation for algebraic numbers (including radicals) as the root of a polynomial of a single variable with integer coefficients lying in a certain interval. You will find interesting applications of Sturm's theorem http://en.wikipedia.org/wiki/Sturm%27s_theorem
You will have to work a bit at Google and dig out the old archives, but MathMorph is an old project ...

0
source share

All Articles