A workaround and general principle: if at all possible, avoid using the 
"symbolic ring", particularly if you're interested in numerical 
approximations anyway. Having all the overhead of SR involved will show 
down your computation considerably -- and, as you can see, can lead to 
unstable numerical schemes.

In your case, the symbolic ring gets involved by mentioning "pi". You could 
do instead

RR = RealField(100) #by default this is 53, which is fine too
(-12.0*RR.pii()).exp() #sage has "float literals" which deals with the 
precision ambiguity in this notation

A little more mathematical-looking notation:

exp(-12.0 * RR.pi() )
exp(-12.0 * RR(pi)) # this still involves SR, but only minimally

Evaluating this expression should of course just work with pretty much any 
precision: exp is very well approximated. Rewriting it as a difference of 
hyperbolic functions is just obviously a bad idea. The reason why the SR 
takes such bad decisions may be deeper and more difficult to change. So I 
think it's a bug, but it may not be easily fixable. So: stick with the 
workaround (which will always be more efficient)

On Sunday, 4 July 2021 at 23:04:44 UTC-7 Brian Lawrence wrote:

>
> Hi,
>
> I'm new to Sage, and I'm running across some unexpected behavior involving 
> numerical evaluation of exponentials involving pi.
>
> In my version (Sagemath 9.2), the following code
> exp(-12.0*pi).n()
> gives the output below.
> 0.000000000000000
>
> The true value of the exponential is something like 4e-17: very small, but 
> definitely not zero.  The problem seems to be that Sage internally converts 
> the exponential to a difference (cosh-sinh) of two huge numbers, and then 
> precision problems kick in.
>
> This problem has been brought up before...
> https://ask.sagemath.org/question/57182/numerical-
> approximation-error-involving-cosh-and-sinh/
> ... but the folks over there seem to have concluded (for reasons I don't 
> understand) that the behavior is not a bug at all.
>
> Brian Lawrence
>
> 'SageMath version 9.2, Release Date: 2020-10-24'
>
> (P.S. I ran across this trying to do some computations with q-expansions 
> of modular forms, where these sorts of exponentials are quite common.)
>
> (P.P.S. Apologies if I'm posting this in the wrong place, I'm new here and 
> not familiar with local norms...)
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/aafa3344-60f6-4e0a-bfcd-59074ce15844n%40googlegroups.com.

Reply via email to