Opened 4 years ago

Last modified 4 years ago

#26297 new defect

Fix comparison of symbolic constants

Reported by: embray Owned by:
Priority: major Milestone: sage-pending
Component: symbolics Keywords:
Cc: chapoton Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

Originally labeled this "python 3" but really this is buggy on Python 2 as well.

On Python 3 it at least straight up doesn't work:

sage: bool(golden_ratio < pi)
TypeError                                 Traceback (most recent call last)
<ipython-input-5-e16abba7bd09> in <module>()
----> 1 bool(golden_ratio < pi)

/home/embray/src/sagemath/sage-python3/local/lib/python3.6/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__nonzero__ (build/cythonized/sage/symbolic/expression.cpp:19335)()
   2851             # constants are wrappers around Sage objects, compare directly
   2852             if is_a_constant(self._gobj.lhs()) and is_a_constant(self._gobj.rhs()):
-> 2853                 return self.operator()(self.lhs().pyobject(), self.rhs().pyobject())
   2854             sig_on()
   2855             try:

TypeError: '<' not supported between instances of 'GoldenRatio' and 'Pi'

On Python 2 this may or may not give the right result, but in general it is wrong:

sage: bool(golden_ratio < pi)

This is because Expression.__bool__ does this:

 2849         if self.is_relational():
 2850             # constants are wrappers around Sage objects, compare directly
 2851             if is_a_constant(self._gobj.lhs()) and is_a_constant(self._gobj.rhs()):
 2852                 return self.operator()(self.lhs().pyobject(), self.rhs().pyobject())

Where the pyobject()s in this case are sage.symbolic.constants.Constant instances, which does not implement rich comparison (only __eq__).

How do we want to go about fixing this? What would be the best way? Realistically speaking we should be able to compare constants' float values. In theory one could define two constants that differ from each other only in the 100th decimal place or something, but is that really a realistic case?

See also #26492, #18077

Change History (4)

comment:1 Changed 4 years ago by embray

Milestone: sage-8.4sage-8.5

comment:2 Changed 4 years ago by embray

Milestone: sage-8.5sage-8.7

Retargeting some of my tickets (somewhat optimistically for now).

comment:3 Changed 4 years ago by jdemeyer

Description: modified (diff)

comment:4 Changed 4 years ago by embray

Milestone: sage-8.7sage-pending

Removing most of the rest of my open tickets out of the 8.7 milestone, which should be closed.

Note: See TracTickets for help on using tickets.