# Why doesn't Mathematica know the exact value of some trivial trigonometric functions?

The trigonometric functions for half angles have well-defined and simple rules. For example $$\tan\left(\frac x2\right)=\frac{\sin x}{1+\cos x}=\frac1{\csc x+\cot x}$$ But I can't get the exact value for some expressions as simple as $$\tan(\pi/8)$$. No matter what I tried, Mathematica wouldn't understand that it's equal to $$\sqrt2-1$$, for example:

Simplify[Tan[Pi/8] + 1 - Sqrt[2]]


does nothing. I tried TrigReduce, TrigExpand, TrigFactor, FullSimplify and some other unrelated functions and they all failed to give the exact value of $$\tan(\pi/8)$$. This became ironic when I checked some other angles such as $$\pi/12$$ and turned out it does know e.g. $$\sin(\pi/12)$$ whose only way of calculation is apparently, through half angle formulas.

Why this happens and how can I get Mathematica to calculate such values when they do have an exact expression?

• Of course, the question is a bit flawed because Tan[Pi/8] is an exact numerical constant. Oct 29, 2021 at 15:36
• @JohnDoty please feel free to edit it if you can think of better words. My vocabulary is not that great. Oct 29, 2021 at 15:38
• Well, what is it that you want, really? What problem are you trying to solve? Oct 29, 2021 at 15:40
• Tan[Pi/8] // N // RootApproximant Oct 29, 2021 at 16:25
• With version 12.3.1 for Microsoft Windows (64-bit) (June 19, 2021), FullSimplify[Tan[Pi/8] + 1 - Sqrt[2]] yields 0. Oct 30, 2021 at 13:26

## 3 Answers

An answer to How does Mathematica calculate $\sin(\pi/5)$? by @J.M. points out that in the Notes on Internal Implementation, it is implied that FunctionExpand is the way to expand trigonometric functions in terms of radicals when possible:

FunctionExpand uses an extension of Gauss's algorithm to expand trigonometric functions with arguments that are rational multiples of π.

FunctionExpand[Tan[Pi/8]]
(*  Sqrt[(2 - Sqrt[2])/(2 + Sqrt[2])]  *)


The internal function that does what the OP wants is called SimplifyTrigToRealRadicals[], which is called by FunctionExpand and ToRadicals in this case. It in turn calls a function like SystemTrigToRadicalsDumptan[], which computes the conversion. These function memoize their results, which makes timing them a challenge. However, the timings below show that FunctionExpand does a lot of unnecessary work. We give a complicated example. At first, FunctionExpand does nothing, and ToRadicals returns the simplified expansion in terms of the complex exponential function.

FunctionExpand[Cos[π/257]]
(* Cos[π/257]  <-- takes <0.02s *)

ToRadicals[Cos[π/257]]
(*  -(1/2) (-1)^(256/257) (1 + (-1)^(2/257))  <-- takes 0.004s *)


If the trig-to-radicals functionality has been loaded, then we get a different behavior. I cannot say for certain what functions cause the loading of the package. The first time I tested, it had been loaded and I got results like those below. The second, I retested with a fresh kernel and got the above, which confused me for a while.

SimplifyTrigToRealRadicals; (* load/initialize *)
SystemTrigToRadicalsDumpcos[π/257]; // AbsoluteTiming
(*  {146.975, Null}  <-- the expansion is now memoized *)

ToRadicals[Cos[π/257]] // LeafCount
(*  263365  <-- in terms of (real) radicals; takes only 0.06s *)


Here's a speed comparison, assuming the initialization of SystemTrigToRadicalsDumpcos[π/257] has been done.

SimplifyTrigToRealRadicals[Cos[π/257]]; // RepeatedTiming
ToRadicals[Cos[π/257]]; // RepeatedTiming
RootReduce[Cos[π/257]]; // RepeatedTiming
FunctionExpand[Cos[π/257]]; // AbsoluteTiming
(*
{ 0.0579771, Null}
{ 0.0593911, Null}
{ 1.28168,   Null}
{37.3825,    Null}
*)

• The formatting of third paragraph makes it hard to read/understand. By the way, is there anything special about number 257 or is it chosen randomly? Oct 30, 2021 at 19:28
• @polfosol n=257 is a prime such that a regular n-gon can be constructed by ruler and compass. That comes down to writing Cos[Pi/257] in terms of square roots, more or less. The example is borrowed from the linked question. Such primes have to be Fermat primes, which have the form 2^(2^k)+1. (And thanks for alerting me to a missing backtick.) Oct 30, 2021 at 20:26
TrigToExp[Tan[Pi/8]] // FullSimplify
-1 + Sqrt[2]


Or, even better:

Tan[Pi/8] // RootReduce
-1 + Sqrt[2]

• Who would have thought, of all the functions, RootReduce would do the job? a strange world we live in... Oct 29, 2021 at 15:36
• @polfosol You apparently wanted an explicit algebraic number, so RootReduce made perfect sense to me. Oct 29, 2021 at 15:38

Try to use ToRadicals[Tan[Pi/8]]//FullSimplify`