Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function math.log10 fails on large Sage integers while ok on Python ints #28912

Open
ismail opened this issue Dec 26, 2019 · 8 comments
Open

Comments

@ismail
Copy link

ismail commented Dec 26, 2019

Using SageMath version 9.0.beta10 via docker image.

sage: x=4**10000                                                                                                                                                             
sage: from math import log10                                                                                                                                                 
sage: type(x)                                                                                                                                                                
<class 'sage.rings.integer.Integer'>                                                                                                                                         
sage: log10(x)                                                                                                                                                               
inf                                                                                                                                                                          
sage: log10(int(x))                                                                                                                                                          
6020.599913279624

Related:

CC: @slel

Component: numerical

Issue created by migration from https://trac.sagemath.org/ticket/28912

@ismail ismail added this to the sage-9.0 milestone Dec 26, 2019
@slel
Copy link
Member

slel commented Dec 27, 2019

comment:1

Confirmed in Sage 9.0.beta10.

Smallest Sage integers where this fails:

sage: a = (2^54-1)*2^(1024-54)
sage: aa = int(a)

Import log10 from the math module:

sage: from math import log10
sage: [log10(x) for x in [0.01, 0.1, 1, 10, 100]]
[-2.0, -1.0, 0.0, 1.0, 2.0]

Computing log10(aa) works, but not log10(a):

sage: log10(aa)
308.25471555991675
sage: log10(a)
inf

No problem with a - 1 as a Sage integer:

sage: type(a-1)
<class 'sage.rings.integer.Integer'>
sage: log10(a-1)
308.25471555991675

Workaround:

sage: RR(a).log()/(10.0).log()
308.254715559917

Note that Sage has a built-in method for the "number of digits"
of an integer (a common reason for taking the base ten logarithm):

sage: a.ndigits()
309
sage: a.ndigits(base=2)
1024

@slel
Copy link
Member

slel commented Dec 27, 2019

comment:2

More descriptive ticket title.

@slel slel changed the title sage.rings.integer.Integer is problematic Function math.log10 fails on large Sage integers while ok on Python ints Dec 27, 2019
@ismail
Copy link
Author

ismail commented Dec 27, 2019

comment:3

More of the same problem:

sage: math.log2(a)                                                                                                                                                           
inf                                                                                                                                                                          
sage: math.log(a)                                                                                                                                                            
inf                                                                                                                                                                          
sage: math.isinf(a)                                                                                                                                                          
True 

@embray
Copy link
Contributor

embray commented Jan 6, 2020

comment:4

Ticket retargeted after milestone closed

@embray embray modified the milestones: sage-9.0, sage-9.1 Jan 6, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented May 1, 2020

comment:5

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 1, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Oct 24, 2020
@slel

This comment has been minimized.

@mkoeppe
Copy link
Contributor

mkoeppe commented May 10, 2021

comment:8

Moving to 9.4, as 9.3 has been released.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 May 10, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Aug 22, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 May 3, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@DaveWitteMorris
Copy link
Member

I'm certainly not an expert, but I don't think we can fix this without making Integer a subclass of int (or patching python).

I say this because it seems from line 2349 of cpython/Modules/mathmodule.c that math.log10 calls loghelper, and the code for loghelper that handles a pythonint is conditional on a specific verification ("if (PyLong_Check(arg))" in line 2252) that the argument is an int.

So perhaps this issue should go on the wishlist.

PS. On the other hand, I think it would make a lot of sense to have Integer be a subclass of int, but I'm assuming there are good reasons why it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants