Linear.Quaternion:$ctan from linear-1.19.1.3

Percentage Accurate: 84.6% → 95.8%
Time: 3.3s
Alternatives: 4
Speedup: 1.3×

Specification

?
\[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* (cosh x) (/ y x)) z))
double code(double x, double y, double z) {
	return (cosh(x) * (y / x)) / z;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (cosh(x) * (y / x)) / z
end function
public static double code(double x, double y, double z) {
	return (Math.cosh(x) * (y / x)) / z;
}
def code(x, y, z):
	return (math.cosh(x) * (y / x)) / z
function code(x, y, z)
	return Float64(Float64(cosh(x) * Float64(y / x)) / z)
end
function tmp = code(x, y, z)
	tmp = (cosh(x) * (y / x)) / z;
end
code[x_, y_, z_] := N[(N[(N[Cosh[x], $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	((((1) / (2)) * ((exp(x)) + ((1) / (exp(x))))) * (y / x)) / z
END code
\frac{\cosh x \cdot \frac{y}{x}}{z}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 84.6% accurate, 1.0× speedup?

\[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* (cosh x) (/ y x)) z))
double code(double x, double y, double z) {
	return (cosh(x) * (y / x)) / z;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (cosh(x) * (y / x)) / z
end function
public static double code(double x, double y, double z) {
	return (Math.cosh(x) * (y / x)) / z;
}
def code(x, y, z):
	return (math.cosh(x) * (y / x)) / z
function code(x, y, z)
	return Float64(Float64(cosh(x) * Float64(y / x)) / z)
end
function tmp = code(x, y, z)
	tmp = (cosh(x) * (y / x)) / z;
end
code[x_, y_, z_] := N[(N[(N[Cosh[x], $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	((((1) / (2)) * ((exp(x)) + ((1) / (exp(x))))) * (y / x)) / z
END code
\frac{\cosh x \cdot \frac{y}{x}}{z}

Alternative 1: 95.8% accurate, 0.7× speedup?

\[\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 0.2322009367506751:\\ \;\;\;\;\frac{y \cdot \cosh \left(\left|x\right|\right)}{z \cdot \left|x\right|}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{0 \cdot z}\\ \end{array} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (*
 (copysign 1.0 x)
 (if (<= (fabs x) 0.2322009367506751)
   (/ (* y (cosh (fabs x))) (* z (fabs x)))
   (/ y (* 0.0 z)))))
double code(double x, double y, double z) {
	double tmp;
	if (fabs(x) <= 0.2322009367506751) {
		tmp = (y * cosh(fabs(x))) / (z * fabs(x));
	} else {
		tmp = y / (0.0 * z);
	}
	return copysign(1.0, x) * tmp;
}
public static double code(double x, double y, double z) {
	double tmp;
	if (Math.abs(x) <= 0.2322009367506751) {
		tmp = (y * Math.cosh(Math.abs(x))) / (z * Math.abs(x));
	} else {
		tmp = y / (0.0 * z);
	}
	return Math.copySign(1.0, x) * tmp;
}
def code(x, y, z):
	tmp = 0
	if math.fabs(x) <= 0.2322009367506751:
		tmp = (y * math.cosh(math.fabs(x))) / (z * math.fabs(x))
	else:
		tmp = y / (0.0 * z)
	return math.copysign(1.0, x) * tmp
function code(x, y, z)
	tmp = 0.0
	if (abs(x) <= 0.2322009367506751)
		tmp = Float64(Float64(y * cosh(abs(x))) / Float64(z * abs(x)));
	else
		tmp = Float64(y / Float64(0.0 * z));
	end
	return Float64(copysign(1.0, x) * tmp)
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (abs(x) <= 0.2322009367506751)
		tmp = (y * cosh(abs(x))) / (z * abs(x));
	else
		tmp = y / (0.0 * z);
	end
	tmp_2 = (sign(x) * abs(1.0)) * tmp;
end
code[x_, y_, z_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 0.2322009367506751], N[(N[(y * N[Cosh[N[Abs[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(z * N[Abs[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y / N[(0.0 * z), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.2322009367506751:\\
\;\;\;\;\frac{y \cdot \cosh \left(\left|x\right|\right)}{z \cdot \left|x\right|}\\

\mathbf{else}:\\
\;\;\;\;\frac{y}{0 \cdot z}\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.2322009367506751

    1. Initial program 84.6%

      \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
    2. Step-by-step derivation
      1. Applied rewrites83.8%

        \[\leadsto \frac{y \cdot \cosh x}{z \cdot x} \]

      if 0.2322009367506751 < x

      1. Initial program 84.6%

        \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{y}{x \cdot z} \]
      3. Step-by-step derivation
        1. Applied rewrites49.3%

          \[\leadsto \frac{y}{x \cdot z} \]
        2. Taylor expanded in undef-var around zero

          \[\leadsto \frac{y}{0 \cdot z} \]
        3. Step-by-step derivation
          1. Applied rewrites32.7%

            \[\leadsto \frac{y}{0 \cdot z} \]
        4. Recombined 2 regimes into one program.
        5. Add Preprocessing

        Alternative 2: 95.6% accurate, 1.3× speedup?

        \[\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 0.2322009367506751:\\ \;\;\;\;\frac{y}{\left|x\right| \cdot z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{0 \cdot z}\\ \end{array} \]
        (FPCore (x y z)
          :precision binary64
          :pre TRUE
          (*
         (copysign 1.0 x)
         (if (<= (fabs x) 0.2322009367506751)
           (/ y (* (fabs x) z))
           (/ y (* 0.0 z)))))
        double code(double x, double y, double z) {
        	double tmp;
        	if (fabs(x) <= 0.2322009367506751) {
        		tmp = y / (fabs(x) * z);
        	} else {
        		tmp = y / (0.0 * z);
        	}
        	return copysign(1.0, x) * tmp;
        }
        
        public static double code(double x, double y, double z) {
        	double tmp;
        	if (Math.abs(x) <= 0.2322009367506751) {
        		tmp = y / (Math.abs(x) * z);
        	} else {
        		tmp = y / (0.0 * z);
        	}
        	return Math.copySign(1.0, x) * tmp;
        }
        
        def code(x, y, z):
        	tmp = 0
        	if math.fabs(x) <= 0.2322009367506751:
        		tmp = y / (math.fabs(x) * z)
        	else:
        		tmp = y / (0.0 * z)
        	return math.copysign(1.0, x) * tmp
        
        function code(x, y, z)
        	tmp = 0.0
        	if (abs(x) <= 0.2322009367506751)
        		tmp = Float64(y / Float64(abs(x) * z));
        	else
        		tmp = Float64(y / Float64(0.0 * z));
        	end
        	return Float64(copysign(1.0, x) * tmp)
        end
        
        function tmp_2 = code(x, y, z)
        	tmp = 0.0;
        	if (abs(x) <= 0.2322009367506751)
        		tmp = y / (abs(x) * z);
        	else
        		tmp = y / (0.0 * z);
        	end
        	tmp_2 = (sign(x) * abs(1.0)) * tmp;
        end
        
        code[x_, y_, z_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 0.2322009367506751], N[(y / N[(N[Abs[x], $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision], N[(y / N[(0.0 * z), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
        
        \mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
        \mathbf{if}\;\left|x\right| \leq 0.2322009367506751:\\
        \;\;\;\;\frac{y}{\left|x\right| \cdot z}\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{y}{0 \cdot z}\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if x < 0.2322009367506751

          1. Initial program 84.6%

            \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
          2. Taylor expanded in x around 0

            \[\leadsto \frac{y}{x \cdot z} \]
          3. Step-by-step derivation
            1. Applied rewrites49.3%

              \[\leadsto \frac{y}{x \cdot z} \]

            if 0.2322009367506751 < x

            1. Initial program 84.6%

              \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
            2. Taylor expanded in x around 0

              \[\leadsto \frac{y}{x \cdot z} \]
            3. Step-by-step derivation
              1. Applied rewrites49.3%

                \[\leadsto \frac{y}{x \cdot z} \]
              2. Taylor expanded in undef-var around zero

                \[\leadsto \frac{y}{0 \cdot z} \]
              3. Step-by-step derivation
                1. Applied rewrites32.7%

                  \[\leadsto \frac{y}{0 \cdot z} \]
              4. Recombined 2 regimes into one program.
              5. Add Preprocessing

              Alternative 3: 95.5% accurate, 0.5× speedup?

              \[\begin{array}{l} \mathbf{if}\;\cosh x \cdot \frac{y}{x} \leq \infty:\\ \;\;\;\;\frac{y \cdot \frac{\cosh x}{x}}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y \cdot \frac{\cosh x}{z}}{x}\\ \end{array} \]
              (FPCore (x y z)
                :precision binary64
                :pre TRUE
                (if (<= (* (cosh x) (/ y x)) INFINITY)
                (/ (* y (/ (cosh x) x)) z)
                (/ (* y (/ (cosh x) z)) x)))
              double code(double x, double y, double z) {
              	double tmp;
              	if ((cosh(x) * (y / x)) <= ((double) INFINITY)) {
              		tmp = (y * (cosh(x) / x)) / z;
              	} else {
              		tmp = (y * (cosh(x) / z)) / x;
              	}
              	return tmp;
              }
              
              public static double code(double x, double y, double z) {
              	double tmp;
              	if ((Math.cosh(x) * (y / x)) <= Double.POSITIVE_INFINITY) {
              		tmp = (y * (Math.cosh(x) / x)) / z;
              	} else {
              		tmp = (y * (Math.cosh(x) / z)) / x;
              	}
              	return tmp;
              }
              
              def code(x, y, z):
              	tmp = 0
              	if (math.cosh(x) * (y / x)) <= math.inf:
              		tmp = (y * (math.cosh(x) / x)) / z
              	else:
              		tmp = (y * (math.cosh(x) / z)) / x
              	return tmp
              
              function code(x, y, z)
              	tmp = 0.0
              	if (Float64(cosh(x) * Float64(y / x)) <= Inf)
              		tmp = Float64(Float64(y * Float64(cosh(x) / x)) / z);
              	else
              		tmp = Float64(Float64(y * Float64(cosh(x) / z)) / x);
              	end
              	return tmp
              end
              
              function tmp_2 = code(x, y, z)
              	tmp = 0.0;
              	if ((cosh(x) * (y / x)) <= Inf)
              		tmp = (y * (cosh(x) / x)) / z;
              	else
              		tmp = (y * (cosh(x) / z)) / x;
              	end
              	tmp_2 = tmp;
              end
              
              code[x_, y_, z_] := If[LessEqual[N[(N[Cosh[x], $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision], Infinity], N[(N[(y * N[(N[Cosh[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision], N[(N[(y * N[(N[Cosh[x], $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
              
              \begin{array}{l}
              \mathbf{if}\;\cosh x \cdot \frac{y}{x} \leq \infty:\\
              \;\;\;\;\frac{y \cdot \frac{\cosh x}{x}}{z}\\
              
              \mathbf{else}:\\
              \;\;\;\;\frac{y \cdot \frac{\cosh x}{z}}{x}\\
              
              
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if (*.f64 (cosh.f64 x) (/.f64 y x)) < +inf.0

                1. Initial program 84.6%

                  \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
                2. Step-by-step derivation
                  1. Applied rewrites95.6%

                    \[\leadsto \frac{y \cdot \frac{\cosh x}{x}}{z} \]

                  if +inf.0 < (*.f64 (cosh.f64 x) (/.f64 y x))

                  1. Initial program 84.6%

                    \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
                  2. Step-by-step derivation
                    1. Applied rewrites95.8%

                      \[\leadsto \frac{y \cdot \frac{\cosh x}{z}}{x} \]
                  3. Recombined 2 regimes into one program.
                  4. Add Preprocessing

                  Alternative 4: 49.3% accurate, 3.2× speedup?

                  \[\frac{y}{x \cdot z} \]
                  (FPCore (x y z)
                    :precision binary64
                    :pre TRUE
                    (/ y (* x z)))
                  double code(double x, double y, double z) {
                  	return y / (x * z);
                  }
                  
                  real(8) function code(x, y, z)
                  use fmin_fmax_functions
                      real(8), intent (in) :: x
                      real(8), intent (in) :: y
                      real(8), intent (in) :: z
                      code = y / (x * z)
                  end function
                  
                  public static double code(double x, double y, double z) {
                  	return y / (x * z);
                  }
                  
                  def code(x, y, z):
                  	return y / (x * z)
                  
                  function code(x, y, z)
                  	return Float64(y / Float64(x * z))
                  end
                  
                  function tmp = code(x, y, z)
                  	tmp = y / (x * z);
                  end
                  
                  code[x_, y_, z_] := N[(y / N[(x * z), $MachinePrecision]), $MachinePrecision]
                  
                  f(x, y, z):
                  	x in [-inf, +inf],
                  	y in [-inf, +inf],
                  	z in [-inf, +inf]
                  code: THEORY
                  BEGIN
                  f(x, y, z: real): real =
                  	y / (x * z)
                  END code
                  \frac{y}{x \cdot z}
                  
                  Derivation
                  1. Initial program 84.6%

                    \[\frac{\cosh x \cdot \frac{y}{x}}{z} \]
                  2. Taylor expanded in x around 0

                    \[\leadsto \frac{y}{x \cdot z} \]
                  3. Step-by-step derivation
                    1. Applied rewrites49.3%

                      \[\leadsto \frac{y}{x \cdot z} \]
                    2. Add Preprocessing

                    Reproduce

                    ?
                    herbie shell --seed 2026092 
                    (FPCore (x y z)
                      :name "Linear.Quaternion:$ctan from linear-1.19.1.3"
                      :precision binary64
                      (/ (* (cosh x) (/ y x)) z))