#9627: converting from symbolic ring to int is broken,
-------------------------+--------------------------------------------------
Reporter: syazdani | Owner: robertwb
Type: defect | Status: needs_work
Priority: major | Milestone:
Component: symbolics | Keywords:
Author: | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
-------------------------+--------------------------------------------------
Comment(by tornaria):
Something like this may work:
{{{
def SR_int(x):
"""
sage: SR_int(sin(2)*100)
90
sage: SR_int(-sin(2)*100)
-90
sage: SR_int(log(8)/log(2))
3
sage: SR_int(-log(8)/log(2))
-3
sage: SR_int(SR(3^64)) == 3^64
True
sage: SR_int(SR(10^100)) == 10^100
True
sage: SR_int(SR(10^100+10^-100)) == 10^100
True
sage: SR_int(SR(10^100-10^-100)) == 10^100 - 1
True
sage: SR_int(sqrt(-1))
...
ValueError: math domain error
sage: SR_int(x)
...
ValueError: math domain error
"""
try:
if x in RR:
## truncate toward zero
y = floor(abs(x))
if parent(y) is ZZ:
return int(ZZ(sign(x) * y))
except:
pass
raise ValueError, "math domain error"
}}}
Note that {{{floor(log(8)/log(2))}}} takes about 1s to complete, which
means {{{SR_int(log(8)/log(2))}}} is waaay slower than
{{{int(log(8)/log(2)}}}. But this is at the cost of {{{int(x)}}} being
incorrect when {{{x}}} is very close to an integer (cf.
{{{int(SR(10^20-10^-20))}}}).
OTOH, {{{(log(8)/log(2)).full_simplify()}}} takes 35ms to give {{{3}}}, so
it may be worth revisiting the {{{floor()}}} strategy. Opens a can of
worms, I guess...
----
I won't turn the snippet above into a patch, if somebody likes it and
wants to produce a patch, go ahead.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/9627#comment:4>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.