ab-angle->ABCF D

Percentage Accurate: 82.4% → 99.5%
Time: 4.4s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ -\left(\left(a \cdot a\right) \cdot b\right) \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* (* (* a a) b) b)))
double code(double a, double b) {
	return -(((a * a) * b) * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = -(((a * a) * b) * b)
end function
public static double code(double a, double b) {
	return -(((a * a) * b) * b);
}
def code(a, b):
	return -(((a * a) * b) * b)
function code(a, b)
	return Float64(-Float64(Float64(Float64(a * a) * b) * b))
end
function tmp = code(a, b)
	tmp = -(((a * a) * b) * b);
end
code[a_, b_] := (-N[(N[(N[(a * a), $MachinePrecision] * b), $MachinePrecision] * b), $MachinePrecision])
\begin{array}{l}

\\
-\left(\left(a \cdot a\right) \cdot b\right) \cdot b
\end{array}

Sampling outcomes in binary64 precision:

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

\[\begin{array}{l} \\ -\left(\left(a \cdot a\right) \cdot b\right) \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* (* (* a a) b) b)))
double code(double a, double b) {
	return -(((a * a) * b) * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = -(((a * a) * b) * b)
end function
public static double code(double a, double b) {
	return -(((a * a) * b) * b);
}
def code(a, b):
	return -(((a * a) * b) * b)
function code(a, b)
	return Float64(-Float64(Float64(Float64(a * a) * b) * b))
end
function tmp = code(a, b)
	tmp = -(((a * a) * b) * b);
end
code[a_, b_] := (-N[(N[(N[(a * a), $MachinePrecision] * b), $MachinePrecision] * b), $MachinePrecision])
\begin{array}{l}

\\
-\left(\left(a \cdot a\right) \cdot b\right) \cdot b
\end{array}

Alternative 1: 99.5% accurate, 0.0× speedup?

\[\begin{array}{l} b_m = \left|b\right| \\ [a, b_m] = \mathsf{sort}([a, b_m])\\ \\ \begin{array}{l} t_0 := a \cdot \sqrt{b\_m}\\ t\_0 \cdot \left(b\_m \cdot \left(-t\_0\right)\right) \end{array} \end{array} \]
b_m = (fabs.f64 b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
(FPCore (a b_m)
 :precision binary64
 (let* ((t_0 (* a (sqrt b_m)))) (* t_0 (* b_m (- t_0)))))
b_m = fabs(b);
assert(a < b_m);
double code(double a, double b_m) {
	double t_0 = a * sqrt(b_m);
	return t_0 * (b_m * -t_0);
}
b_m = abs(b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
real(8) function code(a, b_m)
    real(8), intent (in) :: a
    real(8), intent (in) :: b_m
    real(8) :: t_0
    t_0 = a * sqrt(b_m)
    code = t_0 * (b_m * -t_0)
end function
b_m = Math.abs(b);
assert a < b_m;
public static double code(double a, double b_m) {
	double t_0 = a * Math.sqrt(b_m);
	return t_0 * (b_m * -t_0);
}
b_m = math.fabs(b)
[a, b_m] = sort([a, b_m])
def code(a, b_m):
	t_0 = a * math.sqrt(b_m)
	return t_0 * (b_m * -t_0)
b_m = abs(b)
a, b_m = sort([a, b_m])
function code(a, b_m)
	t_0 = Float64(a * sqrt(b_m))
	return Float64(t_0 * Float64(b_m * Float64(-t_0)))
end
b_m = abs(b);
a, b_m = num2cell(sort([a, b_m])){:}
function tmp = code(a, b_m)
	t_0 = a * sqrt(b_m);
	tmp = t_0 * (b_m * -t_0);
end
b_m = N[Abs[b], $MachinePrecision]
NOTE: a and b_m should be sorted in increasing order before calling this function.
code[a_, b$95$m_] := Block[{t$95$0 = N[(a * N[Sqrt[b$95$m], $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(b$95$m * (-t$95$0)), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
b_m = \left|b\right|
\\
[a, b_m] = \mathsf{sort}([a, b_m])\\
\\
\begin{array}{l}
t_0 := a \cdot \sqrt{b\_m}\\
t\_0 \cdot \left(b\_m \cdot \left(-t\_0\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 79.7%

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-cube-cbrt79.4%

      \[\leadsto -\color{blue}{\left(\sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b} \cdot \sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right) \cdot \sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}} \]
    2. pow379.4%

      \[\leadsto -\color{blue}{{\left(\sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right)}^{3}} \]
    3. associate-*l*76.7%

      \[\leadsto -{\left(\sqrt[3]{\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)}}\right)}^{3} \]
    4. swap-sqr99.1%

      \[\leadsto -{\left(\sqrt[3]{\color{blue}{\left(a \cdot b\right) \cdot \left(a \cdot b\right)}}\right)}^{3} \]
    5. cbrt-unprod98.7%

      \[\leadsto -{\color{blue}{\left(\sqrt[3]{a \cdot b} \cdot \sqrt[3]{a \cdot b}\right)}}^{3} \]
    6. pow298.7%

      \[\leadsto -{\color{blue}{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}}^{3} \]
  4. Applied egg-rr98.7%

    \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}^{3}} \]
  5. Step-by-step derivation
    1. unpow398.7%

      \[\leadsto -\color{blue}{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2} \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2}\right) \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2}} \]
    2. pow298.7%

      \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}^{2}} \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2} \]
    3. unpow-prod-down98.7%

      \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2} \cdot \sqrt[3]{a \cdot b}\right)}^{2}} \]
    4. unpow298.7%

      \[\leadsto -{\left(\color{blue}{\left(\sqrt[3]{a \cdot b} \cdot \sqrt[3]{a \cdot b}\right)} \cdot \sqrt[3]{a \cdot b}\right)}^{2} \]
    5. add-cube-cbrt99.7%

      \[\leadsto -{\color{blue}{\left(a \cdot b\right)}}^{2} \]
    6. unpow-prod-down77.0%

      \[\leadsto -\color{blue}{{a}^{2} \cdot {b}^{2}} \]
    7. unpow277.0%

      \[\leadsto -{a}^{2} \cdot \color{blue}{\left(b \cdot b\right)} \]
    8. associate-*r*79.7%

      \[\leadsto -\color{blue}{\left({a}^{2} \cdot b\right) \cdot b} \]
    9. add-sqr-sqrt45.3%

      \[\leadsto -\left({a}^{2} \cdot \color{blue}{\left(\sqrt{b} \cdot \sqrt{b}\right)}\right) \cdot b \]
    10. pow245.3%

      \[\leadsto -\left({a}^{2} \cdot \color{blue}{{\left(\sqrt{b}\right)}^{2}}\right) \cdot b \]
    11. pow-prod-down53.5%

      \[\leadsto -\color{blue}{{\left(a \cdot \sqrt{b}\right)}^{2}} \cdot b \]
    12. *-commutative53.5%

      \[\leadsto -\color{blue}{b \cdot {\left(a \cdot \sqrt{b}\right)}^{2}} \]
    13. unpow253.5%

      \[\leadsto -b \cdot \color{blue}{\left(\left(a \cdot \sqrt{b}\right) \cdot \left(a \cdot \sqrt{b}\right)\right)} \]
    14. associate-*r*56.4%

      \[\leadsto -\color{blue}{\left(b \cdot \left(a \cdot \sqrt{b}\right)\right) \cdot \left(a \cdot \sqrt{b}\right)} \]
  6. Applied egg-rr56.4%

    \[\leadsto -\color{blue}{\left(b \cdot \left(a \cdot \sqrt{b}\right)\right) \cdot \left(a \cdot \sqrt{b}\right)} \]
  7. Final simplification56.4%

    \[\leadsto \left(a \cdot \sqrt{b}\right) \cdot \left(b \cdot \left(-a \cdot \sqrt{b}\right)\right) \]
  8. Add Preprocessing

