I'm interested in using a package that provides arbitrary calculation within LaTeX. I need mathematical functions such as exp(), sin(), cos(). Does such a package exist? If not, which package provides the highest precision calculation that contains such functions?
2 Answers
The package apnum implements arbitrary precision of numeric calculation at TeX macro level. From version 1.4 of apnum (December 2015), the functions sqrt, exp, ln, sin, cos, tan, asin, acos, atan are implemented too. The number of decimal digits after decimal point in the result is given by "\apFRAC" register. But TeX is interpreter only, you cannot except the same speed as with compiled programs (like UNIX bc, for example). The \PI to 100 digits takes 0.18 sec when apnum is used but the same \PI to 500 digits takes 4.1 sec.
Examle:
\input apnum
\def\shownum#1 = #2{\evaldef\OUT{#2}\message{#1 = \OUT}}
\apFRAC=50
\shownum sin(pi/4) = {\SIN{\PI/4}}
\shownum sqrt(2)/2 = {\SQRT{2}/2}
\shownum sin(pi/4) - sqrt(2)/2 = {\SIN{\PI/4} - \SQRT{2}/2}
\shownum pi = \PI
\shownum atan(1) = {\ATAN{1}}
\shownum _pi / 4 = {\PI/4}
\shownum sqrt(17) = {\SQRT{17}}
\shownum e = {\EXP{1}}
\shownum ln(e) = {\LN{\EXP{1}}}
\bye
And the result (on the terminal and in the log file):
The Arbitrary Precision Numbers, 1.4 <Dec 2015>)
sin(pi/4) = .70710678118654752440084436210484903928483593768847
sqrt(2)/2 = .70710678118654752440084436210484903928483593768847
sin(pi/4) - sqrt(2)/2 = 0
pi = 3.14159265358979323846264338327950288419716939937510
atan(1) = .78539816339744830961566084581987572104929234984377
_pi / 4 = .78539816339744830961566084581987572104929234984377
sqrt(17) = 4.12310562561766054982140985597407702514719922537362
e = 2.71828182845904523536028747135266249775724709369995
ln(e) = .99999999999999999999999999999999999999999999999999 )
Note. I modified your title (arbitrary precision within LaTeX -> within TeX) because apnum needs only classical TeX, nothing more. But it works in LaTeX too.
- 74,238
As @Joseph Wright says, LaTeX is not a computer algebra system. But if you're okay with using LaTeX (as opposed to TEX), there is the sagetex package which lets you use the CAS Sage in your LaTeX documents. Arbitrary precision in Sage is covered here.
\documentclass{article}
\usepackage{fullpage}%smaller margins for more output
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
RealField(80)
\end{sagesilent}
\noindent Using a precision of $80$:\\
$\pi \approx \sage{pi.n(digits=80)}$\\
$\sin\left(\frac{\pi}{4}\right) \approx \sage{sin(pi/4).n(digits=80)}$\\
$e^{-1} \approx \sage{exp(-1).n(digits=80)}$
\end{document}
The output is:
So sagetex lets you easily use calculations within your LaTeX document in Python/Sage code that's not difficult to understand: use \sage{} to put the calculations into your document. Documentation on trig functions is here; logarithms/exponentials are here. Of course, a CAS gives you even more complicated functions, too. You need to either get Sage on your computer or, even easier, open a free SageMath Cloud account. In a couple of minutes you can get an account, copy/paste the code above and be up and running.
- 12,451
-
Question is about LaTeX: I've rolled-back to make this clear (though I'm 99% certain this is a dupe). – Joseph Wright Dec 26 '15 at 18:37
l3fp(part ofexpl3) provides expandable calculations with 16 digits of precision, including some mathematical functions inclusive ofexp,log,cos,sin. – Nov 28 '15 at 12:47+, -, *, /and powers with integral exponents), packageapnumprovides (non-expandable) macros for arbitrary precision, andxint(expandable) also.xintalso has a square root macro, which can be used with a user chosen arbitrary precision. – Nov 28 '15 at 12:50TikZ/pgfimplements with limited precision the basic mathematical functions, and has a floating point library for more. It can also interface with packagefpwhich does fixed point computations. It has apgfmathlibrary with useful syntax. Experts, please add more info and I will suppress this approximate comment. – Nov 28 '15 at 12:53