bit_length()

707 views
Skip to first unread message

Roberto Catanuto

unread,
Dec 23, 2013, 4:14:53 PM12/23/13
to sage-...@googlegroups.com
Hi, I'm trying to run a code that contains these lines:
-----------------
num = 123
num.bit_length()
-----------------

and receive the following:
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'bit_length'

It works fine on local IDLE

Could you help me on that?

Thank you very much.

Harald Schilly

unread,
Dec 23, 2013, 5:03:59 PM12/23/13
to sage-...@googlegroups.com
When you are working in the "Sage Worksheet", you are by default using "Sage". In your case, there is something called "preparser" at work, which translates some input to Sage specific types or understands some additional grammar.

In your case, you can disable this by adding "%python" at the to top of the cell for the current cell. You can also disable this preparser just for the number:

num = 123r # <- note the "r"

Harald




--
You received this message because you are subscribed to the Google Groups "sage-cloud" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-cloud+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-cloud/1ed73365-33dc-411d-b84e-fdc182bd89af%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Simon King

unread,
Dec 23, 2013, 4:44:29 PM12/23/13
to sage-...@googlegroups.com
Hi Roberto,

On 2013-12-23, Roberto Catanuto <robe...@gmail.com> wrote:
> Hi, I'm trying to run a code that contains these lines:
> -----------------
> num = 123
> num.bit_length()
> -----------------
>
> and receive the following:
> AttributeError: 'sage.rings.integer.Integer' object has no attribute 'bit_length'

When you type something in an interactive Sage session, then Sage's
preparser adds some syntactic sugar. Using the command "preparse()" you
can see how the preparser expands the input. Examples:

sage: preparse("f(x)=x^2")
'__tmp__=var("x"); f = symbolic_expression(x**Integer(2)).function(x)'
sage: preparse("R.<x>=QQ[]")
"R = QQ['x']; (x,) = R._first_ngens(1)"
sage: preparse("123")
'Integer(123)'

In particular, if you type in an integer, then an instance of Sage's own
integer class results---but *not* a Python int!

So, either you need to explicitly create a python int:
sage: int(123).bit_length()
7
or you need to invoke the corresponding method of Sage integers:
sage: 123.nbits()
7

Best regards,
Simon


P Purkayastha

unread,
Dec 23, 2013, 9:57:25 PM12/23/13
to sage-...@googlegroups.com, simon...@uni-jena.de
Shouldn't the Integer type just create an alias method called bit_length? I always thought that the the sage Integer was a "superset" of the python int.
 

Roberto Catanuto

unread,
Dec 24, 2013, 10:46:05 AM12/24/13
to sage-...@googlegroups.com
On Monday, 23 December 2013 23:03:59 UTC+1, Harald Schilly wrote:
When you are working in the "Sage Worksheet", you are by default using "Sage". In your case, there is something called "preparser" at work, which translates some input to Sage specific types or understands some additional grammar.

In your case, you can disable this by adding "%python" at the to top of the cell for the current cell. You can also disable this preparser just for the number:


Good, now I understand it better!
thanks!
Reply all
Reply to author
Forward
0 new messages