Hyperbolic sine

Percentage Accurate: 53.6% → 100.0%
Time: 1.7min
Alternatives: 4
Speedup: 2.0×

Specification

?
\[\frac{e^{x} - e^{-x}}{2} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ (- (exp x) (exp (- x))) 2.0))
double code(double x) {
	return (exp(x) - exp(-x)) / 2.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (exp(x) - exp(-x)) / 2.0d0
end function
public static double code(double x) {
	return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
def code(x):
	return (math.exp(x) - math.exp(-x)) / 2.0
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / 2.0)
end
function tmp = code(x)
	tmp = (exp(x) - exp(-x)) / 2.0;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((exp(x)) - (exp((- x)))) / (2)
END code
\frac{e^{x} - e^{-x}}{2}

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

\[\frac{e^{x} - e^{-x}}{2} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ (- (exp x) (exp (- x))) 2.0))
double code(double x) {
	return (exp(x) - exp(-x)) / 2.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (exp(x) - exp(-x)) / 2.0d0
end function
public static double code(double x) {
	return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
def code(x):
	return (math.exp(x) - math.exp(-x)) / 2.0
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / 2.0)
end
function tmp = code(x)
	tmp = (exp(x) - exp(-x)) / 2.0;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((exp(x)) - (exp((- x)))) / (2)
END code
\frac{e^{x} - e^{-x}}{2}

Alternative 1: 100.0% accurate, 2.0× speedup?

\[\sinh x \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (sinh x))
double code(double x) {
	return sinh(x);
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = sinh(x)
end function
public static double code(double x) {
	return Math.sinh(x);
}
def code(x):
	return math.sinh(x)
function code(x)
	return sinh(x)
end
function tmp = code(x)
	tmp = sinh(x);
end
code[x_] := N[Sinh[x], $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((1) / (2)) * ((exp(x)) + ((- (1)) / (exp(x))))
END code
\sinh x
Derivation
  1. Initial program 53.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Applied rewrites100.0%

    \[\leadsto \sinh x \]
  3. Add Preprocessing

Alternative 2: 98.8% accurate, 1.7× speedup?

\[\mathsf{copysign}\left(1, x\right) \cdot \left(1 \cdot \mathsf{expm1}\left(\left|x\right|\right)\right) \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (* (copysign 1.0 x) (* 1.0 (expm1 (fabs x)))))
double code(double x) {
	return copysign(1.0, x) * (1.0 * expm1(fabs(x)));
}
public static double code(double x) {
	return Math.copySign(1.0, x) * (1.0 * Math.expm1(Math.abs(x)));
}
def code(x):
	return math.copysign(1.0, x) * (1.0 * math.expm1(math.fabs(x)))
function code(x)
	return Float64(copysign(1.0, x) * Float64(1.0 * expm1(abs(x))))
end
code[x_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(1.0 * N[(Exp[N[Abs[x], $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \left(1 \cdot \mathsf{expm1}\left(\left|x\right|\right)\right)
Derivation
  1. Initial program 53.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Applied rewrites75.3%

    \[\leadsto \frac{e^{x} - -1}{e^{x} \cdot 2} \cdot \mathsf{expm1}\left(x\right) \]
  3. Taylor expanded in x around 0

    \[\leadsto \left(1 + x \cdot \left(\frac{1}{4} \cdot x - \frac{1}{2}\right)\right) \cdot \mathsf{expm1}\left(x\right) \]
  4. Applied rewrites87.6%

    \[\leadsto \left(1 + x \cdot \left(0.25 \cdot x - 0.5\right)\right) \cdot \mathsf{expm1}\left(x\right) \]
  5. Taylor expanded in x around 0

    \[\leadsto 1 \cdot \mathsf{expm1}\left(x\right) \]
  6. Applied rewrites75.1%

    \[\leadsto 1 \cdot \mathsf{expm1}\left(x\right) \]
  7. Add Preprocessing

Alternative 3: 52.8% accurate, 4.1× speedup?

\[\frac{x + x}{2} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ (+ x x) 2.0))
double code(double x) {
	return (x + x) / 2.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (x + x) / 2.0d0
end function
public static double code(double x) {
	return (x + x) / 2.0;
}
def code(x):
	return (x + x) / 2.0
function code(x)
	return Float64(Float64(x + x) / 2.0)
end
function tmp = code(x)
	tmp = (x + x) / 2.0;
end
code[x_] := N[(N[(x + x), $MachinePrecision] / 2.0), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(x + x) / (2)
END code
\frac{x + x}{2}
Derivation
  1. Initial program 53.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Taylor expanded in x around 0

    \[\leadsto \frac{2 \cdot x}{2} \]
  3. Applied rewrites52.8%

    \[\leadsto \frac{2 \cdot x}{2} \]
  4. Applied rewrites52.8%

    \[\leadsto \frac{x + x}{2} \]
  5. Add Preprocessing

Alternative 4: 3.5% accurate, 7.6× speedup?

\[1 \cdot 0 \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (* 1.0 0.0))
double code(double x) {
	return 1.0 * 0.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = 1.0d0 * 0.0d0
end function
public static double code(double x) {
	return 1.0 * 0.0;
}
def code(x):
	return 1.0 * 0.0
function code(x)
	return Float64(1.0 * 0.0)
end
function tmp = code(x)
	tmp = 1.0 * 0.0;
end
code[x_] := N[(1.0 * 0.0), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(1) * (0)
END code
1 \cdot 0
Derivation
  1. Initial program 53.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Applied rewrites75.3%

    \[\leadsto \frac{e^{x} - -1}{e^{x} \cdot 2} \cdot \mathsf{expm1}\left(x\right) \]
  3. Taylor expanded in x around 0

    \[\leadsto \left(1 + x \cdot \left(\frac{1}{4} \cdot x - \frac{1}{2}\right)\right) \cdot \mathsf{expm1}\left(x\right) \]
  4. Applied rewrites87.6%

    \[\leadsto \left(1 + x \cdot \left(0.25 \cdot x - 0.5\right)\right) \cdot \mathsf{expm1}\left(x\right) \]
  5. Taylor expanded in x around 0

    \[\leadsto 1 \cdot \mathsf{expm1}\left(x\right) \]
  6. Applied rewrites75.1%

    \[\leadsto 1 \cdot \mathsf{expm1}\left(x\right) \]
  7. Applied rewrites3.5%

    \[\leadsto 1 \cdot 0 \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2026074 +o generate:egglog
(FPCore (x)
  :name "Hyperbolic sine"
  :precision binary64
  (/ (- (exp x) (exp (- x))) 2.0))