Alternative 2: 50.1% accurate, 0.0× speedup?

\[\begin{array}{l} b_m = \left|b\right| \\ [a, b_m] = \mathsf{sort}([a, b_m])\\ \\ \begin{array}{l} t_0 := b\_m \cdot \sqrt{a}\\ -t\_0 \cdot \left(a \cdot t\_0\right) \end{array} \end{array} \]
b_m = (fabs.f64 b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
(FPCore (a b_m)
 :precision binary64
 (let* ((t_0 (* b_m (sqrt a)))) (- (* t_0 (* a t_0)))))
b_m = fabs(b);
assert(a < b_m);
double code(double a, double b_m) {
	double t_0 = b_m * sqrt(a);
	return -(t_0 * (a * t_0));
}
b_m = abs(b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
real(8) function code(a, b_m)
    real(8), intent (in) :: a
    real(8), intent (in) :: b_m
    real(8) :: t_0
    t_0 = b_m * sqrt(a)
    code = -(t_0 * (a * t_0))
end function
b_m = Math.abs(b);
assert a < b_m;
public static double code(double a, double b_m) {
	double t_0 = b_m * Math.sqrt(a);
	return -(t_0 * (a * t_0));
}
b_m = math.fabs(b)
[a, b_m] = sort([a, b_m])
def code(a, b_m):
	t_0 = b_m * math.sqrt(a)
	return -(t_0 * (a * t_0))
b_m = abs(b)
a, b_m = sort([a, b_m])
function code(a, b_m)
	t_0 = Float64(b_m * sqrt(a))
	return Float64(-Float64(t_0 * Float64(a * t_0)))
end
b_m = abs(b);
a, b_m = num2cell(sort([a, b_m])){:}
function tmp = code(a, b_m)
	t_0 = b_m * sqrt(a);
	tmp = -(t_0 * (a * t_0));
end
b_m = N[Abs[b], $MachinePrecision]
NOTE: a and b_m should be sorted in increasing order before calling this function.
code[a_, b$95$m_] := Block[{t$95$0 = N[(b$95$m * N[Sqrt[a], $MachinePrecision]), $MachinePrecision]}, (-N[(t$95$0 * N[(a * t$95$0), $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}
b_m = \left|b\right|
\\
[a, b_m] = \mathsf{sort}([a, b_m])\\
\\
\begin{array}{l}
t_0 := b\_m \cdot \sqrt{a}\\
-t\_0 \cdot \left(a \cdot t\_0\right)
\end{array}
\end{array}
Derivation
  1. Initial program 79.7%

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-cube-cbrt79.4%

      \[\leadsto -\color{blue}{\left(\sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b} \cdot \sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right) \cdot \sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}} \]
    2. pow379.4%

      \[\leadsto -\color{blue}{{\left(\sqrt[3]{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right)}^{3}} \]
    3. associate-*l*76.7%

      \[\leadsto -{\left(\sqrt[3]{\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)}}\right)}^{3} \]
    4. swap-sqr99.1%

      \[\leadsto -{\left(\sqrt[3]{\color{blue}{\left(a \cdot b\right) \cdot \left(a \cdot b\right)}}\right)}^{3} \]
    5. cbrt-unprod98.7%

      \[\leadsto -{\color{blue}{\left(\sqrt[3]{a \cdot b} \cdot \sqrt[3]{a \cdot b}\right)}}^{3} \]
    6. pow298.7%

      \[\leadsto -{\color{blue}{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}}^{3} \]
  4. Applied egg-rr98.7%

    \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}^{3}} \]
  5. Step-by-step derivation
    1. unpow398.7%

      \[\leadsto -\color{blue}{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2} \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2}\right) \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2}} \]
    2. pow298.7%

      \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2}\right)}^{2}} \cdot {\left(\sqrt[3]{a \cdot b}\right)}^{2} \]
    3. unpow-prod-down98.7%

      \[\leadsto -\color{blue}{{\left({\left(\sqrt[3]{a \cdot b}\right)}^{2} \cdot \sqrt[3]{a \cdot b}\right)}^{2}} \]
    4. unpow298.7%

      \[\leadsto -{\left(\color{blue}{\left(\sqrt[3]{a \cdot b} \cdot \sqrt[3]{a \cdot b}\right)} \cdot \sqrt[3]{a \cdot b}\right)}^{2} \]
    5. add-cube-cbrt99.7%

      \[\leadsto -{\color{blue}{\left(a \cdot b\right)}}^{2} \]
    6. unpow-prod-down77.0%

      \[\leadsto -\color{blue}{{a}^{2} \cdot {b}^{2}} \]
    7. unpow277.0%

      \[\leadsto -\color{blue}{\left(a \cdot a\right)} \cdot {b}^{2} \]
    8. add-sqr-sqrt77.0%

      \[\leadsto -\left(a \cdot a\right) \cdot \color{blue}{\left(\sqrt{{b}^{2}} \cdot \sqrt{{b}^{2}}\right)} \]
    9. sqrt-unprod68.6%

      \[\leadsto -\left(a \cdot a\right) \cdot \color{blue}{\sqrt{{b}^{2} \cdot {b}^{2}}} \]
    10. sqr-neg68.6%

      \[\leadsto -\left(a \cdot a\right) \cdot \sqrt{\color{blue}{\left(-{b}^{2}\right) \cdot \left(-{b}^{2}\right)}} \]
    11. sqrt-unprod16.7%

      \[\leadsto -\left(a \cdot a\right) \cdot \color{blue}{\left(\sqrt{-{b}^{2}} \cdot \sqrt{-{b}^{2}}\right)} \]
    12. add-sqr-sqrt27.9%

      \[\leadsto -\left(a \cdot a\right) \cdot \color{blue}{\left(-{b}^{2}\right)} \]
    13. associate-*r*28.1%

      \[\leadsto -\color{blue}{a \cdot \left(a \cdot \left(-{b}^{2}\right)\right)} \]
    14. *-commutative28.1%

      \[\leadsto -\color{blue}{\left(a \cdot \left(-{b}^{2}\right)\right) \cdot a} \]
    15. add-sqr-sqrt25.1%

      \[\leadsto -\color{blue}{\left(\sqrt{a \cdot \left(-{b}^{2}\right)} \cdot \sqrt{a \cdot \left(-{b}^{2}\right)}\right)} \cdot a \]
    16. associate-*l*25.1%

      \[\leadsto -\color{blue}{\sqrt{a \cdot \left(-{b}^{2}\right)} \cdot \left(\sqrt{a \cdot \left(-{b}^{2}\right)} \cdot a\right)} \]
  6. Applied egg-rr52.6%

    \[\leadsto -\color{blue}{\left(b \cdot \sqrt{a}\right) \cdot \left(\left(b \cdot \sqrt{a}\right) \cdot a\right)} \]
  7. Final simplification52.6%

    \[\leadsto -\left(b \cdot \sqrt{a}\right) \cdot \left(a \cdot \left(b \cdot \sqrt{a}\right)\right) \]
  8. Add Preprocessing

