
(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 9 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)) (fma (tan y) (- (tan z)) 1.0)) (tan a))))
double code(double x, double y, double z, double a) {
return x + (((tan(y) + tan(z)) / fma(tan(y), -tan(z), 1.0)) - tan(a));
}
function code(x, y, z, a) return Float64(x + Float64(Float64(Float64(tan(y) + tan(z)) / fma(tan(y), Float64(-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[(N[Tan[y], $MachinePrecision] * (-N[Tan[z], $MachinePrecision]) + 1.0), $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\frac{\tan y + \tan z}{\mathsf{fma}\left(\tan y, -\tan z, 1\right)} - \tan a\right)
\end{array}
Initial program 80.9%
tan-sum99.6%
div-inv99.6%
Applied egg-rr99.6%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
expm1-log1p-u94.1%
expm1-undefine94.2%
log1p-undefine94.2%
add-exp-log99.6%
Applied egg-rr99.6%
associate--l+99.6%
fma-neg99.6%
metadata-eval99.6%
Simplified99.6%
sub-neg99.6%
associate--r+99.7%
metadata-eval99.7%
Applied egg-rr99.7%
*-rgt-identity99.7%
associate-*r/99.6%
fma-undefine99.7%
sub0-neg99.7%
fma-undefine99.6%
distribute-neg-in99.6%
metadata-eval99.6%
+-commutative99.6%
sub-neg99.6%
fma-neg99.6%
Simplified99.7%
(FPCore (x y z a)
:precision binary64
(let* ((t_0 (+ (tan y) (tan z))))
(if (<= (tan a) -8e-16)
(+ x (- (tan (+ y z)) (tan a)))
(if (<= (tan a) 2e-11)
(+ x (/ t_0 (fma (tan y) (- (tan z)) 1.0)))
(+ x (- t_0 (tan a)))))))
double code(double x, double y, double z, double a) {
double t_0 = tan(y) + tan(z);
double tmp;
if (tan(a) <= -8e-16) {
tmp = x + (tan((y + z)) - tan(a));
} else if (tan(a) <= 2e-11) {
tmp = x + (t_0 / fma(tan(y), -tan(z), 1.0));
} else {
tmp = x + (t_0 - tan(a));
}
return tmp;
}
function code(x, y, z, a) t_0 = Float64(tan(y) + tan(z)) tmp = 0.0 if (tan(a) <= -8e-16) tmp = Float64(x + Float64(tan(Float64(y + z)) - tan(a))); elseif (tan(a) <= 2e-11) tmp = Float64(x + Float64(t_0 / fma(tan(y), Float64(-tan(z)), 1.0))); else tmp = Float64(x + Float64(t_0 - tan(a))); end return tmp end
code[x_, y_, z_, a_] := Block[{t$95$0 = N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Tan[a], $MachinePrecision], -8e-16], N[(x + N[(N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Tan[a], $MachinePrecision], 2e-11], N[(x + N[(t$95$0 / N[(N[Tan[y], $MachinePrecision] * (-N[Tan[z], $MachinePrecision]) + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x + N[(t$95$0 - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \tan y + \tan z\\
\mathbf{if}\;\tan a \leq -8 \cdot 10^{-16}:\\
\;\;\;\;x + \left(\tan \left(y + z\right) - \tan a\right)\\
\mathbf{elif}\;\tan a \leq 2 \cdot 10^{-11}:\\
\;\;\;\;x + \frac{t\_0}{\mathsf{fma}\left(\tan y, -\tan z, 1\right)}\\
\mathbf{else}:\\
\;\;\;\;x + \left(t\_0 - \tan a\right)\\
\end{array}
\end{array}
if (tan.f64 a) < -7.9999999999999998e-16Initial program 80.5%
if -7.9999999999999998e-16 < (tan.f64 a) < 1.99999999999999988e-11Initial program 80.9%
expm1-log1p-u76.8%
+-commutative76.8%
associate-+l-76.8%
Applied egg-rr76.8%
Taylor expanded in a around 0 76.7%
log1p-define76.7%
Simplified76.7%
expm1-log1p-u80.8%
tan-quot80.9%
+-commutative80.9%
tan-sum99.5%
div-inv99.5%
fma-define99.5%
Applied egg-rr99.5%
fma-undefine99.5%
+-commutative99.5%
associate-*r/99.5%
*-rgt-identity99.5%
sub-neg99.5%
+-commutative99.5%
metadata-eval99.5%
sub-neg99.5%
distribute-rgt-neg-in99.5%
fma-neg99.5%
metadata-eval99.5%
Simplified99.5%
if 1.99999999999999988e-11 < (tan.f64 a) Initial program 81.1%
tan-sum99.3%
div-inv99.3%
Applied egg-rr99.3%
associate-*r/99.3%
*-rgt-identity99.3%
Simplified99.3%
expm1-log1p-u95.2%
expm1-undefine95.3%
log1p-undefine95.3%
add-exp-log99.4%
Applied egg-rr99.4%
associate--l+99.3%
fma-neg99.3%
metadata-eval99.3%
Simplified99.3%
sub-neg99.3%
associate--r+99.3%
metadata-eval99.3%
Applied egg-rr99.3%
*-rgt-identity99.3%
associate-*r/99.3%
fma-undefine99.3%
sub0-neg99.3%
fma-undefine99.3%
distribute-neg-in99.3%
metadata-eval99.3%
+-commutative99.3%
sub-neg99.3%
fma-neg99.3%
Simplified99.3%
Taylor expanded in y around 0 82.0%
Final simplification89.9%
(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 80.9%
tan-sum99.6%
div-inv99.6%
Applied egg-rr99.6%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
(FPCore (x y z a) :precision binary64 (+ x (- (+ (tan y) (tan z)) (tan a))))
double code(double x, double y, double z, double a) {
return x + ((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)) - tan(a))
end function
public static double code(double x, double y, double z, double a) {
return x + ((Math.tan(y) + Math.tan(z)) - Math.tan(a));
}
def code(x, y, z, a): return x + ((math.tan(y) + math.tan(z)) - math.tan(a))
function code(x, y, z, a) return Float64(x + Float64(Float64(tan(y) + tan(z)) - tan(a))) end
function tmp = code(x, y, z, a) tmp = x + ((tan(y) + tan(z)) - tan(a)); end
code[x_, y_, z_, a_] := N[(x + N[(N[(N[Tan[y], $MachinePrecision] + N[Tan[z], $MachinePrecision]), $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\left(\tan y + \tan z\right) - \tan a\right)
\end{array}
Initial program 80.9%
tan-sum99.6%
div-inv99.6%
Applied egg-rr99.6%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
expm1-log1p-u94.1%
expm1-undefine94.2%
log1p-undefine94.2%
add-exp-log99.6%
Applied egg-rr99.6%
associate--l+99.6%
fma-neg99.6%
metadata-eval99.6%
Simplified99.6%
sub-neg99.6%
associate--r+99.7%
metadata-eval99.7%
Applied egg-rr99.7%
*-rgt-identity99.7%
associate-*r/99.6%
fma-undefine99.7%
sub0-neg99.7%
fma-undefine99.6%
distribute-neg-in99.6%
metadata-eval99.6%
+-commutative99.6%
sub-neg99.6%
fma-neg99.6%
Simplified99.7%
Taylor expanded in y around 0 81.2%
Final simplification81.2%
(FPCore (x y z a) :precision binary64 (if (<= z 0.00039) (+ x (- (tan y) (tan a))) (+ x (tan (+ y z)))))
double code(double x, double y, double z, double a) {
double tmp;
if (z <= 0.00039) {
tmp = x + (tan(y) - tan(a));
} else {
tmp = x + tan((y + z));
}
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 (z <= 0.00039d0) then
tmp = x + (tan(y) - tan(a))
else
tmp = x + tan((y + z))
end if
code = tmp
end function
public static double code(double x, double y, double z, double a) {
double tmp;
if (z <= 0.00039) {
tmp = x + (Math.tan(y) - Math.tan(a));
} else {
tmp = x + Math.tan((y + z));
}
return tmp;
}
def code(x, y, z, a): tmp = 0 if z <= 0.00039: tmp = x + (math.tan(y) - math.tan(a)) else: tmp = x + math.tan((y + z)) return tmp
function code(x, y, z, a) tmp = 0.0 if (z <= 0.00039) tmp = Float64(x + Float64(tan(y) - tan(a))); else tmp = Float64(x + tan(Float64(y + z))); end return tmp end
function tmp_2 = code(x, y, z, a) tmp = 0.0; if (z <= 0.00039) tmp = x + (tan(y) - tan(a)); else tmp = x + tan((y + z)); end tmp_2 = tmp; end
code[x_, y_, z_, a_] := If[LessEqual[z, 0.00039], N[(x + N[(N[Tan[y], $MachinePrecision] - N[Tan[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x + N[Tan[N[(y + z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.00039:\\
\;\;\;\;x + \left(\tan y - \tan a\right)\\
\mathbf{else}:\\
\;\;\;\;x + \tan \left(y + z\right)\\
\end{array}
\end{array}
if z < 3.89999999999999993e-4Initial program 88.9%
expm1-log1p-u80.5%
+-commutative80.5%
associate-+l-80.5%
Applied egg-rr80.5%
Taylor expanded in z around 0 71.9%
expm1-log1p-u79.0%
tan-quot79.0%
associate--r-79.1%
Applied egg-rr79.1%
if 3.89999999999999993e-4 < z Initial program 57.8%
expm1-log1p-u53.4%
+-commutative53.4%
associate-+l-53.4%
Applied egg-rr53.4%
Taylor expanded in a around 0 37.0%
log1p-define37.0%
Simplified37.0%
expm1-log1p-u40.3%
tan-quot40.3%
+-commutative40.3%
Applied egg-rr40.3%
Final simplification69.1%
(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 80.9%
(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 80.9%
expm1-log1p-u73.5%
+-commutative73.5%
associate-+l-73.5%
Applied egg-rr73.5%
Taylor expanded in a around 0 48.3%
log1p-define48.4%
Simplified48.4%
expm1-log1p-u51.0%
tan-quot51.0%
+-commutative51.0%
Applied egg-rr51.0%
Final simplification51.0%
(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 80.9%
Taylor expanded in x around inf 31.8%
(FPCore (x y z a) :precision binary64 -1.0)
double code(double x, double y, double z, double a) {
return -1.0;
}
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 = -1.0d0
end function
public static double code(double x, double y, double z, double a) {
return -1.0;
}
def code(x, y, z, a): return -1.0
function code(x, y, z, a) return -1.0 end
function tmp = code(x, y, z, a) tmp = -1.0; end
code[x_, y_, z_, a_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 80.9%
expm1-log1p-u73.5%
+-commutative73.5%
associate-+l-73.5%
Applied egg-rr73.5%
Taylor expanded in x around inf 21.0%
mul-1-neg21.0%
log-rec21.0%
remove-double-neg21.0%
Simplified21.0%
Taylor expanded in x around 0 3.6%
herbie shell --seed 2024096
(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))))