
(FPCore (x) :precision binary64 (sqrt (/ (- (exp (* 2.0 x)) 1.0) (- (exp x) 1.0))))
double code(double x) {
return sqrt(((exp((2.0 * x)) - 1.0) / (exp(x) - 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((exp((2.0d0 * x)) - 1.0d0) / (exp(x) - 1.0d0)))
end function
public static double code(double x) {
return Math.sqrt(((Math.exp((2.0 * x)) - 1.0) / (Math.exp(x) - 1.0)));
}
def code(x): return math.sqrt(((math.exp((2.0 * x)) - 1.0) / (math.exp(x) - 1.0)))
function code(x) return sqrt(Float64(Float64(exp(Float64(2.0 * x)) - 1.0) / Float64(exp(x) - 1.0))) end
function tmp = code(x) tmp = sqrt(((exp((2.0 * x)) - 1.0) / (exp(x) - 1.0))); end
code[x_] := N[Sqrt[N[(N[(N[Exp[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{e^{2 \cdot x} - 1}{e^{x} - 1}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (sqrt (/ (- (exp (* 2.0 x)) 1.0) (- (exp x) 1.0))))
double code(double x) {
return sqrt(((exp((2.0 * x)) - 1.0) / (exp(x) - 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((exp((2.0d0 * x)) - 1.0d0) / (exp(x) - 1.0d0)))
end function
public static double code(double x) {
return Math.sqrt(((Math.exp((2.0 * x)) - 1.0) / (Math.exp(x) - 1.0)));
}
def code(x): return math.sqrt(((math.exp((2.0 * x)) - 1.0) / (math.exp(x) - 1.0)))
function code(x) return sqrt(Float64(Float64(exp(Float64(2.0 * x)) - 1.0) / Float64(exp(x) - 1.0))) end
function tmp = code(x) tmp = sqrt(((exp((2.0 * x)) - 1.0) / (exp(x) - 1.0))); end
code[x_] := N[Sqrt[N[(N[(N[Exp[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{e^{2 \cdot x} - 1}{e^{x} - 1}}
\end{array}
(FPCore (x) :precision binary64 (sqrt (+ 1.0 (exp x))))
double code(double x) {
return sqrt((1.0 + exp(x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((1.0d0 + exp(x)))
end function
public static double code(double x) {
return Math.sqrt((1.0 + Math.exp(x)));
}
def code(x): return math.sqrt((1.0 + math.exp(x)))
function code(x) return sqrt(Float64(1.0 + exp(x))) end
function tmp = code(x) tmp = sqrt((1.0 + exp(x))); end
code[x_] := N[Sqrt[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{1 + e^{x}}
\end{array}
Initial program 36.1%
*-commutative36.1%
exp-lft-sqr36.9%
difference-of-sqr-137.9%
associate-/l*38.3%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(if (<= x -4.4)
(sqrt 2.0)
(if (<= x 1.66)
(pow (+ 0.5 (* x -0.25)) -0.5)
(* (sqrt 2.0) (+ 1.0 (* x (* x 0.09375)))))))
double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = sqrt(2.0);
} else if (x <= 1.66) {
tmp = pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = sqrt(2.0) * (1.0 + (x * (x * 0.09375)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-4.4d0)) then
tmp = sqrt(2.0d0)
else if (x <= 1.66d0) then
tmp = (0.5d0 + (x * (-0.25d0))) ** (-0.5d0)
else
tmp = sqrt(2.0d0) * (1.0d0 + (x * (x * 0.09375d0)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = Math.sqrt(2.0);
} else if (x <= 1.66) {
tmp = Math.pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = Math.sqrt(2.0) * (1.0 + (x * (x * 0.09375)));
}
return tmp;
}
def code(x): tmp = 0 if x <= -4.4: tmp = math.sqrt(2.0) elif x <= 1.66: tmp = math.pow((0.5 + (x * -0.25)), -0.5) else: tmp = math.sqrt(2.0) * (1.0 + (x * (x * 0.09375))) return tmp
function code(x) tmp = 0.0 if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.66) tmp = Float64(0.5 + Float64(x * -0.25)) ^ -0.5; else tmp = Float64(sqrt(2.0) * Float64(1.0 + Float64(x * Float64(x * 0.09375)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.66) tmp = (0.5 + (x * -0.25)) ^ -0.5; else tmp = sqrt(2.0) * (1.0 + (x * (x * 0.09375))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -4.4], N[Sqrt[2.0], $MachinePrecision], If[LessEqual[x, 1.66], N[Power[N[(0.5 + N[(x * -0.25), $MachinePrecision]), $MachinePrecision], -0.5], $MachinePrecision], N[(N[Sqrt[2.0], $MachinePrecision] * N[(1.0 + N[(x * N[(x * 0.09375), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.4:\\
\;\;\;\;\sqrt{2}\\
\mathbf{elif}\;x \leq 1.66:\\
\;\;\;\;{\left(0.5 + x \cdot -0.25\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \left(1 + x \cdot \left(x \cdot 0.09375\right)\right)\\
\end{array}
\end{array}
if x < -4.4000000000000004Initial program 100.0%
*-commutative100.0%
exp-lft-sqr100.0%
difference-of-sqr-1100.0%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 20.7%
if -4.4000000000000004 < x < 1.65999999999999992Initial program 5.4%
*-commutative5.4%
exp-lft-sqr6.6%
difference-of-sqr-18.1%
associate-/l*8.1%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+6.6%
metadata-eval6.6%
exp-lft-sqr5.4%
*-commutative5.4%
expm1-udef7.8%
expm1-udef99.9%
div-inv99.6%
div-inv99.9%
clear-num99.9%
inv-pow99.9%
metadata-eval99.9%
pow-pow100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 99.5%
if 1.65999999999999992 < x Initial program 51.9%
*-commutative51.9%
exp-lft-sqr51.9%
difference-of-sqr-151.9%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+51.9%
metadata-eval51.9%
exp-lft-sqr51.9%
*-commutative51.9%
expm1-udef51.9%
expm1-udef51.9%
div-inv51.9%
div-inv51.9%
clear-num51.9%
inv-pow51.9%
metadata-eval51.9%
pow-pow51.9%
Applied egg-rr100.0%
Taylor expanded in x around 0 0.0%
Taylor expanded in x around 0 11.9%
*-rgt-identity11.9%
associate-*r*11.9%
associate-*r*11.9%
*-commutative11.9%
distribute-rgt-out11.9%
distribute-lft-out11.9%
+-commutative11.9%
*-commutative11.9%
unpow211.9%
associate-*r*11.9%
distribute-rgt-out11.9%
*-commutative11.9%
Simplified11.9%
Taylor expanded in x around inf 11.7%
*-commutative11.7%
Simplified11.7%
Final simplification73.6%
(FPCore (x)
:precision binary64
(if (<= x -4.4)
(sqrt 2.0)
(if (<= x 1.46)
(pow (+ 0.5 (* x -0.25)) -0.5)
(* (sqrt 2.0) (+ 1.0 (* x 0.25))))))
double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = sqrt(2.0);
} else if (x <= 1.46) {
tmp = pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = sqrt(2.0) * (1.0 + (x * 0.25));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-4.4d0)) then
tmp = sqrt(2.0d0)
else if (x <= 1.46d0) then
tmp = (0.5d0 + (x * (-0.25d0))) ** (-0.5d0)
else
tmp = sqrt(2.0d0) * (1.0d0 + (x * 0.25d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = Math.sqrt(2.0);
} else if (x <= 1.46) {
tmp = Math.pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = Math.sqrt(2.0) * (1.0 + (x * 0.25));
}
return tmp;
}
def code(x): tmp = 0 if x <= -4.4: tmp = math.sqrt(2.0) elif x <= 1.46: tmp = math.pow((0.5 + (x * -0.25)), -0.5) else: tmp = math.sqrt(2.0) * (1.0 + (x * 0.25)) return tmp
function code(x) tmp = 0.0 if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.46) tmp = Float64(0.5 + Float64(x * -0.25)) ^ -0.5; else tmp = Float64(sqrt(2.0) * Float64(1.0 + Float64(x * 0.25))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.46) tmp = (0.5 + (x * -0.25)) ^ -0.5; else tmp = sqrt(2.0) * (1.0 + (x * 0.25)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -4.4], N[Sqrt[2.0], $MachinePrecision], If[LessEqual[x, 1.46], N[Power[N[(0.5 + N[(x * -0.25), $MachinePrecision]), $MachinePrecision], -0.5], $MachinePrecision], N[(N[Sqrt[2.0], $MachinePrecision] * N[(1.0 + N[(x * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.4:\\
\;\;\;\;\sqrt{2}\\
\mathbf{elif}\;x \leq 1.46:\\
\;\;\;\;{\left(0.5 + x \cdot -0.25\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \left(1 + x \cdot 0.25\right)\\
\end{array}
\end{array}
if x < -4.4000000000000004Initial program 100.0%
*-commutative100.0%
exp-lft-sqr100.0%
difference-of-sqr-1100.0%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 20.7%
if -4.4000000000000004 < x < 1.46Initial program 5.4%
*-commutative5.4%
exp-lft-sqr6.6%
difference-of-sqr-18.1%
associate-/l*8.1%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+6.6%
metadata-eval6.6%
exp-lft-sqr5.4%
*-commutative5.4%
expm1-udef7.8%
expm1-udef99.9%
div-inv99.6%
div-inv99.9%
clear-num99.9%
inv-pow99.9%
metadata-eval99.9%
pow-pow100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 99.5%
if 1.46 < x Initial program 51.9%
*-commutative51.9%
exp-lft-sqr51.9%
difference-of-sqr-151.9%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+51.9%
metadata-eval51.9%
exp-lft-sqr51.9%
*-commutative51.9%
expm1-udef51.9%
expm1-udef51.9%
div-inv51.9%
div-inv51.9%
clear-num51.9%
inv-pow51.9%
metadata-eval51.9%
pow-pow51.9%
Applied egg-rr100.0%
Taylor expanded in x around 0 11.2%
associate-*r*11.2%
distribute-rgt1-in11.2%
*-commutative11.2%
Simplified11.2%
Final simplification73.6%
(FPCore (x) :precision binary64 (if (<= x -4.4) (sqrt 2.0) (if (<= x 1.55) (pow (+ 0.5 (* x -0.25)) -0.5) (sqrt (+ x 2.0)))))
double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = sqrt(2.0);
} else if (x <= 1.55) {
tmp = pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = sqrt((x + 2.0));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-4.4d0)) then
tmp = sqrt(2.0d0)
else if (x <= 1.55d0) then
tmp = (0.5d0 + (x * (-0.25d0))) ** (-0.5d0)
else
tmp = sqrt((x + 2.0d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = Math.sqrt(2.0);
} else if (x <= 1.55) {
tmp = Math.pow((0.5 + (x * -0.25)), -0.5);
} else {
tmp = Math.sqrt((x + 2.0));
}
return tmp;
}
def code(x): tmp = 0 if x <= -4.4: tmp = math.sqrt(2.0) elif x <= 1.55: tmp = math.pow((0.5 + (x * -0.25)), -0.5) else: tmp = math.sqrt((x + 2.0)) return tmp
function code(x) tmp = 0.0 if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.55) tmp = Float64(0.5 + Float64(x * -0.25)) ^ -0.5; else tmp = sqrt(Float64(x + 2.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -4.4) tmp = sqrt(2.0); elseif (x <= 1.55) tmp = (0.5 + (x * -0.25)) ^ -0.5; else tmp = sqrt((x + 2.0)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -4.4], N[Sqrt[2.0], $MachinePrecision], If[LessEqual[x, 1.55], N[Power[N[(0.5 + N[(x * -0.25), $MachinePrecision]), $MachinePrecision], -0.5], $MachinePrecision], N[Sqrt[N[(x + 2.0), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.4:\\
\;\;\;\;\sqrt{2}\\
\mathbf{elif}\;x \leq 1.55:\\
\;\;\;\;{\left(0.5 + x \cdot -0.25\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{x + 2}\\
\end{array}
\end{array}
if x < -4.4000000000000004Initial program 100.0%
*-commutative100.0%
exp-lft-sqr100.0%
difference-of-sqr-1100.0%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 20.7%
if -4.4000000000000004 < x < 1.55000000000000004Initial program 5.4%
*-commutative5.4%
exp-lft-sqr6.6%
difference-of-sqr-18.1%
associate-/l*8.1%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+6.6%
metadata-eval6.6%
exp-lft-sqr5.4%
*-commutative5.4%
expm1-udef7.8%
expm1-udef99.9%
div-inv99.6%
div-inv99.9%
clear-num99.9%
inv-pow99.9%
metadata-eval99.9%
pow-pow100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 99.5%
if 1.55000000000000004 < x Initial program 51.9%
*-commutative51.9%
exp-lft-sqr51.9%
difference-of-sqr-151.9%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 11.0%
Final simplification73.6%
(FPCore (x) :precision binary64 (if (<= x -2.7) (sqrt 2.0) (* (sqrt 2.0) (+ 1.0 (* x (+ 0.25 (* x 0.09375)))))))
double code(double x) {
double tmp;
if (x <= -2.7) {
tmp = sqrt(2.0);
} else {
tmp = sqrt(2.0) * (1.0 + (x * (0.25 + (x * 0.09375))));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-2.7d0)) then
tmp = sqrt(2.0d0)
else
tmp = sqrt(2.0d0) * (1.0d0 + (x * (0.25d0 + (x * 0.09375d0))))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -2.7) {
tmp = Math.sqrt(2.0);
} else {
tmp = Math.sqrt(2.0) * (1.0 + (x * (0.25 + (x * 0.09375))));
}
return tmp;
}
def code(x): tmp = 0 if x <= -2.7: tmp = math.sqrt(2.0) else: tmp = math.sqrt(2.0) * (1.0 + (x * (0.25 + (x * 0.09375)))) return tmp
function code(x) tmp = 0.0 if (x <= -2.7) tmp = sqrt(2.0); else tmp = Float64(sqrt(2.0) * Float64(1.0 + Float64(x * Float64(0.25 + Float64(x * 0.09375))))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -2.7) tmp = sqrt(2.0); else tmp = sqrt(2.0) * (1.0 + (x * (0.25 + (x * 0.09375)))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -2.7], N[Sqrt[2.0], $MachinePrecision], N[(N[Sqrt[2.0], $MachinePrecision] * N[(1.0 + N[(x * N[(0.25 + N[(x * 0.09375), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.7:\\
\;\;\;\;\sqrt{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \left(1 + x \cdot \left(0.25 + x \cdot 0.09375\right)\right)\\
\end{array}
\end{array}
if x < -2.7000000000000002Initial program 100.0%
*-commutative100.0%
exp-lft-sqr100.0%
difference-of-sqr-1100.0%
associate-/l*100.0%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 20.7%
if -2.7000000000000002 < x Initial program 6.0%
*-commutative6.0%
exp-lft-sqr7.2%
difference-of-sqr-18.6%
associate-/l*9.2%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
pow1/2100.0%
+-commutative100.0%
flip-+7.2%
metadata-eval7.2%
exp-lft-sqr6.0%
*-commutative6.0%
expm1-udef8.3%
expm1-udef99.4%
div-inv99.1%
div-inv99.4%
clear-num99.4%
inv-pow99.4%
metadata-eval99.4%
pow-pow99.4%
Applied egg-rr100.0%
Taylor expanded in x around 0 98.4%
Taylor expanded in x around 0 98.5%
*-rgt-identity98.5%
associate-*r*98.5%
associate-*r*98.5%
*-commutative98.5%
distribute-rgt-out98.5%
distribute-lft-out98.5%
+-commutative98.5%
*-commutative98.5%
unpow298.5%
associate-*r*98.5%
distribute-rgt-out98.5%
*-commutative98.5%
Simplified98.5%
Final simplification73.6%
(FPCore (x) :precision binary64 (sqrt 2.0))
double code(double x) {
return sqrt(2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(2.0d0)
end function
public static double code(double x) {
return Math.sqrt(2.0);
}
def code(x): return math.sqrt(2.0)
function code(x) return sqrt(2.0) end
function tmp = code(x) tmp = sqrt(2.0); end
code[x_] := N[Sqrt[2.0], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{2}
\end{array}
Initial program 36.1%
*-commutative36.1%
exp-lft-sqr36.9%
difference-of-sqr-137.9%
associate-/l*38.3%
*-inverses100.0%
/-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 72.0%
Final simplification72.0%
herbie shell --seed 2024018
(FPCore (x)
:name "sqrtexp (problem 3.4.4)"
:precision binary64
(sqrt (/ (- (exp (* 2.0 x)) 1.0) (- (exp x) 1.0))))