inverse-esp

Percentage Accurate: 99.8% → 99.8%
Time: 1.7min
Alternatives: 6
Speedup: 1.0×

Specification

?
\[ecc > 0 \land ecc < 1\]
\[ecc \cdot \sin phip \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (* ecc (sin phip)))
double code(double phip, double ecc) {
	return ecc * sin(phip);
}
real(8) function code(phip, ecc)
use fmin_fmax_functions
    real(8), intent (in) :: phip
    real(8), intent (in) :: ecc
    code = ecc * sin(phip)
end function
public static double code(double phip, double ecc) {
	return ecc * Math.sin(phip);
}
def code(phip, ecc):
	return ecc * math.sin(phip)
function code(phip, ecc)
	return Float64(ecc * sin(phip))
end
function tmp = code(phip, ecc)
	tmp = ecc * sin(phip);
end
code[phip_, ecc_] := N[(ecc * N[Sin[phip], $MachinePrecision]), $MachinePrecision]
f(phip, ecc):
	phip in [-inf, +inf],
	ecc in [0, 1]
code: THEORY
BEGIN
f(phip, ecc: real): real =
	ecc * (sin(phip))
END code
ecc \cdot \sin phip

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

\[ecc > 0 \land ecc < 1\]
\[ecc \cdot \sin phip \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (* ecc (sin phip)))
double code(double phip, double ecc) {
	return ecc * sin(phip);
}
real(8) function code(phip, ecc)
use fmin_fmax_functions
    real(8), intent (in) :: phip
    real(8), intent (in) :: ecc
    code = ecc * sin(phip)
end function
public static double code(double phip, double ecc) {
	return ecc * Math.sin(phip);
}
def code(phip, ecc):
	return ecc * math.sin(phip)
function code(phip, ecc)
	return Float64(ecc * sin(phip))
end
function tmp = code(phip, ecc)
	tmp = ecc * sin(phip);
end
code[phip_, ecc_] := N[(ecc * N[Sin[phip], $MachinePrecision]), $MachinePrecision]
f(phip, ecc):
	phip in [-inf, +inf],
	ecc in [0, 1]
code: THEORY
BEGIN
f(phip, ecc: real): real =
	ecc * (sin(phip))
END code
ecc \cdot \sin phip

Alternative 1: 60.0% accurate, 0.4× speedup?

\[ecc > 0 \land ecc < 1\]
\[\begin{array}{l} t_0 := \sin \left(\left|phip\right|\right)\\ \mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -0.02:\\ \;\;\;\;-ecc\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{-7}:\\ \;\;\;\;ecc \cdot \left|phip\right|\\ \mathbf{else}:\\ \;\;\;\;ecc\\ \end{array} \end{array} \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (let* ((t_0 (sin (fabs phip))))
  (*
   (copysign 1.0 phip)
   (if (<= t_0 -0.02)
     (- ecc)
     (if (<= t_0 4e-7) (* ecc (fabs phip)) ecc)))))
double code(double phip, double ecc) {
	double t_0 = sin(fabs(phip));
	double tmp;
	if (t_0 <= -0.02) {
		tmp = -ecc;
	} else if (t_0 <= 4e-7) {
		tmp = ecc * fabs(phip);
	} else {
		tmp = ecc;
	}
	return copysign(1.0, phip) * tmp;
}
public static double code(double phip, double ecc) {
	double t_0 = Math.sin(Math.abs(phip));
	double tmp;
	if (t_0 <= -0.02) {
		tmp = -ecc;
	} else if (t_0 <= 4e-7) {
		tmp = ecc * Math.abs(phip);
	} else {
		tmp = ecc;
	}
	return Math.copySign(1.0, phip) * tmp;
}
def code(phip, ecc):
	t_0 = math.sin(math.fabs(phip))
	tmp = 0
	if t_0 <= -0.02:
		tmp = -ecc
	elif t_0 <= 4e-7:
		tmp = ecc * math.fabs(phip)
	else:
		tmp = ecc
	return math.copysign(1.0, phip) * tmp
function code(phip, ecc)
	t_0 = sin(abs(phip))
	tmp = 0.0
	if (t_0 <= -0.02)
		tmp = Float64(-ecc);
	elseif (t_0 <= 4e-7)
		tmp = Float64(ecc * abs(phip));
	else
		tmp = ecc;
	end
	return Float64(copysign(1.0, phip) * tmp)
end
function tmp_2 = code(phip, ecc)
	t_0 = sin(abs(phip));
	tmp = 0.0;
	if (t_0 <= -0.02)
		tmp = -ecc;
	elseif (t_0 <= 4e-7)
		tmp = ecc * abs(phip);
	else
		tmp = ecc;
	end
	tmp_2 = (sign(phip) * abs(1.0)) * tmp;
