ab-angle->ABCF D

Percentage Accurate: 81.8% → 99.7%
Time: 6.5s
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: 81.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: 99.7% accurate, 0.5× speedup?

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

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-neg.f64N/A

      \[\leadsto \color{blue}{\mathsf{neg}\left(\left(\left(a \cdot a\right) \cdot b\right) \cdot b\right)} \]
    2. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right) \]
    3. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\left(a \cdot a\right) \cdot b\right)} \cdot b\right) \]
    4. associate-*l*N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)}\right) \]
    5. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot a\right)} \cdot \left(b \cdot b\right)\right) \]
    6. unswap-sqrN/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot b\right) \cdot \left(a \cdot b\right)}\right) \]
    7. distribute-lft-neg-inN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
    8. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
    9. distribute-rgt-neg-inN/A

      \[\leadsto \color{blue}{\left(a \cdot \left(\mathsf{neg}\left(b\right)\right)\right)} \cdot \left(a \cdot b\right) \]
    10. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(a \cdot \left(\mathsf{neg}\left(b\right)\right)\right)} \cdot \left(a \cdot b\right) \]
    11. lower-neg.f64N/A

      \[\leadsto \left(a \cdot \color{blue}{\left(\mathsf{neg}\left(b\right)\right)}\right) \cdot \left(a \cdot b\right) \]
    12. lower-*.f6499.6

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

    \[\leadsto \color{blue}{\left(a \cdot \left(-b\right)\right) \cdot \left(a \cdot b\right)} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(a \cdot \left(\mathsf{neg}\left(b\right)\right)\right)} \cdot \left(a \cdot b\right) \]
    2. lift-neg.f64N/A

      \[\leadsto \left(a \cdot \color{blue}{\left(\mathsf{neg}\left(b\right)\right)}\right) \cdot \left(a \cdot b\right) \]
    3. neg-sub0N/A

      \[\leadsto \left(a \cdot \color{blue}{\left(0 - b\right)}\right) \cdot \left(a \cdot b\right) \]
    4. flip3--N/A

      \[\leadsto \left(a \cdot \color{blue}{\frac{{0}^{3} - {b}^{3}}{0 \cdot 0 + \left(b \cdot b + 0 \cdot b\right)}}\right) \cdot \left(a \cdot b\right) \]
    5. clear-numN/A

      \[\leadsto \left(a \cdot \color{blue}{\frac{1}{\frac{0 \cdot 0 + \left(b \cdot b + 0 \cdot b\right)}{{0}^{3} - {b}^{3}}}}\right) \cdot \left(a \cdot b\right) \]
    6. un-div-invN/A

      \[\leadsto \color{blue}{\frac{a}{\frac{0 \cdot 0 + \left(b \cdot b + 0 \cdot b\right)}{{0}^{3} - {b}^{3}}}} \cdot \left(a \cdot b\right) \]
    7. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{a}{\frac{0 \cdot 0 + \left(b \cdot b + 0 \cdot b\right)}{{0}^{3} - {b}^{3}}}} \cdot \left(a \cdot b\right) \]
    8. metadata-evalN/A

      \[\leadsto \frac{a}{\frac{\color{blue}{0} + \left(b \cdot b + 0 \cdot b\right)}{{0}^{3} - {b}^{3}}} \cdot \left(a \cdot b\right) \]
    9. +-lft-identityN/A

      \[\leadsto \frac{a}{\frac{\color{blue}{b \cdot b + 0 \cdot b}}{{0}^{3} - {b}^{3}}} \cdot \left(a \cdot b\right) \]
    10. mul0-lftN/A

      \[\leadsto \frac{a}{\frac{b \cdot b + \color{blue}{0}}{{0}^{3} - {b}^{3}}} \cdot \left(a \cdot b\right) \]
    11. +-rgt-identityN/A

      \[\leadsto \frac{a}{\frac{\color{blue}{b \cdot b}}{{0}^{3} - {b}^{3}}} \cdot \left(a \cdot b\right) \]
    12. clear-numN/A

      \[\leadsto \frac{a}{\color{blue}{\frac{1}{\frac{{0}^{3} - {b}^{3}}{b \cdot b}}}} \cdot \left(a \cdot b\right) \]
    13. *-inversesN/A

      \[\leadsto \frac{a}{\frac{\color{blue}{\frac{b}{b}}}{\frac{{0}^{3} - {b}^{3}}{b \cdot b}}} \cdot \left(a \cdot b\right) \]
    14. +-rgt-identityN/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\frac{{0}^{3} - {b}^{3}}{\color{blue}{b \cdot b + 0}}}} \cdot \left(a \cdot b\right) \]
    15. mul0-lftN/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\frac{{0}^{3} - {b}^{3}}{b \cdot b + \color{blue}{0 \cdot b}}}} \cdot \left(a \cdot b\right) \]
    16. +-lft-identityN/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\frac{{0}^{3} - {b}^{3}}{\color{blue}{0 + \left(b \cdot b + 0 \cdot b\right)}}}} \cdot \left(a \cdot b\right) \]
    17. metadata-evalN/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\frac{{0}^{3} - {b}^{3}}{\color{blue}{0 \cdot 0} + \left(b \cdot b + 0 \cdot b\right)}}} \cdot \left(a \cdot b\right) \]
    18. flip3--N/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\color{blue}{0 - b}}} \cdot \left(a \cdot b\right) \]
    19. neg-sub0N/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\color{blue}{\mathsf{neg}\left(b\right)}}} \cdot \left(a \cdot b\right) \]
    20. lift-neg.f64N/A

      \[\leadsto \frac{a}{\frac{\frac{b}{b}}{\color{blue}{\mathsf{neg}\left(b\right)}}} \cdot \left(a \cdot b\right) \]
    21. associate-/r*N/A

      \[\leadsto \frac{a}{\color{blue}{\frac{b}{b \cdot \left(\mathsf{neg}\left(b\right)\right)}}} \cdot \left(a \cdot b\right) \]
    22. *-commutativeN/A

      \[\leadsto \frac{a}{\frac{b}{\color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b}}} \cdot \left(a \cdot b\right) \]
  6. Applied rewrites99.7%

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

