> On Fri, Apr 3, 2009 at 5:52 AM, Marco Streng
> <marco....@gmail.com> wrote:
>>
>> I found that in Sage 3.4,
>> True * Integer(2) returns int(1)
>>
>> I think it would be better if the output were either 2 or a type
>> error. This seems to be a problem with Sage integers, python integers
>> work just fine.
>
> More generally, it would seem that bool * (any Sage element) returns
> int(1) if the Sage element is nonzero and the bool is True, and
> otherwise returns int(0):
>
> sage: True*(2/3)
> 1
> sage: True*x
> 1
> sage: type(True*x)
> <type 'int'>
> sage: False*(2/3)
> 0
> sage: True*0
> 0
Wow, interesting.
>
> Since this is inconsistent with Python's conventions, I think it
> should be changed. The most natural change would be to canonically
> coerce bool to the Python ints 0 and 1, then do the multiply, since
> then everything works as it should.
>
> This will require some change to coerce.pyx, which would be easiest
> for Robert Bradshaw to make...
Currently bool is considered a "numeric type" (this goes way back) so
on failure to actually multiply, it tries to turn both into bools and
to the "multiplication" there. This should probably change, and
ZZ._coerce_map_from_(bool) should return True.
I'll post a patch if no one beats me to it.
- Robert
Just to be clear: we are going to keep "True * 1" as a valid
multiplication? That seems bizarre to me.
Nick
Well, I'm not stuck on it, it'd be mostly to follow Python here
>>> 1 * True
1
>>> 2 * True
2
>>> 1.25 + True
2.25
It does feel a bit odd...
- Robert
Odd or not, it is a fact that in Sage if n is a Python int/float then
the above works as given (and this is the case even in Python 3.0).
I think we should just follow Python for consistency here.
William