end
code[phip_, ecc_] := Block[{t$95$0 = N[Sin[N[Abs[phip], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[phip]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[t$95$0, -0.02], (-ecc), If[LessEqual[t$95$0, 4e-7], N[(ecc * N[Abs[phip], $MachinePrecision]), $MachinePrecision], ecc]]), $MachinePrecision]]
\begin{array}{l}
t_0 := \sin \left(\left|phip\right|\right)\\
\mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq -0.02:\\
\;\;\;\;-ecc\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{-7}:\\
\;\;\;\;ecc \cdot \left|phip\right|\\

\mathbf{else}:\\
\;\;\;\;ecc\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (sin.f64 phip) < -0.02

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites9.2%

      \[\leadsto ecc \cdot -1 \]
    6. Applied rewrites9.2%

      \[\leadsto -ecc \]

    if -0.02 < (sin.f64 phip) < 3.9999999999999998e-7

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]

    if 3.9999999999999998e-7 < (sin.f64 phip)

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites9.1%

      \[\leadsto ecc \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 2: 36.1% accurate, 0.5× speedup?

\[ecc > 0 \land ecc < 1\]
\[\begin{array}{l} t_0 := \sin \left(\left|phip\right|\right)\\ \mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -0.02:\\ \;\;\;\;-ecc\\ \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{-28}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;ecc\\ \end{array} \end{array} \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (let* ((t_0 (sin (fabs phip))))
  (*
   (copysign 1.0 phip)
   (if (<= t_0 -0.02) (- ecc) (if (<= t_0 2e-28) 0.0 ecc)))))
double code(double phip, double ecc) {
	double t_0 = sin(fabs(phip));
	double tmp;
	if (t_0 <= -0.02) {
		tmp = -ecc;
	} else if (t_0 <= 2e-28) {
		tmp = 0.0;
	} else {
		tmp = ecc;
	}
	return copysign(1.0, phip) * tmp;
}
public static double code(double phip, double ecc) {
	double t_0 = Math.sin(Math.abs(phip));
	double tmp;
	if (t_0 <= -0.02) {
		tmp = -ecc;
	} else if (t_0 <= 2e-28) {
		tmp = 0.0;
	} else {
		tmp = ecc;
	}
	return Math.copySign(1.0, phip) * tmp;
}
def code(phip, ecc):
	t_0 = math.sin(math.fabs(phip))
	tmp = 0
	if t_0 <= -0.02:
		tmp = -ecc
	elif t_0 <= 2e-28:
		tmp = 0.0
	else:
		tmp = ecc
	return math.copysign(1.0, phip) * tmp
function code(phip, ecc)
	t_0 = sin(abs(phip))
	tmp = 0.0
	if (t_0 <= -0.02)
		tmp = Float64(-ecc);
	elseif (t_0 <= 2e-28)
		tmp = 0.0;
	else
		tmp = ecc;
	end
	return Float64(copysign(1.0, phip) * tmp)
end
function tmp_2 = code(phip, ecc)
	t_0 = sin(abs(phip));
	tmp = 0.0;
	if (t_0 <= -0.02)
		tmp = -ecc;
	elseif (t_0 <= 2e-28)
		tmp = 0.0;
	else
		tmp = ecc;
	end
	tmp_2 = (sign(phip) * abs(1.0)) * tmp;
end
code[phip_, ecc_] := Block[{t$95$0 = N[Sin[N[Abs[phip], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[phip]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[t$95$0, -0.02], (-ecc), If[LessEqual[t$95$0, 2e-28], 0.0, ecc]]), $MachinePrecision]]
\begin{array}{l}
t_0 := \sin \left(\left|phip\right|\right)\\
\mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq -0.02:\\
\;\;\;\;-ecc\\

\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{-28}:\\
\;\;\;\;0\\

\mathbf{else}:\\
\;\;\;\;ecc\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (sin.f64 phip) < -0.02

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites9.2%

      \[\leadsto ecc \cdot -1 \]
    6. Applied rewrites9.2%

      \[\leadsto -ecc \]

    if -0.02 < (sin.f64 phip) < 1.9999999999999999e-28

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites28.1%

      \[\leadsto 0 \]

    if 1.9999999999999999e-28 < (sin.f64 phip)

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites9.1%

      \[\leadsto ecc \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 32.3% accurate, 0.8× speedup?

\[ecc > 0 \land ecc < 1\]
\[\mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l} \mathbf{if}\;\sin \left(\left|phip\right|\right) \leq 2 \cdot 10^{-28}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;ecc\\ \end{array} \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (* (copysign 1.0 phip) (if (<= (sin (fabs phip)) 2e-28) 0.0 ecc)))
double code(double phip, double ecc) {
	double tmp;
	if (sin(fabs(phip)) <= 2e-28) {
		tmp = 0.0;
	} else {
		tmp = ecc;
	}
	return copysign(1.0, phip) * tmp;
}
public static double code(double phip, double ecc) {
	double tmp;
	if (Math.sin(Math.abs(phip)) <= 2e-28) {
		tmp = 0.0;
	} else {
		tmp = ecc;
	}
	return Math.copySign(1.0, phip) * tmp;
}
def code(phip, ecc):
	tmp = 0
	if math.sin(math.fabs(phip)) <= 2e-28:
		tmp = 0.0
	else:
		tmp = ecc
	return math.copysign(1.0, phip) * tmp
function code(phip, ecc)
	tmp = 0.0
	if (sin(abs(phip)) <= 2e-28)
		tmp = 0.0;
	else
		tmp = ecc;
	end
	return Float64(copysign(1.0, phip) * tmp)
end
function tmp_2 = code(phip, ecc)
	tmp = 0.0;
	if (sin(abs(phip)) <= 2e-28)
		tmp = 0.0;
	else
		tmp = ecc;
	end
	tmp_2 = (sign(phip) * abs(1.0)) * tmp;
end
code[phip_, ecc_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[phip]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Sin[N[Abs[phip], $MachinePrecision]], $MachinePrecision], 2e-28], 0.0, ecc]), $MachinePrecision]
\mathsf{copysign}\left(1, phip\right) \cdot \begin{array}{l}
\mathbf{if}\;\sin \left(\left|phip\right|\right) \leq 2 \cdot 10^{-28}:\\
\;\;\;\;0\\

