Opened 12 years ago

Closed 10 years ago

#9953 closed defect (duplicate)

int(symbolic expr) off by 1

Reported by: Harald Schilly Owned by: Burcin Erocal
Priority: critical Milestone: sage-duplicate/invalid/wontfix
Component: symbolics Keywords:
Cc: Fredrik Johansson, Karl-Dieter Crisman, Zafeirakis Zafeirakopoulos, Michael Orlitzky Merged in:
Authors: Reviewers: Burcin Erocal
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges


Problem, last digit is either 6 or 7. In short:

fermat(n) = 2**2**n + 1
fermat(9) gives ....4097 but:
int(fermat(9)) gives ...4096L.
Same with: long(fermat(9)).
int(2**2**9 +1) gives ...4097L

Burcin says: int(x) for a symbolic expression x just calls int(x.n(prec=100)). We lose that 1 in the approximation.

full example in 4.5.2:

sage: fermat(n) = 2**2**n + 1
sage: fermat(9)
sage: int(fermat(9))
sage: long(fermat(9))
sage: int(2**2**9 +1)

Attachments (1)

9953.patch (819 bytes) - added by David Roe 12 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 12 years ago by Karl-Dieter Crisman

Cc: Karl-Dieter Crisman added

Changed 12 years ago by David Roe

Attachment: 9953.patch added

comment:2 Changed 12 years ago by David Roe

Status: newneeds_work

I don't want to work on this much, but I believe that this patch should fix the problem. If someone wants to add some doctests and polish it a bit, go for it.

comment:3 Changed 12 years ago by D.S. McNeil

The current patch won't work for 0 or negative numbers (the log2 breaks). This can be easily fixed, but dealing with cases like

sage: f = 1-1/10**100
sage: int(f)
sage: int(SR(f))

where the precision needed to get the right result isn't a function of the size is a little trickier. Maybe fast-path the common cases and then fall back on the existing floor/ceil logic?

comment:4 Changed 12 years ago by D.S. McNeil

This appears to be a duplicate of #9627.

comment:5 Changed 11 years ago by Zafeirakis Zafeirakopoulos

Cc: Zafeirakis Zafeirakopoulos added

comment:6 Changed 10 years ago by Michael Orlitzky

Cc: Michael Orlitzky added

comment:7 Changed 10 years ago by Burcin Erocal

Milestone: sage-5.1sage-duplicate/invalid/wontfix
Reviewers: Burcin Erocal
Status: needs_workpositive_review

#12968 has a patch with a positive review which fixes this. We should close this as a duplicate.

comment:8 Changed 10 years ago by Jeroen Demeyer

Resolution: duplicate
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.