
(FPCore (x y z a) :precision binary64 (+ x (- (tan (+ y z)) (tan a))))
double code(double x, double y, double z, double a) {
return x + (tan((y + z)) - tan(a));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + (tan((y + z)) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + (Math.tan((y + z)) - Math.tan(a));
}
def code(x, y, z, a): return x + (math.tan((y + z)) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(tan(Float64(y + z)) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + (tan((y + z)) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\tan \left(y + z\right) - \tan a\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z a) :precision binary64 (+ x (- (tan (+ y z)) (tan a))))
double code(double x, double y, double z, double a) {
return x + (tan((y + z)) - tan(a));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + (tan((y + z)) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + (Math.tan((y + z)) - Math.tan(a));
}
def code(x, y, z, a): return x + (math.tan((y + z)) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(tan(Float64(y + z)) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + (tan((y + z)) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\tan \left(y + z\right) - \tan a\right)
\end{array}
(FPCore (x y z a) :precision binary64 (+ x (- (/ (+ (tan y) (tan z)) (- 1.0 (+ (+ 1.0 (* (tan y) (tan z))) -1.0))) (tan a))))
double code(double x, double y, double z, double a) {
return x + (((tan(y) + tan(z)) / (1.0 - ((1.0 + (tan(y) * tan(z))) + -1.0))) - tan(a));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + (((tan(y) + tan(z)) / (1.0d0 - ((1.0d0 + (tan(y) * tan(z))) + (-1.0d0)))) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + (((Math.tan(y) + Math.tan(z)) / (1.0 - ((1.0 + (Math.tan(y) * Math.tan(z))) + -1.0))) - Math.tan(a));
}
def code(x, y, z, a): return x + (((math.tan(y) + math.tan(z)) / (1.0 - ((1.0 + (math.tan(y) * math.tan(z))) + -1.0))) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(Float64(Float64(tan(y) + tan(z)) / Float64(1.0 - Float64(Float64(1.0 + Float64(tan(y) * tan(z))) + -1.0))) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + (((tan(y) + tan(z)) / (1.0 - ((1.0 + (tan(y) * tan(z))) + -1.0))) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[(N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision] / N[(1.0 - N[(N[(1.0 + N[(N[Tan[y], $MachinePrecision] * N[Tan[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\frac{\tan y + \tan z}{1 - \left(\left(1 + \tan y \cdot \tan z\right) + -1\right)} - \tan a\right)
\end{array}
Initial program 79.3%
tan-sum99.7%
div-inv99.6%
Applied egg-rr99.6%
associate-*r/99.7%
*-rgt-identity99.7%
Simplified99.7%
expm1-log1p-u93.8%
expm1-udef93.8%
log1p-udef93.8%
add-exp-log99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (x y z a)
:precision binary64
(if (<= (tan a) -5e-9)
(+ x (- (tan (+ y z)) (tan a)))
(if (<= (tan a) 5e-23)
(+ x (/ (+ (tan y) (tan z)) (- 1.0 (* (tan y) (tan z)))))
(+ x (- (/ 1.0 (/ (cos (+ y z)) (sin (+ y z)))) (tan a))))))
double code(double x, double y, double z, double a) {
double tmp;
if (tan(a) <= -5e-9) {
tmp = x + (tan((y + z)) - tan(a));
} else if (tan(a) <= 5e-23) {
tmp = x + ((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z))));
} else {
tmp = x + ((1.0 / (cos((y + z)) / sin((y + z)))) - tan(a));
}
return tmp;
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
real(8) :: tmp
if (tan(a) <= (-5d-9)) then
tmp = x + (tan((y + z)) - tan(a))
else if (tan(a) <= 5d-23) then
tmp = x + ((tan(y) + tan(z)) / (1.0d0 - (tan(y) * tan(z))))
else
tmp = x + ((1.0d0 / (cos((y + z)) / sin((y + z)))) - tan(a))
end if
code = tmp
end function
public static double code(double x, double y, double z, double a) {
double tmp;
if (Math.tan(a) <= -5e-9) {
tmp = x + (Math.tan((y + z)) - Math.tan(a));
} else if (Math.tan(a) <= 5e-23) {
tmp = x + ((Math.tan(y) + Math.tan(z)) / (1.0 - (Math.tan(y) * Math.tan(z))));
} else {
tmp = x + ((1.0 / (Math.cos((y + z)) / Math.sin((y + z)))) - Math.tan(a));
}
return tmp;
}
def code(x, y, z, a): tmp = 0 if math.tan(a) <= -5e-9: tmp = x + (math.tan((y + z)) - math.tan(a)) elif math.tan(a) <= 5e-23: tmp = x + ((math.tan(y) + math.tan(z)) / (1.0 - (math.tan(y) * math.tan(z)))) else: tmp = x + ((1.0 / (math.cos((y + z)) / math.sin((y + z)))) - math.tan(a)) return tmp
function code(x, y, z, a) tmp = 0.0 if (tan(a) <= -5e-9) tmp = Float64(x + Float64(tan(Float64(y + z)) - tan(a))); elseif (tan(a) <= 5e-23) tmp = Float64(x + Float64(Float64(tan(y) + tan(z)) / Float64(1.0 - Float64(tan(y) * tan(z))))); else tmp = Float64(x + Float64(Float64(1.0 / Float64(cos(Float64(y + z)) / sin(Float64(y + z)))) - tan(a))); end return tmp end
function tmp_2 = code(x, y, z, a) tmp = 0.0; if (tan(a) <= -5e-9) tmp = x + (tan((y + z)) - tan(a)); elseif (tan(a) <= 5e-23) tmp = x + ((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z)))); else tmp = x + ((1.0 / (cos((y + z)) / sin((y + z)))) - tan(a)); end tmp_2 = tmp; end
code[x_, y_, z_, a_] := If[LessEqual[N[Tan[a], $MachinePrecision], -5e-9], N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Tan[a], $MachinePrecision], 5e-23], N[(x + N[(N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision] / N[(1.0 - N[(N[Tan[y], $MachinePrecision] * N[Tan[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x + N[(N[(1.0 / N[(N[Cos[N[(y + z), $MachinePrecision]], $MachinePrecision] / N[Sin[N[(y + z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\tan a \leq -5 \cdot 10^{-9}:\\
\;\;\;\;x + \left(\tan \left(y + z\right) - \tan a\right)\\
\mathbf{elif}\;\tan a \leq 5 \cdot 10^{-23}:\\
\;\;\;\;x + \frac{\tan y + \tan z}{1 - \tan y \cdot \tan z}\\
\mathbf{else}:\\
\;\;\;\;x + \left(\frac{1}{\frac{\cos \left(y + z\right)}{\sin \left(y + z\right)}} - \tan a\right)\\
\end{array}
\end{array}
if (tan.f64 a) < -5.0000000000000001e-9Initial program 85.4%
if -5.0000000000000001e-9 < (tan.f64 a) < 5.0000000000000002e-23Initial program 77.8%
+-commutative77.8%
associate-+l-77.8%
Applied egg-rr77.8%
Taylor expanded in a around 0 77.8%
neg-mul-177.8%
Simplified77.8%
tan-sum99.7%
div-inv99.7%
fma-neg99.7%
Applied egg-rr99.7%
fma-udef99.7%
associate-*r/99.7%
*-rgt-identity99.7%
remove-double-neg99.7%
Simplified99.7%
if 5.0000000000000002e-23 < (tan.f64 a) Initial program 75.6%
tan-quot75.6%
clear-num75.6%
Applied egg-rr75.6%
Final simplification89.2%
(FPCore (x y z a) :precision binary64 (+ x (- (/ (+ (tan y) (tan z)) (- 1.0 (* (tan y) (tan z)))) (tan a))))
double code(double x, double y, double z, double a) {
return x + (((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z)))) - tan(a));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + (((tan(y) + tan(z)) / (1.0d0 - (tan(y) * tan(z)))) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + (((Math.tan(y) + Math.tan(z)) / (1.0 - (Math.tan(y) * Math.tan(z)))) - Math.tan(a));
}
def code(x, y, z, a): return x + (((math.tan(y) + math.tan(z)) / (1.0 - (math.tan(y) * math.tan(z)))) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(Float64(Float64(tan(y) + tan(z)) / Float64(1.0 - Float64(tan(y) * tan(z)))) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + (((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z)))) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[(N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision] / N[(1.0 - N[(N[Tan[y], $MachinePrecision] * N[Tan[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\frac{\tan y + \tan z}{1 - \tan y \cdot \tan z} - \tan a\right)
\end{array}
Initial program 79.3%
tan-sum99.7%
div-inv99.6%
Applied egg-rr99.6%
associate-*r/99.7%
*-rgt-identity99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x y z a) :precision binary64 (if (<= (+ y z) -0.5) (+ x (tan (+ y z))) (+ x (- (tan z) (tan a)))))
double code(double x, double y, double z, double a) {
double tmp;
if ((y + z) <= -0.5) {
tmp = x + tan((y + z));
} else {
tmp = x + (tan(z) - tan(a));
}
return tmp;
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
real(8) :: tmp
if ((y + z) <= (-0.5d0)) then
tmp = x + tan((y + z))
else
tmp = x + (tan(z) - tan(a))
end if
code = tmp
end function
public static double code(double x, double y, double z, double a) {
double tmp;
if ((y + z) <= -0.5) {
tmp = x + Math.tan((y + z));
} else {
tmp = x + (Math.tan(z) - Math.tan(a));
}
return tmp;
}
def code(x, y, z, a): tmp = 0 if (y + z) <= -0.5: tmp = x + math.tan((y + z)) else: tmp = x + (math.tan(z) - math.tan(a)) return tmp
function code(x, y, z, a) tmp = 0.0 if (Float64(y + z) <= -0.5) tmp = Float64(x + tan(Float64(y + z))); else tmp = Float64(x + Float64(tan(z) - tan(a))); end return tmp end
function tmp_2 = code(x, y, z, a) tmp = 0.0; if ((y + z) <= -0.5) tmp = x + tan((y + z)); else tmp = x + (tan(z) - tan(a)); end tmp_2 = tmp; end
code[x_, y_, z_, a_] := If[LessEqual[N[(y + z), $MachinePrecision], -0.5], N[(x + N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(x + N[(N[Tan[z], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y + z \leq -0.5:\\
\;\;\;\;x + \tan \left(y + z\right)\\
\mathbf{else}:\\
\;\;\;\;x + \left(\tan z - \tan a\right)\\
\end{array}
\end{array}
if (+.f64 y z) < -0.5Initial program 74.5%
+-commutative74.5%
associate-+l-74.5%
Applied egg-rr74.5%
Taylor expanded in a around 0 43.6%
neg-mul-143.6%
Simplified43.6%
sub-neg43.6%
Applied egg-rr43.6%
remove-double-neg43.6%
+-commutative43.6%
Simplified43.6%
if -0.5 < (+.f64 y z) Initial program 82.1%
Taylor expanded in y around 0 65.3%
+-commutative65.3%
Simplified65.3%
tan-quot65.3%
tan-quot65.3%
associate--l+65.3%
Applied egg-rr65.3%
associate-+r-65.3%
+-commutative65.3%
associate--l+65.4%
Simplified65.4%
Final simplification57.3%
(FPCore (x y z a) :precision binary64 (if (<= (+ y z) -0.5) (+ x (/ (sin y) (cos y))) (+ x (- (tan z) (tan a)))))
double code(double x, double y, double z, double a) {
double tmp;
if ((y + z) <= -0.5) {
tmp = x + (sin(y) / cos(y));
} else {
tmp = x + (tan(z) - tan(a));
}
return tmp;
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
real(8) :: tmp
if ((y + z) <= (-0.5d0)) then
tmp = x + (sin(y) / cos(y))
else
tmp = x + (tan(z) - tan(a))
end if
code = tmp
end function
public static double code(double x, double y, double z, double a) {
double tmp;
if ((y + z) <= -0.5) {
tmp = x + (Math.sin(y) / Math.cos(y));
} else {
tmp = x + (Math.tan(z) - Math.tan(a));
}
return tmp;
}
def code(x, y, z, a): tmp = 0 if (y + z) <= -0.5: tmp = x + (math.sin(y) / math.cos(y)) else: tmp = x + (math.tan(z) - math.tan(a)) return tmp
function code(x, y, z, a) tmp = 0.0 if (Float64(y + z) <= -0.5) tmp = Float64(x + Float64(sin(y) / cos(y))); else tmp = Float64(x + Float64(tan(z) - tan(a))); end return tmp end
function tmp_2 = code(x, y, z, a) tmp = 0.0; if ((y + z) <= -0.5) tmp = x + (sin(y) / cos(y)); else tmp = x + (tan(z) - tan(a)); end tmp_2 = tmp; end
code[x_, y_, z_, a_] := If[LessEqual[N[(y + z), $MachinePrecision], -0.5], N[(x + N[(N[Sin[y], $MachinePrecision] / N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x + N[(N[Tan[z], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y + z \leq -0.5:\\
\;\;\;\;x + \frac{\sin y}{\cos y}\\
\mathbf{else}:\\
\;\;\;\;x + \left(\tan z - \tan a\right)\\
\end{array}
\end{array}
if (+.f64 y z) < -0.5Initial program 74.5%
+-commutative74.5%
associate-+l-74.5%
Applied egg-rr74.5%
Taylor expanded in a around 0 43.6%
neg-mul-143.6%
Simplified43.6%
Taylor expanded in z around 0 32.0%
+-commutative32.0%
Simplified32.0%
if -0.5 < (+.f64 y z) Initial program 82.1%
Taylor expanded in y around 0 65.3%
+-commutative65.3%
Simplified65.3%
tan-quot65.3%
tan-quot65.3%
associate--l+65.3%
Applied egg-rr65.3%
associate-+r-65.3%
+-commutative65.3%
associate--l+65.4%
Simplified65.4%
Final simplification53.0%
(FPCore (x y z a) :precision binary64 (+ x (- (tan (+ y z)) (tan a))))
double code(double x, double y, double z, double a) {
return x + (tan((y + z)) - tan(a));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + (tan((y + z)) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + (Math.tan((y + z)) - Math.tan(a));
}
def code(x, y, z, a): return x + (math.tan((y + z)) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(tan(Float64(y + z)) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + (tan((y + z)) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\tan \left(y + z\right) - \tan a\right)
\end{array}
Initial program 79.3%
Final simplification79.3%
(FPCore (x y z a) :precision binary64 (+ x (tan (+ y z))))
double code(double x, double y, double z, double a) {
return x + tan((y + z));
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x + tan((y + z))
end function
public static double code(double x, double y, double z, double a) {
return x + Math.tan((y + z));
}
def code(x, y, z, a): return x + math.tan((y + z))
function code(x, y, z, a) return Float64(x + tan(Float64(y + z))) end
function tmp = code(x, y, z, a) tmp = x + tan((y + z)); end
code[x_, y_, z_, a_] := N[(x + N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \tan \left(y + z\right)
\end{array}
Initial program 79.3%
+-commutative79.3%
associate-+l-79.2%
Applied egg-rr79.2%
Taylor expanded in a around 0 47.9%
neg-mul-147.9%
Simplified47.9%
sub-neg47.9%
Applied egg-rr47.9%
remove-double-neg47.9%
+-commutative47.9%
Simplified47.9%
Final simplification47.9%
(FPCore (x y z a) :precision binary64 x)
double code(double x, double y, double z, double a) {
return x;
}
real(8) function code(x, y, z, a)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: a
code = x
end function
public static double code(double x, double y, double z, double a) {
return x;
}
def code(x, y, z, a): return x
function code(x, y, z, a) return x end
function tmp = code(x, y, z, a) tmp = x; end
code[x_, y_, z_, a_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 79.3%
Taylor expanded in x around inf 31.0%
Final simplification31.0%
herbie shell --seed 2024010
(FPCore (x y z a)
:name "tan-example (used to crash)"
:precision binary64
:pre (and (and (and (or (== x 0.0) (and (<= 0.5884142 x) (<= x 505.5909))) (or (and (<= -1.796658e+308 y) (<= y -9.425585e-310)) (and (<= 1.284938e-309 y) (<= y 1.751224e+308)))) (or (and (<= -1.776707e+308 z) (<= z -8.599796e-310)) (and (<= 3.293145e-311 z) (<= z 1.725154e+308)))) (or (and (<= -1.796658e+308 a) (<= a -9.425585e-310)) (and (<= 1.284938e-309 a) (<= a 1.751224e+308))))
(+ x (- (tan (+ y z)) (tan a))))