Numerical approximation error involving cosh and sinh
Hi. I'm new to Sage and to computational mathematics in general. I have been trying to code something, but I am struggling to understand some numerical approximation errors. My problem seems to boil down to the following phenomenon:
sage: x = exp(12*pi*i*i)
sage: x.n()
0.000000000000000
Which is as expected, $x=e^{-12\pi }$ is "very close" to 0. However, I do not understand why Sage gives me
sage: (5+x).n()
4.00000000000000
If I give it more bits of precision, then it works:
sage: (5+x).n(200)
5.0000000000000000424115118301607754401746440550887456940681
But I can't understand why it is making such grotesque error. Sage knows $x$ is very small (it returned 0.000000000000000). So why did it approximate $5+x$ to 4?
What is also weird, is that if I repeat the same computation with 4+x or 6+x instead, then it works just fine:
sage: (6+x).n()
6.00000000000000
My apologies if this is something trivial in computational maths, but can someone clarify what is going on here?
In a situation like this, is there a way for me to know if I have given Sage enough bits of precision to get at least the first few digits correct?
(Remark: I wrote exp(12*pi*i*i)
instead of exp(-12*pi)
to make Sage use sinh and cosh.)
Welcome to Ask Sage! Thank you for your question.
Congratulations! You found a bug!
Thanks for the report. This reminds me of
although it seems quite distinct.
So this is a bug? I was trying to do some computations with modular forms, but was getting some weird errors when evaluating them at some specific points of the upper halfplane. After a long time trying to understand what was going on, it boiled down to this issue here. So, should I open a report ticket? Or does this post already counts as one? Many thanks!!
If we accept that
exp(12*pi*i*i)
is transformed intocosh(12*pi) - sinh(12*pi)
, then the observed behaviour is not a bug.Indeed, evaluating
cosh(12*pi) - sinh(12*pi)
is done by evaluatingpi
to some precision, then computing12*pi
, thencosh(12*pi)
andsinh(12*pi)
, then subtracting, and the loss of precision is such that no information on the value of the expression is left.However it could be argued that transforming
exp(12*pi*i*i)
intocosh(12*pi) - sinh(12*pi)
is in itself a bug, as it can lead to the loss of information observed in the question.