
(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 6 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 (+ (- (/ (+ (tan y) (tan z)) (+ 1.0 (+ 1.0 (- -1.0 (* (tan y) (tan z)))))) (tan a)) x))
double code(double x, double y, double z, double a) {
return (((tan(y) + tan(z)) / (1.0 + (1.0 + (-1.0 - (tan(y) * tan(z)))))) - tan(a)) + 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 = (((tan(y) + tan(z)) / (1.0d0 + (1.0d0 + ((-1.0d0) - (tan(y) * tan(z)))))) - tan(a)) + x
end function
public static double code(double x, double y, double z, double a) {
return (((Math.tan(y) + Math.tan(z)) / (1.0 + (1.0 + (-1.0 - (Math.tan(y) * Math.tan(z)))))) - Math.tan(a)) + x;
}
def code(x, y, z, a): return (((math.tan(y) + math.tan(z)) / (1.0 + (1.0 + (-1.0 - (math.tan(y) * math.tan(z)))))) - math.tan(a)) + x
function code(x, y, z, a) return Float64(Float64(Float64(Float64(tan(y) + tan(z)) / Float64(1.0 + Float64(1.0 + Float64(-1.0 - Float64(tan(y) * tan(z)))))) - tan(a)) + x) end
function tmp = code(x, y, z, a) tmp = (((tan(y) + tan(z)) / (1.0 + (1.0 + (-1.0 - (tan(y) * tan(z)))))) - tan(a)) + x; end
code[x_, y_, z_, a_] := N[(N[(N[(N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(1.0 + N[(-1.0 - N[(N[Tan[y], $MachinePrecision] * N[Tan[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{\tan y + \tan z}{1 + \left(1 + \left(-1 - \tan y \cdot \tan z\right)\right)} - \tan a\right) + x
\end{array}
Initial program 81.2%
+-commutative81.2%
sub-neg81.2%
associate-+l+81.2%
tan-sum99.7%
div-inv99.7%
fma-define99.7%
neg-mul-199.7%
fma-define99.7%
Applied egg-rr99.7%
fma-undefine99.7%
fma-undefine99.7%
neg-mul-199.7%
associate-+r+99.8%
sub-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
Simplified99.8%
expm1-log1p-u94.7%
expm1-undefine94.7%
log1p-undefine94.7%
add-exp-log99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x y z a)
:precision binary64
(let* ((t_0 (tan (+ y z))))
(if (<= (tan a) -0.02)
(+ x (- t_0 (/ (sin a) (cos a))))
(if (<= (tan a) 2e-33)
(+ (/ (+ (tan y) (tan z)) (- 1.0 (* (tan y) (tan z)))) (- x a))
(+ t_0 (- x (tan a)))))))
double code(double x, double y, double z, double a) {
double t_0 = tan((y + z));
double tmp;
if (tan(a) <= -0.02) {
tmp = x + (t_0 - (sin(a) / cos(a)));
} else if (tan(a) <= 2e-33) {
tmp = ((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z)))) + (x - a);
} else {
tmp = t_0 + (x - 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) :: t_0
real(8) :: tmp
t_0 = tan((y + z))
if (tan(a) <= (-0.02d0)) then
tmp = x + (t_0 - (sin(a) / cos(a)))
else if (tan(a) <= 2d-33) then
tmp = ((tan(y) + tan(z)) / (1.0d0 - (tan(y) * tan(z)))) + (x - a)
else
tmp = t_0 + (x - tan(a))
end if
code = tmp
end function
public static double code(double x, double y, double z, double a) {
double t_0 = Math.tan((y + z));
double tmp;
if (Math.tan(a) <= -0.02) {
tmp = x + (t_0 - (Math.sin(a) / Math.cos(a)));
} else if (Math.tan(a) <= 2e-33) {
tmp = ((Math.tan(y) + Math.tan(z)) / (1.0 - (Math.tan(y) * Math.tan(z)))) + (x - a);
} else {
tmp = t_0 + (x - Math.tan(a));
}
return tmp;
}
def code(x, y, z, a): t_0 = math.tan((y + z)) tmp = 0 if math.tan(a) <= -0.02: tmp = x + (t_0 - (math.sin(a) / math.cos(a))) elif math.tan(a) <= 2e-33: tmp = ((math.tan(y) + math.tan(z)) / (1.0 - (math.tan(y) * math.tan(z)))) + (x - a) else: tmp = t_0 + (x - math.tan(a)) return tmp
function code(x, y, z, a) t_0 = tan(Float64(y + z)) tmp = 0.0 if (tan(a) <= -0.02) tmp = Float64(x + Float64(t_0 - Float64(sin(a) / cos(a)))); elseif (tan(a) <= 2e-33) tmp = Float64(Float64(Float64(tan(y) + tan(z)) / Float64(1.0 - Float64(tan(y) * tan(z)))) + Float64(x - a)); else tmp = Float64(t_0 + Float64(x - tan(a))); end return tmp end
function tmp_2 = code(x, y, z, a) t_0 = tan((y + z)); tmp = 0.0; if (tan(a) <= -0.02) tmp = x + (t_0 - (sin(a) / cos(a))); elseif (tan(a) <= 2e-33) tmp = ((tan(y) + tan(z)) / (1.0 - (tan(y) * tan(z)))) + (x - a); else tmp = t_0 + (x - tan(a)); end tmp_2 = tmp; end
code[x_, y_, z_, a_] := Block[{t$95$0 = N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[Tan[a], $MachinePrecision], -0.02], N[(x + N[(t$95$0 - N[(N[Sin[a], $MachinePrecision] / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Tan[a], $MachinePrecision], 2e-33], 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[(x - a), $MachinePrecision]), $MachinePrecision], N[(t$95$0 + N[(x - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \tan \left(y + z\right)\\
\mathbf{if}\;\tan a \leq -0.02:\\
\;\;\;\;x + \left(t\_0 - \frac{\sin a}{\cos a}\right)\\
\mathbf{elif}\;\tan a \leq 2 \cdot 10^{-33}:\\
\;\;\;\;\frac{\tan y + \tan z}{1 - \tan y \cdot \tan z} + \left(x - a\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0 + \left(x - \tan a\right)\\
\end{array}
\end{array}
if (tan.f64 a) < -0.0200000000000000004Initial program 80.4%
Taylor expanded in a around inf 80.4%
if -0.0200000000000000004 < (tan.f64 a) < 2.0000000000000001e-33Initial program 81.1%
+-commutative81.1%
associate-+l-81.1%
Applied egg-rr81.1%
Taylor expanded in a around 0 81.1%
tan-sum99.9%
div-inv99.9%
Applied egg-rr99.9%
associate-*r/99.9%
*-rgt-identity99.9%
Simplified99.9%
if 2.0000000000000001e-33 < (tan.f64 a) Initial program 82.2%
+-commutative82.2%
associate-+l-82.2%
Applied egg-rr82.2%
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 81.2%
+-commutative81.2%
sub-neg81.2%
associate-+l+81.2%
tan-sum99.7%
div-inv99.7%
fma-define99.7%
neg-mul-199.7%
fma-define99.7%
Applied egg-rr99.7%
fma-undefine99.7%
fma-undefine99.7%
neg-mul-199.7%
associate-+r+99.8%
sub-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
Simplified99.8%
Final simplification99.8%
(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 81.2%
(FPCore (x y z a) :precision binary64 (+ x (+ (tan a) (tan (+ y z)))))
double code(double x, double y, double z, double a) {
return x + (tan(a) + 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(a) + tan((y + z)))
end function
public static double code(double x, double y, double z, double a) {
return x + (Math.tan(a) + Math.tan((y + z)));
}
def code(x, y, z, a): return x + (math.tan(a) + math.tan((y + z)))
function code(x, y, z, a) return Float64(x + Float64(tan(a) + tan(Float64(y + z)))) end
function tmp = code(x, y, z, a) tmp = x + (tan(a) + tan((y + z))); end
code[x_, y_, z_, a_] := N[(x + N[(N[Tan[a], $MachinePrecision] + N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\tan a + \tan \left(y + z\right)\right)
\end{array}
Initial program 81.2%
+-commutative81.2%
associate-+l-81.2%
Applied egg-rr81.2%
associate--r-81.2%
+-commutative81.2%
associate--l+81.2%
sub-neg81.2%
rem-square-sqrt38.2%
fabs-sqr38.2%
rem-square-sqrt64.2%
fabs-neg64.2%
rem-square-sqrt26.0%
fabs-sqr26.0%
rem-square-sqrt48.0%
associate-+l+48.0%
Simplified48.0%
Final simplification48.0%
(FPCore (x y z a) :precision binary64 (+ x (- (tan (+ y z)) a)))
double code(double x, double y, double z, double a) {
return x + (tan((y + z)) - 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)) - a)
end function
public static double code(double x, double y, double z, double a) {
return x + (Math.tan((y + z)) - a);
}
def code(x, y, z, a): return x + (math.tan((y + z)) - a)
function code(x, y, z, a) return Float64(x + Float64(tan(Float64(y + z)) - a)) end
function tmp = code(x, y, z, a) tmp = x + (tan((y + z)) - a); end
code[x_, y_, z_, a_] := N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\tan \left(y + z\right) - a\right)
\end{array}
Initial program 81.2%
+-commutative81.2%
associate-+l-81.2%
Applied egg-rr81.2%
Taylor expanded in a around 0 37.9%
associate--r-37.9%
Applied egg-rr37.9%
Final simplification37.9%
herbie shell --seed 2024179
(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))))