Ask Your Question

Revision history [back]

The max function in Sage is the Python built-in function max, which relies on comparisons.

This stackoverflow answer explains roughly how it works. To compute max(a, b),

  1. Provisionally set the result to the first value, a.
  2. If b > a, change the result to b.
  3. Return the result.

In step 2, comparing objects of different types is tricky.

One would expect objects representing real numbers to compare well even if they are of different types. In your example, this fails.

As a practical suggestion: to compare numbers using < and >, first take them to a common parent where such comparison is defined properly.

In your case, if a = Integer(0) and b = mpmath.mpf(-1), you could compare RDF(a)and RDF(b) instead of comparing a and b. (Note: RDF stands for "real double field", a fast and reliable approximation to the field of real numbers, recommended in many contexts.)

sage: import mpmath
sage: a, b = Integer(0), mpmath.mpf(-1)
sage: max(a, b)
sage: max(b, a)
sage: max(RDF(a), RDF(b))
sage: max(RDF(b), RDF(a))