ab-angle->ABCF D

Percentage Accurate: 82.8% → 96.7%
Time: 5.9s
Alternatives: 6
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 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: 82.8% 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: 96.7% accurate, 0.5× speedup?

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

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


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

    1. Initial program 80.7%

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

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

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

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

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

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

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

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

    if 9.99999999999999912e-299 < (*.f64 a a)

    1. Initial program 86.6%

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

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

Alternative 2: 99.7% accurate, 1.0× speedup?

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

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Taylor expanded in a around 0 77.6%

    \[\leadsto \color{blue}{-1 \cdot \left({a}^{2} \cdot {b}^{2}\right)} \]
  4. Step-by-step derivation
    1. mul-1-neg77.6%

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

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

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

      \[\leadsto -\color{blue}{\left(a \cdot b\right) \cdot \left(a \cdot b\right)} \]
    5. unpow299.7%

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

    \[\leadsto \color{blue}{-{\left(a \cdot b\right)}^{2}} \]
  6. Step-by-step derivation
    1. neg-mul-199.7%

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

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

      \[\leadsto \color{blue}{\left(-1 \cdot \left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
  7. Applied egg-rr99.7%

    \[\leadsto \color{blue}{\left(-1 \cdot \left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
  8. Step-by-step derivation
    1. mul-1-neg99.7%

      \[\leadsto \color{blue}{\left(-a \cdot b\right)} \cdot \left(a \cdot b\right) \]
    2. neg-sub099.7%

      \[\leadsto \color{blue}{\left(0 - a \cdot b\right)} \cdot \left(a \cdot b\right) \]
  9. Applied egg-rr99.7%

    \[\leadsto \color{blue}{\left(0 - a \cdot b\right)} \cdot \left(a \cdot b\right) \]
  10. Step-by-step derivation
    1. neg-sub099.7%

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

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

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

Alternative 3: 82.8% accurate, 1.0× speedup?

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

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

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

Alternative 4: 28.4% accurate, 1.1× speedup?

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

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

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

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

    \[\leadsto \color{blue}{\left(a \cdot \left(a \cdot b\right)\right) \cdot \left(-b\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. neg-sub094.0%

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \color{blue}{\left(0 - b\right)} \]
    2. sub-neg94.0%

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \color{blue}{\left(0 + \left(-b\right)\right)} \]
    3. add-sqr-sqrt48.4%

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

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \left(0 + \color{blue}{\sqrt{\left(-b\right) \cdot \left(-b\right)}}\right) \]
    5. sqr-neg57.7%

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

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \left(0 + \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right) \]
    7. add-sqr-sqrt32.8%

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \left(0 + \color{blue}{b}\right) \]
  6. Applied egg-rr32.8%

    \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \color{blue}{\left(0 + b\right)} \]
  7. Step-by-step derivation
    1. +-lft-identity32.8%

      \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \color{blue}{b} \]
  8. Simplified32.8%

    \[\leadsto \left(a \cdot \left(a \cdot b\right)\right) \cdot \color{blue}{b} \]
  9. Final simplification32.8%

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

Alternative 5: 28.3% accurate, 1.1× speedup?

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

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt31.9%

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

      \[\leadsto \color{blue}{\sqrt{\left(-\left(\left(a \cdot a\right) \cdot b\right) \cdot b\right) \cdot \left(-\left(\left(a \cdot a\right) \cdot b\right) \cdot b\right)}} \]
    3. sqr-neg32.8%

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

      \[\leadsto \color{blue}{\sqrt{\left(\left(a \cdot a\right) \cdot b\right) \cdot b} \cdot \sqrt{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}} \]
    5. add-sqr-sqrt32.7%

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

      \[\leadsto \color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)} \]
    7. swap-sqr32.7%

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

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

Alternative 6: 28.3% accurate, 1.1× speedup?

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

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

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

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

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

      \[\leadsto \color{blue}{a \cdot \left(-\left(b \cdot b\right) \cdot a\right)} \]
    5. distribute-rgt-neg-in85.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
  5. Step-by-step derivation
    1. neg-sub094.9%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \color{blue}{\left(0 - a\right)}\right)\right) \]
    2. sub-neg94.9%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \color{blue}{\left(0 + \left(-a\right)\right)}\right)\right) \]
    3. add-sqr-sqrt47.5%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \left(0 + \color{blue}{\sqrt{-a} \cdot \sqrt{-a}}\right)\right)\right) \]
    4. sqrt-unprod58.6%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \left(0 + \color{blue}{\sqrt{\left(-a\right) \cdot \left(-a\right)}}\right)\right)\right) \]
    5. sqr-neg58.6%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \left(0 + \sqrt{\color{blue}{a \cdot a}}\right)\right)\right) \]
    6. sqrt-prod17.4%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \left(0 + \color{blue}{\sqrt{a} \cdot \sqrt{a}}\right)\right)\right) \]
    7. add-sqr-sqrt32.8%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \left(0 + \color{blue}{a}\right)\right)\right) \]
  6. Applied egg-rr32.8%

    \[\leadsto a \cdot \left(b \cdot \left(b \cdot \color{blue}{\left(0 + a\right)}\right)\right) \]
  7. Step-by-step derivation
    1. +-lft-identity32.8%

      \[\leadsto a \cdot \left(b \cdot \left(b \cdot \color{blue}{a}\right)\right) \]
  8. Simplified32.8%

    \[\leadsto a \cdot \left(b \cdot \left(b \cdot \color{blue}{a}\right)\right) \]
  9. Final simplification32.8%

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

Reproduce

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