2

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?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Mike Renfro Nov 28 '15 at 05:13
  • 2
    LaTeX is a typesetting system not a computer alegbra one. There are several questions already focussed on floating point or arbitrary integer calculations. One could do arbitrary real number work but an external tool is going to be a lot easier. – Joseph Wright Nov 28 '15 at 08:45
  • l3fp (part of expl3) provides expandable calculations with 16 digits of precision, including some mathematical functions inclusive of exp, log, cos, sin. –  Nov 28 '15 at 12:47
  • 1
    regarding basic arithmetic (+, -, *, / and powers with integral exponents), package apnum provides (non-expandable) macros for arbitrary precision, and xint (expandable) also. xint also has a square root macro, which can be used with a user chosen arbitrary precision. –  Nov 28 '15 at 12:50
  • TikZ/pgf implements with limited precision the basic mathematical functions, and has a floating point library for more. It can also interface with package fp which does fixed point computations. It has a pgfmath library with useful syntax. Experts, please add more info and I will suppress this approximate comment. –  Nov 28 '15 at 12:53
  • 1
    Are you willing to consider double-precision algebraic capabilities, or are you truly after arbitrary precision, e.g., hundreds or even thousands (or millions, billions, ...) digits of precision? – Mico Dec 26 '15 at 18:43

2 Answers2

2

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.

wipet
  • 74,238
1

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: enter image description here 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.

DJP
  • 12,451