This is the mail archive of the
`gcc-patches@gcc.gnu.org`
mailing list for the GCC project.

# Re: [PATCH] fold tan(x)/sin(x) as 1.0/cos(x)

*From*: Roger Sayle <roger at eyesopen dot com>
*To*: gcc-patches at gcc dot gnu dot org
*Date*: Fri, 12 Mar 2004 06:24:53 -0700 (MST)
*Subject*: Re: [PATCH] fold tan(x)/sin(x) as 1.0/cos(x)

On Fri, 12 Mar 2004, Uros Bizjak wrote:
> 2004-03-12 Uros Bizjak <uros@kss-loka.si>
>
> * fold-const.c (fold <RDIV_EXPR>): With
> flag_unsafe_math_optimizations fold tan(x)/sin(x) as 1.0/cos(x).
>
Hi Uros,
This transformation tan(x)/sin(x) -> 1.0/cos(x), and the related one,
sin(x)/tan(x) -> cos(x), were proposed in the following patch:
http://gcc.gnu.org/ml/gcc-patches/2003-06/msg00715.html
However, as pointed out by Brad Lucier during the review, these
transformations may return the wrong result when x == 0.0.
For example, with your transformation tan(0.0)/sin(0.0) = 0.0/0.0 = NaN
which isn't the same as 1.0/cos(0.0) which is 1.0/1.0 which is 1.0.
In addition to the wrong answer the first may generate FP exceptions
on some platforms, which won't occur in the transformed code.
The conclussion, at that time, was that these transformations were just
too unsafe for GCC even with -ffast-math. However, its difficult to
define precisely where the "safe enough" borderline lies, so I'm happy
to listen to arguments from both sides, but my current feeling is that
that this transformation is indeed a too unsafe, and should be performed
by the programmer if he/she isn't concerned about the signularity at
zero.
Roger
--