Alternative 2: 99.7% accurate, 1.0× speedup?

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

    \[-\left(\left(a \cdot a\right) \cdot b\right) \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-neg.f64N/A

      \[\leadsto \color{blue}{\mathsf{neg}\left(\left(\left(a \cdot a\right) \cdot b\right) \cdot b\right)} \]
    2. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\left(a \cdot a\right) \cdot b\right) \cdot b}\right) \]
    3. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\left(a \cdot a\right) \cdot b\right)} \cdot b\right) \]
    4. associate-*l*N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot a\right) \cdot \left(b \cdot b\right)}\right) \]
    5. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot a\right)} \cdot \left(b \cdot b\right)\right) \]
    6. unswap-sqrN/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(a \cdot b\right) \cdot \left(a \cdot b\right)}\right) \]
    7. distribute-lft-neg-inN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
    8. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(a \cdot b\right)\right) \cdot \left(a \cdot b\right)} \]
    9. distribute-rgt-neg-inN/A

      \[\leadsto \color{blue}{\left(a \cdot \left(\mathsf{neg}\left(b\right)\right)\right)} \cdot \left(a \cdot b\right) \]
    10. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(a \cdot \left(\mathsf{neg}\left(b\right)\right)\right)} \cdot \left(a \cdot b\right) \]
    11. lower-neg.f64N/A

      \[\leadsto \left(a \cdot \color{blue}{\left(\mathsf{neg}\left(b\right)\right)}\right) \cdot \left(a \cdot b\right) \]
    12. lower-*.f6499.7

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

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

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

Reproduce

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