Ask Your Question

Sage is not preparsing Python 3 underscored integers

asked 2019-09-13 22:41:47 +0200

dsejas gravatar image

updated 2019-09-13 23:04:52 +0200

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)'


preparse('1000000 + 3')


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


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?

edit retag flag offensive close merge delete


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

Iguananaut gravatar imageIguananaut ( 2019-09-18 11:05:24 +0200 )edit

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

dsejas gravatar imagedsejas ( 2019-09-30 23:35:19 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-09-14 02:41:37 +0200

tmonteil gravatar image

updated 2019-10-17 14:25:44 +0200

Iguananaut gravatar image

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

edit flag offensive delete link more


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

dsejas gravatar imagedsejas ( 2019-09-15 02:50:11 +0200 )edit

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

tmonteil gravatar imagetmonteil ( 2019-09-15 13:09:37 +0200 )edit

There is now a proposed fix at the ticket.

John Palmieri gravatar imageJohn Palmieri ( 2019-10-22 02:39:54 +0200 )edit

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

dsejas gravatar imagedsejas ( 2019-10-22 21:07:05 +0200 )edit

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 ( 2019-10-30 17:31:45 +0200 )edit

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


Asked: 2019-09-13 22:41:47 +0200

Seen: 392 times

Last updated: Oct 17 '19