Ask Your Question
2

Sage is not preparsing Python 3 underscored integers

asked 5 years ago

dsejas gravatar image

updated 5 years ago

Hello, Sage community!

With the upcoming migration of Sage from Python 2 to Python 3 (really soon, let us hope!), there are some testings I have been trying, and I have noticed that the "underscored integer literals", like "1_000_000" are not preparsed by Sage. For example,

preparse('1_000_000 + 3')

results in

'1_000_000 + Integer(3)'

However,

preparse('1000000 + 3')

returns

'Integer(1000000) + Integer(3)'

On the other hand, if I write

type(1_000_000 + 3)

the result is

<class 'sage.rings.integer.Integer'>

which is great, but

type(1_000_000 + 3_000)

instead gives me

<class 'int'>

Finally,

type(1000000 + 3000)

gives us

<class 'sage.rings.integer.Integer'>

I know one of the reasons for the Integer type was to allow divisions like 3/2 to return the float 1.5 even with Python 2, which would return (very disturbingly) 1, in other case. However, Python 3 integers already do this. Besides what I mentioned above and this, I haven't found any other difference in behavior.

So I was wondering: Is there any disadvantage in Sage not being preparsing this type of literals?

Preview: (hide)

Comments

Huh. I didn't even know this a feature of Python 3.

Iguananaut gravatar imageIguananaut ( 5 years ago )

It is quite useful for the clarity of the programs. I think "51_428_826" is more readable than "51428826".

dsejas gravatar imagedsejas ( 5 years ago )

1 Answer

Sort by » oldest newest most voted
3

answered 5 years ago

tmonteil gravatar image

updated 5 years ago

Iguananaut gravatar image

Good point. Indeed, the preparser is not able to handle such literals yet. This is now trac ticket 28490.

Preview: (hide)
link

Comments

Thank you, @tmontiel. I suppose I should have reported this directly as a bug. I will open an account in trac.sagemath.org for future reports. Thank you again!

dsejas gravatar imagedsejas ( 5 years ago )

This is not necessary, ask.sagemath.org is a good place to report bugs as well.

tmonteil gravatar imagetmonteil ( 5 years ago )

There is now a proposed fix at the ticket.

John Palmieri gravatar imageJohn Palmieri ( 5 years ago )

Will it be available at Sage-9.0? Thank you very much!

dsejas gravatar imagedsejas ( 5 years ago )

Hello, @tmontiel and @john-palmieri. I don't know which one of you should I refer this observation to, so I am mentioning both of you. I just compiled sage-9.0-beta3 with Python 3. The proposed fix works beautifully, except for the fact that numbers like 1_000.3 are preparsed like 1_000.gen(3), which produces an error.

I don't know thisgen() method, so I don't know how to fix this. Have you considered extending the fix for real numbers? Thank you for your efforts!

dsejas gravatar imagedsejas ( 5 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 468 times

Last updated: Oct 17 '19