Opened 4 years ago

# symbolic constant e reverses relations

Reported by: Owned by: tmonteil major sage-8.4 symbolics rws N/A #18077

### Description

As discussed on this ask question:

```sage: x == e
e == x
sage: x > e
e < x
sage: x <= e
e >= x
```

Note that it only happens with `e`, this constant is handled in the dedicated `SAGE_ROOT/src/sage/symbolic/constants_c.pyx`.

This does not happen with the symbolic expression `e`:

```sage: x == e*1
x == e
sage: x > e*1
x > e
sage: x <= e*1
x <= e
```

• Cc rws added

### comment:2 follow-up: ↓ 3 Changed 4 years ago by jdemeyer

The reason is that `constant_E` is a subclass. When Python sees `a < b` where `type(b)` is a strict subclass of `type(a)`, it instead evaluates `b > a`.

So this gives us basically the following options:

1. Consider this not a bug and move on.
1. Ensure that `type(e)` is the same as `type(x)`.
1. Ensure that `type(e)` and `type(x)` are both subtypes of a common subclass.
Last edited 4 years ago by jdemeyer (previous) (diff)

### comment:3 in reply to: ↑ 2 ; follow-ups: ↓ 4 ↓ 5 Changed 4 years ago by tmonteil

The reason is that `constant_E` is a subclass. When Python sees `a < b` where `type(b)` is a strict subclass of `type(a)`, it instead evaluates `b > a`.

Aha, so it is at the Python level, thanks for the hint, i could not find the explanation within Sage source code.

So this gives us basically the following options:

1. Consider this not a bug and move on.

The problem comes from #26490, so in this case, we should do more parsing to replace lines such as `tempic=tempic+","+(dvar==ics[1])._maxima_().str()` to something that hardcodes maxima's "=" symbol. Also, from a user perspective, one could program something with `rhs` and `lhs` and get confused when such unexpected case appears.

1. Ensure that `type(e)` is the same as `type(x)`.

Would replacing

```e = E()
```

with

```e = E() * 1
```

in `SAGE_ROOT/src/sage/symbolic/constants.py` be considered as a correct fix ?

1. Ensure that `type(e)` and `type(x)` are both subtypes of a common subclass.

### comment:4 in reply to: ↑ 3 Changed 4 years ago by jdemeyer

Would replacing

```e = E()
```

with

```e = E() * 1
```

in `SAGE_ROOT/src/sage/symbolic/constants.py` be considered as a correct fix ?

If you want to do that, then why do we need the `constant_E` class in the first place? My guess is that it's mainly for documentation, such that `e?` gives information about `e` as opposed to generic expressions.

### comment:5 in reply to: ↑ 3 Changed 4 years ago by jdemeyer

To elaborate on this: for arithmetic operators like `__add__`, there is a reversed version like `__radd__`. But the reversed version of `__le__` is just `__gt__`. So user code cannot make a difference between `a < b` and `b > a`.