In the comments you have proposed using a library for symbolic calculations, which I think is a good solution to this problem.
To this end, you have to download the library you linked and its dependencies. Because they are hosted on GitHub I download them using Git.
git clone https://github.com/thenumbernine/symmath-lua symmath
git clone https://github.com/thenumbernine/lua-ext ext
Then my document directory looks something like this:
.
├── ext
│ [...]
│ ├── ext.lua
│ [...]
├── symmath
│ [...]
│ ├── symmath.lua
│ [...]
└── test.tex
Then the MWE below can be used. There I have to work around three distinct bugs.
symmath aggressively tries to use FFI to speed up complex number operations. FFI is always available in LuaTeX but only usable when shell escape is enabled, so I remove FFI when shell escape is not enabled.
The Lua package loader that is installed by lualibs has some kind of bug which does not allow two questions marks in the package.path. I'm not sure what is causing this, so I've reported it here: https://github.com/latex3/lualibs/issues/2
Instead I just use package.searchpath and pass the modified path by hand without modifying package.path.
symmath expects Constant to be in the global scope which is not the default.
\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
-- Disable FFI without shell escape
if status.shell_escape ~= 1 then
package.loaded.ffi = nil
end
-- Work around loader bug
local file, err = package.searchpath("symmath", package.path .. ";./?/?.lua")
if not err then
package.preload["symmath"] = loadfile(file)
end
-- Load symmath
sym = require("symmath")
Constant = sym.Constant -- bug in symmath
\end{luacode*}
\begin{document}
\[ \sin\pi = \directlua{tex.sprint(tostring(sym.eval(sym.sin(sym.pi))))} \]
\[ \tan\pi/2 = \directlua{tex.sprint(tostring(sym.eval(sym.tan(sym.pi / 2))))} \]
\end{document}

Update 2022-04-12
Since this answer was written a lot has happened and the loader bug was fixed. Also symmath has seen ongoing development and is actively maintained, so it seems it is a very good choice for symbolic math in Lua.
The directory structure for the document remains the same as shown above. But instead of the awkward manual loading, it is now possible to use the luapackageloader package in conjunction with appending to package.path.
To not pollute the global environment with symbols, symmath offers a setup function which takes an environment table to insert its functions into.
The only drawback is that the ffi still has to be disabled manually when not using --shell-escape. I think a bug report to the developers is in order once I find a simple reproducer.
\documentclass{article}
\usepackage{luacode}
\usepackage{luapackageloader}
\begin{luacode*}
-- Disable FFI without shell escape
if status.shell_escape ~= 1 then
package.loaded.ffi = nil
end
-- Load symmath
package.path = package.path .. ";./?/?.lua"
sym = {}
require("symmath").setup{env=sym}
\end{luacode*}
\begin{document}
[ \sin\pi = \directlua{tex.sprint(tostring(sym.eval(sym.sin(sym.pi))))} ]
[ \tan\pi/2 = \directlua{tex.sprint(tostring(sym.eval(sym.tan(sym.pi / 2))))} ]
\end{document}
tex.sprintwith a number. Always usetex.sprint(tostring(<number>))unless you know what you are doing. – Henri Menke Apr 16 '20 at 07:54lualibsthat prevents that currently: https://github.com/latex3/lualibs/issues/2 – Henri Menke Apr 17 '20 at 00:14