possible sage bug: True*2=1

7 views
Skip to first unread message

Marco Streng

unread,
Apr 3, 2009, 8:52:43 AM4/3/09
to sage-devel
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.

Some more details:
----------------------------------------------------------------------
| Sage Version 3.4, Release Date: 2009-03-11 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: True*1341234874
1
sage: True*int(1341234874)
1341234874
sage: type(True*1341234874)
<type 'int'>

William Stein

unread,
Apr 3, 2009, 10:03:42 AM4/3/09
to sage-...@googlegroups.com
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

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...

William


>
> Some more details:
> ----------------------------------------------------------------------
> | Sage Version 3.4, Release Date: 2009-03-11                         |
> | Type notebook() for the GUI, and license() for information.        |
> ----------------------------------------------------------------------
> sage: True*1341234874
> 1
> sage: True*int(1341234874)
> 1341234874
> sage: type(True*1341234874)
> <type 'int'>
>
> >
>



--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Robert Bradshaw

unread,
Apr 3, 2009, 3:08:50 PM4/3/09
to sage-...@googlegroups.com
On Apr 3, 2009, at 7:03 AM, William Stein wrote:

> 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

Nick Alexander

unread,
Apr 3, 2009, 3:13:27 PM4/3/09
to sage-...@googlegroups.com
> This should probably change, and
> ZZ._coerce_map_from_(bool) should return True.

Just to be clear: we are going to keep "True * 1" as a valid
multiplication? That seems bizarre to me.

Nick

Robert Bradshaw

unread,
Apr 3, 2009, 3:33:37 PM4/3/09
to sage-...@googlegroups.com

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

William Stein

unread,
Apr 4, 2009, 1:35:07 AM4/4/09
to sage-devel
2009/4/3 Robert Bradshaw <robe...@math.washington.edu>:

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

Reply all
Reply to author
Forward
0 new messages