possible bug: numerical approx exp, pi, sinh, cosh...

43 views
Skip to first unread message

Brian Lawrence

unread,
Jul 5, 2021, 2:04:44 AM7/5/21
to sage-devel

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...
... 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...)

Frédéric Chapoton

unread,
Jul 5, 2021, 2:29:38 AM7/5/21
to sage-devel
Well, it works with the real ball field or with more precision :

R = RBF
a = exp(-12*pi)
R(a),a.n(100)
([4.2411511830161e-17 +/- 2.87e-31], 4.2411511830160775440174644060e-17)

Another place to ask questions is https://ask.sagemath.org/

Nils Bruin

unread,
Jul 5, 2021, 12:12:28 PM7/5/21
to sage-devel
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)

Brian Lawrence

unread,
Jul 5, 2021, 1:35:18 PM7/5/21
to sage-...@googlegroups.com
Thanks!  This is very helpful.

--
You received this message because you are subscribed to a topic in the Google Groups "sage-devel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-devel/Ev9dSQNr4ig/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-devel+...@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 all
Reply to author
Forward
0 new messages