Difference of squares

Percentage Accurate: 93.9% → 100.0%
Time: 1.3s
Alternatives: 2
Speedup: 1.0×

Specification

?
\[a \cdot a - b \cdot b \]
(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)
use fmin_fmax_functions
    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(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
a \cdot a - b \cdot b

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

\[a \cdot a - b \cdot b \]
(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)
use fmin_fmax_functions
    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(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
a \cdot a - b \cdot b

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\left(a - b\right) \cdot \left(b + a\right) \]
(FPCore (a b)
  :precision binary64
  (* (- a b) (+ b a)))
double code(double a, double b) {
	return (a - b) * (b + a);
}
real(8) function code(a, b)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a - b) * (b + a)
end function
public static double code(double a, double b) {
	return (a - b) * (b + a);
}
def code(a, b):
	return (a - b) * (b + a)
function code(a, b)
	return Float64(Float64(a - b) * Float64(b + a))
end
function tmp = code(a, b)
	tmp = (a - b) * (b + a);
end
code[a_, b_] := N[(N[(a - b), $MachinePrecision] * N[(b + a), $MachinePrecision]), $MachinePrecision]
\left(a - b\right) \cdot \left(b + a\right)
Derivation
  1. Initial program 93.9%

    \[a \cdot a - b \cdot b \]
  2. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{a \cdot a - b \cdot b} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{a \cdot a} - b \cdot b \]
    3. lift-*.f64N/A

      \[\leadsto a \cdot a - \color{blue}{b \cdot b} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
    5. *-commutativeN/A

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

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

      \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a + b\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(b + a\right)} \]
    9. lower-+.f64100.0%

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

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

Alternative 2: 60.7% accurate, 1.0× speedup?

\[\left(\left|a\right| - \left|b\right|\right) \cdot \left|b\right| \]
(FPCore (a b)
  :precision binary64
  (* (- (fabs a) (fabs b)) (fabs b)))
double code(double a, double b) {
	return (fabs(a) - fabs(b)) * fabs(b);
}
real(8) function code(a, b)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (abs(a) - abs(b)) * abs(b)
end function
public static double code(double a, double b) {
	return (Math.abs(a) - Math.abs(b)) * Math.abs(b);
}
def code(a, b):
	return (math.fabs(a) - math.fabs(b)) * math.fabs(b)
function code(a, b)
	return Float64(Float64(abs(a) - abs(b)) * abs(b))
end
function tmp = code(a, b)
	tmp = (abs(a) - abs(b)) * abs(b);
end
code[a_, b_] := N[(N[(N[Abs[a], $MachinePrecision] - N[Abs[b], $MachinePrecision]), $MachinePrecision] * N[Abs[b], $MachinePrecision]), $MachinePrecision]
\left(\left|a\right| - \left|b\right|\right) \cdot \left|b\right|
Derivation
  1. Initial program 93.9%

    \[a \cdot a - b \cdot b \]
  2. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{a \cdot a - b \cdot b} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{a \cdot a} - b \cdot b \]
    3. lift-*.f64N/A

      \[\leadsto a \cdot a - \color{blue}{b \cdot b} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
    5. *-commutativeN/A

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

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

      \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a + b\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(b + a\right)} \]
    9. lower-+.f64100.0%

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

    \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(b + a\right)} \]
  4. Taylor expanded in a around 0

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

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

    Developer Target 1: 100.0% accurate, 1.0× speedup?

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

    Reproduce

    ?
    herbie shell --seed 2025323 
    (FPCore (a b)
      :name "Difference of squares"
      :precision binary64
    
      :alt
      (! :herbie-platform c (* (+ a b) (- a b)))
    
      (- (* a a) (* b b)))