Ask Your Question
2

Sage is not preparsing Python 3 underscored integers

asked 2019-09-13 15:41:47 -0600

dsejas gravatar image

updated 2019-09-13 16:04:52 -0600

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?

edit retag flag offensive close merge delete

Comments

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

Iguananaut gravatar imageIguananaut ( 2019-09-18 04:05:24 -0600 )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 16:35:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-09-13 19:41:37 -0600

tmonteil gravatar image

updated 2019-10-17 07:25:44 -0600

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

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 ( 2019-09-14 19:50:11 -0600 )edit

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

tmonteil gravatar imagetmonteil ( 2019-09-15 06:09:37 -0600 )edit

There is now a proposed fix at the ticket.

John Palmieri gravatar imageJohn Palmieri ( 2019-10-21 19:39:54 -0600 )edit

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

dsejas gravatar imagedsejas ( 2019-10-22 14:07:05 -0600 )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 11:31:45 -0600 )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

Stats

Asked: 2019-09-13 15:41:47 -0600

Seen: 168 times

Last updated: Oct 17