(/ z0 (* (tan z1) z2))

Percentage Accurate: 95.8% → 99.6%
Time: 52.0s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\frac{z0}{\tan z1 \cdot z2} \]
(FPCore (z0 z1 z2)
  :precision binary64
  (/ z0 (* (tan z1) z2)))
double code(double z0, double z1, double z2) {
	return z0 / (tan(z1) * z2);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(z0, z1, z2)
use fmin_fmax_functions
    real(8), intent (in) :: z0
    real(8), intent (in) :: z1
    real(8), intent (in) :: z2
    code = z0 / (tan(z1) * z2)
end function
public static double code(double z0, double z1, double z2) {
	return z0 / (Math.tan(z1) * z2);
}
def code(z0, z1, z2):
	return z0 / (math.tan(z1) * z2)
function code(z0, z1, z2)
	return Float64(z0 / Float64(tan(z1) * z2))
end
function tmp = code(z0, z1, z2)
	tmp = z0 / (tan(z1) * z2);
end
code[z0_, z1_, z2_] := N[(z0 / N[(N[Tan[z1], $MachinePrecision] * z2), $MachinePrecision]), $MachinePrecision]
\frac{z0}{\tan z1 \cdot z2}

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 3 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: 95.8% accurate, 1.0× speedup?

\[\frac{z0}{\tan z1 \cdot z2} \]
(FPCore (z0 z1 z2)
  :precision binary64
  (/ z0 (* (tan z1) z2)))
double code(double z0, double z1, double z2) {
	return z0 / (tan(z1) * z2);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(z0, z1, z2)
use fmin_fmax_functions
    real(8), intent (in) :: z0
    real(8), intent (in) :: z1
    real(8), intent (in) :: z2
    code = z0 / (tan(z1) * z2)
end function
public static double code(double z0, double z1, double z2) {
	return z0 / (Math.tan(z1) * z2);
}
def code(z0, z1, z2):
	return z0 / (math.tan(z1) * z2)
function code(z0, z1, z2)
	return Float64(z0 / Float64(tan(z1) * z2))
end
function tmp = code(z0, z1, z2)
	tmp = z0 / (tan(z1) * z2);
end
code[z0_, z1_, z2_] := N[(z0 / N[(N[Tan[z1], $MachinePrecision] * z2), $MachinePrecision]), $MachinePrecision]
\frac{z0}{\tan z1 \cdot z2}

Alternative 1: 99.6% accurate, 0.2× speedup?

\[\begin{array}{l} t_0 := \tan \left(\left|z1\right|\right)\\ \mathsf{copysign}\left(1, z1\right) \cdot \left(\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l} \mathbf{if}\;t\_0 \cdot \left|z2\right| \leq \frac{80960901}{202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784}:\\ \;\;\;\;\frac{\frac{z0}{\left|z2\right|}}{t\_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{z0}{\sin \left(\left|z1\right|\right) \cdot \left|z2\right|} \cdot \cos \left(\left|z1\right|\right)\\ \end{array}\right) \end{array} \]
(FPCore (z0 z1 z2)
  :precision binary64
  (let* ((t_0 (tan (fabs z1))))
  (*
   (copysign 1 z1)
   (*
    (copysign 1 z2)
    (if (<=
         (* t_0 (fabs z2))
         80960901/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784)
      (/ (/ z0 (fabs z2)) t_0)
      (* (/ z0 (* (sin (fabs z1)) (fabs z2))) (cos (fabs z1))))))))
double code(double z0, double z1, double z2) {
	double t_0 = tan(fabs(z1));
	double tmp;
	if ((t_0 * fabs(z2)) <= 4e-316) {
		tmp = (z0 / fabs(z2)) / t_0;
	} else {
		tmp = (z0 / (sin(fabs(z1)) * fabs(z2))) * cos(fabs(z1));
	}
	return copysign(1.0, z1) * (copysign(1.0, z2) * tmp);
}
public static double code(double z0, double z1, double z2) {
	double t_0 = Math.tan(Math.abs(z1));
	double tmp;
	if ((t_0 * Math.abs(z2)) <= 4e-316) {
		tmp = (z0 / Math.abs(z2)) / t_0;
	} else {
		tmp = (z0 / (Math.sin(Math.abs(z1)) * Math.abs(z2))) * Math.cos(Math.abs(z1));
	}
	return Math.copySign(1.0, z1) * (Math.copySign(1.0, z2) * tmp);
}
def code(z0, z1, z2):
	t_0 = math.tan(math.fabs(z1))
	tmp = 0
	if (t_0 * math.fabs(z2)) <= 4e-316:
		tmp = (z0 / math.fabs(z2)) / t_0
	else:
		tmp = (z0 / (math.sin(math.fabs(z1)) * math.fabs(z2))) * math.cos(math.fabs(z1))
	return math.copysign(1.0, z1) * (math.copysign(1.0, z2) * tmp)
function code(z0, z1, z2)
	t_0 = tan(abs(z1))
	tmp = 0.0
	if (Float64(t_0 * abs(z2)) <= 4e-316)
		tmp = Float64(Float64(z0 / abs(z2)) / t_0);
	else
		tmp = Float64(Float64(z0 / Float64(sin(abs(z1)) * abs(z2))) * cos(abs(z1)));
	end
	return Float64(copysign(1.0, z1) * Float64(copysign(1.0, z2) * tmp))
end
function tmp_2 = code(z0, z1, z2)
	t_0 = tan(abs(z1));
	tmp = 0.0;
	if ((t_0 * abs(z2)) <= 4e-316)
		tmp = (z0 / abs(z2)) / t_0;
	else
		tmp = (z0 / (sin(abs(z1)) * abs(z2))) * cos(abs(z1));
	end
	tmp_2 = (sign(z1) * abs(1.0)) * ((sign(z2) * abs(1.0)) * tmp);
end
code[z0_, z1_, z2_] := Block[{t$95$0 = N[Tan[N[Abs[z1], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[z1]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[z2]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[(t$95$0 * N[Abs[z2], $MachinePrecision]), $MachinePrecision], 80960901/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784], N[(N[(z0 / N[Abs[z2], $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision], N[(N[(z0 / N[(N[Sin[N[Abs[z1], $MachinePrecision]], $MachinePrecision] * N[Abs[z2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[N[Abs[z1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
t_0 := \tan \left(\left|z1\right|\right)\\
\mathsf{copysign}\left(1, z1\right) \cdot \left(\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \cdot \left|z2\right| \leq \frac{80960901}{202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784}:\\
\;\;\;\;\frac{\frac{z0}{\left|z2\right|}}{t\_0}\\

\mathbf{else}:\\
\;\;\;\;\frac{z0}{\sin \left(\left|z1\right|\right) \cdot \left|z2\right|} \cdot \cos \left(\left|z1\right|\right)\\


\end{array}\right)
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (tan.f64 z1) z2) < 3.9999999840454223e-316

    1. Initial program 95.8%

      \[\frac{z0}{\tan z1 \cdot z2} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{z0}{\tan z1 \cdot z2}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{z0}{\color{blue}{\tan z1 \cdot z2}} \]
      3. *-commutativeN/A

        \[\leadsto \frac{z0}{\color{blue}{z2 \cdot \tan z1}} \]
      4. associate-/r*N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]
      6. lower-/.f6496.1%

        \[\leadsto \frac{\color{blue}{\frac{z0}{z2}}}{\tan z1} \]
    3. Applied rewrites96.1%

      \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]

    if 3.9999999840454223e-316 < (*.f64 (tan.f64 z1) z2)

    1. Initial program 95.8%

      \[\frac{z0}{\tan z1 \cdot z2} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{z0}{\tan z1 \cdot z2}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{z0}{\color{blue}{\tan z1 \cdot z2}} \]
      3. *-commutativeN/A

        \[\leadsto \frac{z0}{\color{blue}{z2 \cdot \tan z1}} \]
      4. associate-/r*N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]
      5. lift-tan.f64N/A

        \[\leadsto \frac{\frac{z0}{z2}}{\color{blue}{\tan z1}} \]
      6. tan-quotN/A

        \[\leadsto \frac{\frac{z0}{z2}}{\color{blue}{\frac{\sin z1}{\cos z1}}} \]
      7. associate-/r/N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\sin z1} \cdot \cos z1} \]
      8. lower-*.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\sin z1} \cdot \cos z1} \]
      9. associate-/l/N/A

        \[\leadsto \color{blue}{\frac{z0}{z2 \cdot \sin z1}} \cdot \cos z1 \]
      10. *-commutativeN/A

        \[\leadsto \frac{z0}{\color{blue}{\sin z1 \cdot z2}} \cdot \cos z1 \]
      11. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{z0}{\sin z1 \cdot z2}} \cdot \cos z1 \]
      12. lower-*.f64N/A

        \[\leadsto \frac{z0}{\color{blue}{\sin z1 \cdot z2}} \cdot \cos z1 \]
      13. lower-sin.f64N/A

        \[\leadsto \frac{z0}{\color{blue}{\sin z1} \cdot z2} \cdot \cos z1 \]
      14. lower-cos.f6495.7%

        \[\leadsto \frac{z0}{\sin z1 \cdot z2} \cdot \color{blue}{\cos z1} \]
    3. Applied rewrites95.7%

      \[\leadsto \color{blue}{\frac{z0}{\sin z1 \cdot z2} \cdot \cos z1} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 99.6% accurate, 0.5× speedup?

\[\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l} \mathbf{if}\;\left|z2\right| \leq 900:\\ \;\;\;\;\frac{\frac{z0}{\left|z2\right|}}{\tan z1}\\ \mathbf{else}:\\ \;\;\;\;\frac{z0}{\tan z1 \cdot \left|z2\right|}\\ \end{array} \]
(FPCore (z0 z1 z2)
  :precision binary64
  (*
 (copysign 1 z2)
 (if (<= (fabs z2) 900)
   (/ (/ z0 (fabs z2)) (tan z1))
   (/ z0 (* (tan z1) (fabs z2))))))
double code(double z0, double z1, double z2) {
	double tmp;
	if (fabs(z2) <= 900.0) {
		tmp = (z0 / fabs(z2)) / tan(z1);
	} else {
		tmp = z0 / (tan(z1) * fabs(z2));
	}
	return copysign(1.0, z2) * tmp;
}
public static double code(double z0, double z1, double z2) {
	double tmp;
	if (Math.abs(z2) <= 900.0) {
		tmp = (z0 / Math.abs(z2)) / Math.tan(z1);
	} else {
		tmp = z0 / (Math.tan(z1) * Math.abs(z2));
	}
	return Math.copySign(1.0, z2) * tmp;
}
def code(z0, z1, z2):
	tmp = 0
	if math.fabs(z2) <= 900.0:
		tmp = (z0 / math.fabs(z2)) / math.tan(z1)
	else:
		tmp = z0 / (math.tan(z1) * math.fabs(z2))
	return math.copysign(1.0, z2) * tmp
function code(z0, z1, z2)
	tmp = 0.0
	if (abs(z2) <= 900.0)
		tmp = Float64(Float64(z0 / abs(z2)) / tan(z1));
	else
		tmp = Float64(z0 / Float64(tan(z1) * abs(z2)));
	end
	return Float64(copysign(1.0, z2) * tmp)
end
function tmp_2 = code(z0, z1, z2)
	tmp = 0.0;
	if (abs(z2) <= 900.0)
		tmp = (z0 / abs(z2)) / tan(z1);
	else
		tmp = z0 / (tan(z1) * abs(z2));
	end
	tmp_2 = (sign(z2) * abs(1.0)) * tmp;
end
code[z0_, z1_, z2_] := N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[z2]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[z2], $MachinePrecision], 900], N[(N[(z0 / N[Abs[z2], $MachinePrecision]), $MachinePrecision] / N[Tan[z1], $MachinePrecision]), $MachinePrecision], N[(z0 / N[(N[Tan[z1], $MachinePrecision] * N[Abs[z2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|z2\right| \leq 900:\\
\;\;\;\;\frac{\frac{z0}{\left|z2\right|}}{\tan z1}\\

\mathbf{else}:\\
\;\;\;\;\frac{z0}{\tan z1 \cdot \left|z2\right|}\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z2 < 900

    1. Initial program 95.8%

      \[\frac{z0}{\tan z1 \cdot z2} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{z0}{\tan z1 \cdot z2}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{z0}{\color{blue}{\tan z1 \cdot z2}} \]
      3. *-commutativeN/A

        \[\leadsto \frac{z0}{\color{blue}{z2 \cdot \tan z1}} \]
      4. associate-/r*N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]
      6. lower-/.f6496.1%

        \[\leadsto \frac{\color{blue}{\frac{z0}{z2}}}{\tan z1} \]
    3. Applied rewrites96.1%

      \[\leadsto \color{blue}{\frac{\frac{z0}{z2}}{\tan z1}} \]

    if 900 < z2

    1. Initial program 95.8%

      \[\frac{z0}{\tan z1 \cdot z2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2025277 -o generate:taylor -o generate:evaluate
(FPCore (z0 z1 z2)
  :name "(/ z0 (* (tan z1) z2))"
  :precision binary64
  (/ z0 (* (tan z1) z2)))