Opened 11 years ago

Closed 9 years ago

#9953 closed defect (duplicate)

int(symbolic expr) off by 1

Reported by: schilly Owned by: burcin
Priority: critical Milestone: sage-duplicate/invalid/wontfix
Component: symbolics Keywords:
Cc: fredrik.johansson, kcrisman, zaf, mjo Merged in:
Authors: Reviewers: Burcin Erocal
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

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)
134078079299425970995740249982058461274793658205923933777\
235614437217640300735469768018742981669034276900318581864\
86050853753882811946569946433649006084097
sage: int(fermat(9))
134078079299425970995740249982058461274793658205923933777\
235614437217640300735469768018742981669034276900318581864\
86050853753882811946569946433649006084096L
sage: long(fermat(9))
134078079299425970995740249982058461274793658205923933777\
235614437217640300735469768018742981669034276900318581864\
86050853753882811946569946433649006084096L
sage: int(2**2**9 +1)
134078079299425970995740249982058461274793658205923933777\
235614437217640300735469768018742981669034276900318581864\
86050853753882811946569946433649006084097L

Attachments (1)

9953.patch (819 bytes) - added by roed 10 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 11 years ago by kcrisman

  • Cc kcrisman added

Changed 10 years ago by roed

comment:2 Changed 10 years ago by roed

  • Status changed from new to needs_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 10 years ago by dsm

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)
0
sage: int(SR(f))
1

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 10 years ago by dsm

This appears to be a duplicate of #9627.

comment:5 Changed 10 years ago by zaf

  • Cc zaf added

comment:6 Changed 9 years ago by mjo

  • Cc mjo added

comment:7 Changed 9 years ago by burcin

  • Milestone changed from sage-5.1 to sage-duplicate/invalid/wontfix
  • Reviewers set to Burcin Erocal
  • Status changed from needs_work to positive_review

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

comment:8 Changed 9 years ago by jdemeyer

  • Resolution set to duplicate
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.