\mathbf{else}:\\
\;\;\;\;ecc\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sin.f64 phip) < 1.9999999999999999e-28

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites28.1%

      \[\leadsto 0 \]

    if 1.9999999999999999e-28 < (sin.f64 phip)

    1. Initial program 99.8%

      \[ecc \cdot \sin phip \]
    2. Taylor expanded in phip around 0

      \[\leadsto ecc \cdot phip \]
    3. Applied rewrites51.7%

      \[\leadsto ecc \cdot phip \]
    4. Applied rewrites51.7%

      \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
    5. Applied rewrites9.1%

      \[\leadsto ecc \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 28.1% accurate, 37.6× speedup?

\[ecc > 0 \land ecc < 1\]
\[0 \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  0.0)
double code(double phip, double ecc) {
	return 0.0;
}
real(8) function code(phip, ecc)
use fmin_fmax_functions
    real(8), intent (in) :: phip
    real(8), intent (in) :: ecc
    code = 0.0d0
end function
public static double code(double phip, double ecc) {
	return 0.0;
}
def code(phip, ecc):
	return 0.0
function code(phip, ecc)
	return 0.0
end
function tmp = code(phip, ecc)
	tmp = 0.0;
end
code[phip_, ecc_] := 0.0
f(phip, ecc):
	phip in [-inf, +inf],
	ecc in [0, 1]
code: THEORY
BEGIN
f(phip, ecc: real): real =
	0
END code
0
Derivation
  1. Initial program 99.8%

    \[ecc \cdot \sin phip \]
  2. Taylor expanded in phip around 0

    \[\leadsto ecc \cdot phip \]
  3. Applied rewrites51.7%

    \[\leadsto ecc \cdot phip \]
  4. Applied rewrites51.7%

    \[\leadsto ecc \cdot \frac{1}{\frac{2}{phip + phip}} \]
  5. Applied rewrites28.1%

    \[\leadsto 0 \]
  6. Add Preprocessing

Alternative 5: 3.6% accurate, 37.6× speedup?

\[ecc > 0 \land ecc < 1\]
\[-0.08333333333333333 \]
(FPCore (phip ecc)
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  -0.08333333333333333)
double code(double phip, double ecc) {
	return -0.08333333333333333;
}
real(8) function code(phip, ecc)
use fmin_fmax_functions
    real(8), intent (in) :: phip
    real(8), intent (in) :: ecc
    code = -0.08333333333333333d0
end function
public static double code(double phip, double ecc) {
	return -0.08333333333333333;
}
def code(phip, ecc):
	return -0.08333333333333333
function code(phip, ecc)
	return -0.08333333333333333
end
function tmp = code(phip, ecc)
	tmp = -0.08333333333333333;
end
code[phip_, ecc_] := -0.08333333333333333
f(phip, ecc):
	phip in [-inf, +inf],
	ecc in [0, 1]
code: THEORY
BEGIN
f(phip, ecc: real): real =
	-8333333333333332870740406406184774823486804962158203125e-56
END code
-0.08333333333333333
Derivation
  1. Initial program 99.8%

    \[ecc \cdot \sin phip \]
  2. Taylor expanded in phip around 0

    \[\leadsto phip \cdot \left(ecc + \frac{-1}{6} \cdot \left(ecc \cdot {phip}^{2}\right)\right) \]
  3. Applied rewrites51.2%

    \[\leadsto phip \cdot \left(ecc + -0.16666666666666666 \cdot \left(ecc \cdot {phip}^{2}\right)\right) \]
  4. Applied rewrites51.2%

    \[\leadsto \mathsf{fma}\left(\left(-0.16666666666666666 \cdot phip\right) \cdot ecc, phip \cdot phip, ecc \cdot phip\right) \]
  5. Applied rewrites3.6%

    \[\leadsto -0.08333333333333333 \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2026050 +o generate:egglog
(FPCore (phip ecc)
  :name "inverse-esp"
  :precision binary64
  :pre (and (> ecc 0.0) (< ecc 1.0))
  (* ecc (sin phip)))