Alternative 3: 92.5% accurate, 0.4× speedup?

\[\begin{array}{l} b_m = \left|b\right| \\ [a, b_m] = \mathsf{sort}([a, b_m])\\ \\ \begin{array}{l} t_0 := b\_m \cdot \left(a \cdot a\right)\\ \mathbf{if}\;b\_m \cdot t\_0 \leq 10^{-12}:\\ \;\;\;\;a \cdot \left(b\_m \cdot \left(b\_m \cdot \left(-a\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-b\_m\right) \cdot t\_0\\ \end{array} \end{array} \]
b_m = (fabs.f64 b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
(FPCore (a b_m)
 :precision binary64
 (let* ((t_0 (* b_m (* a a))))
   (if (<= (* b_m t_0) 1e-12) (* a (* b_m (* b_m (- a)))) (* (- b_m) t_0))))
b_m = fabs(b);
assert(a < b_m);
double code(double a, double b_m) {
	double t_0 = b_m * (a * a);
	double tmp;
	if ((b_m * t_0) <= 1e-12) {
		tmp = a * (b_m * (b_m * -a));
	} else {
		tmp = -b_m * t_0;
	}
	return tmp;
}
b_m = abs(b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
real(8) function code(a, b_m)
    real(8), intent (in) :: a
    real(8), intent (in) :: b_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = b_m * (a * a)
    if ((b_m * t_0) <= 1d-12) then
        tmp = a * (b_m * (b_m * -a))
    else
        tmp = -b_m * t_0
    end if
    code = tmp
end function
b_m = Math.abs(b);
assert a < b_m;
public static double code(double a, double b_m) {
	double t_0 = b_m * (a * a);
	double tmp;
	if ((b_m * t_0) <= 1e-12) {
		tmp = a * (b_m * (b_m * -a));
	} else {
		tmp = -b_m * t_0;
	}
	return tmp;
}
b_m = math.fabs(b)
[a, b_m] = sort([a, b_m])
def code(a, b_m):
	t_0 = b_m * (a * a)
	tmp = 0
	if (b_m * t_0) <= 1e-12:
		tmp = a * (b_m * (b_m * -a))
	else:
		tmp = -b_m * t_0
	return tmp
b_m = abs(b)
a, b_m = sort([a, b_m])
function code(a, b_m)
	t_0 = Float64(b_m * Float64(a * a))
	tmp = 0.0
	if (Float64(b_m * t_0) <= 1e-12)
		tmp = Float64(a * Float64(b_m * Float64(b_m * Float64(-a))));
	else
		tmp = Float64(Float64(-b_m) * t_0);
	end
	return tmp
end
b_m = abs(b);
a, b_m = num2cell(sort([a, b_m])){:}
function tmp_2 = code(a, b_m)
	t_0 = b_m * (a * a);
	tmp = 0.0;
	if ((b_m * t_0) <= 1e-12)
		tmp = a * (b_m * (b_m * -a));
	else
		tmp = -b_m * t_0;
	end
	tmp_2 = tmp;
end
b_m = N[Abs[b], $MachinePrecision]
NOTE: a and b_m should be sorted in increasing order before calling this function.
code[a_, b$95$m_] := Block[{t$95$0 = N[(b$95$m * N[(a * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(b$95$m * t$95$0), $MachinePrecision], 1e-12], N[(a * N[(b$95$m * N[(b$95$m * (-a)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-b$95$m) * t$95$0), $MachinePrecision]]]
\begin{array}{l}
b_m = \left|b\right|
\\
[a, b_m] = \mathsf{sort}([a, b_m])\\
\\
\begin{array}{l}
t_0 := b\_m \cdot \left(a \cdot a\right)\\
\mathbf{if}\;b\_m \cdot t\_0 \leq 10^{-12}:\\
\;\;\;\;a \cdot \left(b\_m \cdot \left(b\_m \cdot \left(-a\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(-b\_m\right) \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 (*.f64 a a) b) b) < 9.9999999999999998e-13

    1. Initial program 80.2%

      \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
    2. Step-by-step derivation
      1. associate-*l*77.4%

        \[\leadsto -\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)} \]
      2. associate-*r*83.5%

        \[\leadsto -\color{blue}{a \cdot \left(a \cdot \left(b \cdot b\right)\right)} \]
      3. *-commutative83.5%

        \[\leadsto -a \cdot \color{blue}{\left(\left(b \cdot b\right) \cdot a\right)} \]
      4. distribute-rgt-neg-in83.5%

        \[\leadsto \color{blue}{a \cdot \left(-\left(b \cdot b\right) \cdot a\right)} \]
      5. distribute-rgt-neg-in83.5%

        \[\leadsto a \cdot \color{blue}{\left(\left(b \cdot b\right) \cdot \left(-a\right)\right)} \]
      6. associate-*r*95.1%

        \[\leadsto a \cdot \color{blue}{\left(b \cdot \left(b \cdot \left(-a\right)\right)\right)} \]
    3. Simplified95.1%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot \left(b \cdot \left(-a\right)\right)\right)} \]
    4. Add Preprocessing

    if 9.9999999999999998e-13 < (*.f64 (*.f64 (*.f64 a a) b) b)

    1. Initial program 79.3%

      \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
    2. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Final simplification87.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \cdot \left(b \cdot \left(a \cdot a\right)\right) \leq 10^{-12}:\\ \;\;\;\;a \cdot \left(b \cdot \left(b \cdot \left(-a\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-b\right) \cdot \left(b \cdot \left(a \cdot a\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 96.2% accurate, 0.6× speedup?

\[\begin{array}{l} b_m = \left|b\right| \\ [a, b_m] = \mathsf{sort}([a, b_m])\\ \\ \begin{array}{l} \mathbf{if}\;b\_m \leq 3.25 \cdot 10^{+267}:\\ \;\;\;\;a \cdot \left(b\_m \cdot \left(b\_m \cdot \left(-a\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;b\_m \cdot \left(-a \cdot \left(b\_m \cdot a\right)\right)\\ \end{array} \end{array} \]
b_m = (fabs.f64 b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
(FPCore (a b_m)
 :precision binary64
 (if (<= b_m 3.25e+267)
   (* a (* b_m (* b_m (- a))))
   (* b_m (- (* a (* b_m a))))))
b_m = fabs(b);
assert(a < b_m);
double code(double a, double b_m) {
	double tmp;
	if (b_m <= 3.25e+267) {
		tmp = a * (b_m * (b_m * -a));
	} else {
		tmp = b_m * -(a * (b_m * a));
	}
	return tmp;
}
b_m = abs(b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
real(8) function code(a, b_m)
    real(8), intent (in) :: a
    real(8), intent (in) :: b_m
    real(8) :: tmp
    if (b_m <= 3.25d+267) then
        tmp = a * (b_m * (b_m * -a))
    else
        tmp = b_m * -(a * (b_m * a))
    end if
    code = tmp
end function
b_m = Math.abs(b);
assert a < b_m;
public static double code(double a, double b_m) {
	double tmp;
	if (b_m <= 3.25e+267) {
		tmp = a * (b_m * (b_m * -a));
	} else {
		tmp = b_m * -(a * (b_m * a));
	}
	return tmp;
}
b_m = math.fabs(b)
[a, b_m] = sort([a, b_m])
def code(a, b_m):
	tmp = 0
	if b_m <= 3.25e+267:
		tmp = a * (b_m * (b_m * -a))
	else:
		tmp = b_m * -(a * (b_m * a))
	return tmp
b_m = abs(b)
a, b_m = sort([a, b_m])
function code(a, b_m)
	tmp = 0.0
	if (b_m <= 3.25e+267)
		tmp = Float64(a * Float64(b_m * Float64(b_m * Float64(-a))));
	else
		tmp = Float64(b_m * Float64(-Float64(a * Float64(b_m * a))));
	end
	return tmp
end
b_m = abs(b);
a, b_m = num2cell(sort([a, b_m])){:}
function tmp_2 = code(a, b_m)
	tmp = 0.0;
	if (b_m <= 3.25e+267)
		tmp = a * (b_m * (b_m * -a));
	else
		tmp = b_m * -(a * (b_m * a));
	end
	tmp_2 = tmp;
end
b_m = N[Abs[b], $MachinePrecision]
NOTE: a and b_m should be sorted in increasing order before calling this function.
code[a_, b$95$m_] := If[LessEqual[b$95$m, 3.25e+267], N[(a * N[(b$95$m * N[(b$95$m * (-a)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(b$95$m * (-N[(a * N[(b$95$m * a), $MachinePrecision]), $MachinePrecision])), $MachinePrecision]]
\begin{array}{l}
b_m = \left|b\right|
\\
[a, b_m] = \mathsf{sort}([a, b_m])\\
\\
\begin{array}{l}
\mathbf{if}\;b\_m \leq 3.25 \cdot 10^{+267}:\\
\;\;\;\;a \cdot \left(b\_m \cdot \left(b\_m \cdot \left(-a\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;b\_m \cdot \left(-a \cdot \left(b\_m \cdot a\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 3.24999999999999991e267

    1. Initial program 79.9%

      \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
    2. Step-by-step derivation
      1. associate-*l*77.1%

        \[\leadsto -\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)} \]
      2. associate-*r*84.0%

        \[\leadsto -\color{blue}{a \cdot \left(a \cdot \left(b \cdot b\right)\right)} \]
      3. *-commutative84.0%

        \[\leadsto -a \cdot \color{blue}{\left(\left(b \cdot b\right) \cdot a\right)} \]
      4. distribute-rgt-neg-in84.0%

        \[\leadsto \color{blue}{a \cdot \left(-\left(b \cdot b\right) \cdot a\right)} \]
      5. distribute-rgt-neg-in84.0%

        \[\leadsto a \cdot \color{blue}{\left(\left(b \cdot b\right) \cdot \left(-a\right)\right)} \]
      6. associate-*r*94.9%

        \[\leadsto a \cdot \color{blue}{\left(b \cdot \left(b \cdot \left(-a\right)\right)\right)} \]
    3. Simplified94.9%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot \left(b \cdot \left(-a\right)\right)\right)} \]
    4. Add Preprocessing

    if 3.24999999999999991e267 < b

    1. Initial program 75.7%

      \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
    2. Step-by-step derivation
      1. distribute-rgt-neg-in75.7%

        \[\leadsto \color{blue}{\left(\left(a \cdot a\right) \cdot b\right) \cdot \left(-b\right)} \]
      2. associate-*l*99.9%

        \[\leadsto \color{blue}{\left(a \cdot \left(a \cdot b\right)\right)} \cdot \left(-b\right) \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{\left(a \cdot \left(a \cdot b\right)\right) \cdot \left(-b\right)} \]
    4. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Final simplification95.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq 3.25 \cdot 10^{+267}:\\ \;\;\;\;a \cdot \left(b \cdot \left(b \cdot \left(-a\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(-a \cdot \left(b \cdot a\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 82.4% accurate, 1.0× speedup?

\[\begin{array}{l} b_m = \left|b\right| \\ [a, b_m] = \mathsf{sort}([a, b_m])\\ \\ \left(-b\_m\right) \cdot \left(b\_m \cdot \left(a \cdot a\right)\right) \end{array} \]
b_m = (fabs.f64 b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
(FPCore (a b_m) :precision binary64 (* (- b_m) (* b_m (* a a))))
b_m = fabs(b);
assert(a < b_m);
double code(double a, double b_m) {
	return -b_m * (b_m * (a * a));
}
b_m = abs(b)
NOTE: a and b_m should be sorted in increasing order before calling this function.
real(8) function code(a, b_m)
    real(8), intent (in) :: a
    real(8), intent (in) :: b_m
    code = -b_m * (b_m * (a * a))
end function
b_m = Math.abs(b);
assert a < b_m;
public static double code(double a, double b_m) {
	return -b_m * (b_m * (a * a));
}
b_m = math.fabs(b)
[a, b_m] = sort([a, b_m])
def code(a, b_m):
	return -b_m * (b_m * (a * a))
b_m = abs(b)
a, b_m = sort([a, b_m])
function code(a, b_m)
	return Float64(Float64(-b_m) * Float64(b_m * Float64(a * a)))
end
b_m = abs(b);
a, b_m = num2cell(sort([a, b_m])){:}
function tmp = code(a, b_m)
	tmp = -b_m * (b_m * (a * a));
end
b_m = N[Abs[b], $MachinePrecision]
NOTE: a and b_m should be sorted in increasing order before calling this function.
code[a_, b$95$m_] := N[((-b$95$m) * N[(b$95$m * N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
b_m = \left|b\right|
\\
[a, b_m] = \mathsf{sort}([a, b_m])\\
\\
\left(-b\_m\right) \cdot \left(b\_m \cdot \left(a \cdot a\right)\right)
\end{array}
Derivation
  1. Initial program 79.7%

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Final simplification79.7%

    \[\leadsto \left(-b\right) \cdot \left(b \cdot \left(a \cdot a\right)\right) \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2024044 
(FPCore (a b)
  :name "ab-angle->ABCF D"
  :precision binary64
  (- (* (* (* a a) b) b)))