(1 - a * x) / (x - y)

Percentage Accurate: 100.0% → 100.0%
Time: 1.5s
Alternatives: 4
Speedup: N/A×

Specification

?
\[\left(\left(0 \leq a \land a \leq 1\right) \land \left(0 \leq x \land x \leq 1000000000\right)\right) \land \left(-1000000000 \leq y \land y \leq -1000000000\right)\]
\[\frac{1 - a \cdot x}{x - y} \]
(FPCore (a x y)
  :precision binary64
  (/ (- 1.0 (* a x)) (- x y)))
double code(double a, double x, double y) {
	return (1.0 - (a * x)) / (x - y);
}
real(8) function code(a, x, y)
    real(8), intent (in) :: a
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (1.0d0 - (a * x)) / (x - y)
end function
public static double code(double a, double x, double y) {
	return (1.0 - (a * x)) / (x - y);
}
def code(a, x, y):
	return (1.0 - (a * x)) / (x - y)
function code(a, x, y)
	return Float64(Float64(1.0 - Float64(a * x)) / Float64(x - y))
end
function tmp = code(a, x, y)
	tmp = (1.0 - (a * x)) / (x - y);
end
code[a_, x_, y_] := N[(N[(1.0 - N[(a * x), $MachinePrecision]), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\frac{1 - a \cdot x}{x - y}

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

\[\frac{1 - a \cdot x}{x - y} \]
(FPCore (a x y)
  :precision binary64
  (/ (- 1.0 (* a x)) (- x y)))
double code(double a, double x, double y) {
	return (1.0 - (a * x)) / (x - y);
}
real(8) function code(a, x, y)
    real(8), intent (in) :: a
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (1.0d0 - (a * x)) / (x - y)
end function
public static double code(double a, double x, double y) {
	return (1.0 - (a * x)) / (x - y);
}
def code(a, x, y):
	return (1.0 - (a * x)) / (x - y)
function code(a, x, y)
	return Float64(Float64(1.0 - Float64(a * x)) / Float64(x - y))
end
function tmp = code(a, x, y)
	tmp = (1.0 - (a * x)) / (x - y);
end
code[a_, x_, y_] := N[(N[(1.0 - N[(a * x), $MachinePrecision]), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\frac{1 - a \cdot x}{x - y}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\frac{\mathsf{fma}\left(x, a, -1\right)}{y - x} \]
(FPCore (a x y)
  :precision binary64
  (/ (fma x a -1.0) (- y x)))
double code(double a, double x, double y) {
	return fma(x, a, -1.0) / (y - x);
}
function code(a, x, y)
	return Float64(fma(x, a, -1.0) / Float64(y - x))
end
code[a_, x_, y_] := N[(N[(x * a + -1.0), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]
\frac{\mathsf{fma}\left(x, a, -1\right)}{y - x}
Derivation
  1. Initial program 100.0%

    \[\frac{1 - a \cdot x}{x - y} \]
  2. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{1 - a \cdot x}{x - y}} \]
    2. frac-2negN/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\left(1 - a \cdot x\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)}} \]
    3. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\left(1 - a \cdot x\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)}} \]
    4. lift--.f64N/A

      \[\leadsto \frac{\mathsf{neg}\left(\color{blue}{\left(1 - a \cdot x\right)}\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    5. sub-flipN/A

      \[\leadsto \frac{\mathsf{neg}\left(\color{blue}{\left(1 + \left(\mathsf{neg}\left(a \cdot x\right)\right)\right)}\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    6. distribute-neg-inN/A

      \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(a \cdot x\right)\right)\right)\right)}}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    7. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{-1} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(a \cdot x\right)\right)\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    8. +-commutativeN/A

      \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(a \cdot x\right)\right)\right)\right) + -1}}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    9. metadata-evalN/A

      \[\leadsto \frac{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(a \cdot x\right)\right)\right)\right) + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)}}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    10. remove-double-negN/A

      \[\leadsto \frac{\color{blue}{a \cdot x} + \left(\mathsf{neg}\left(1\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    11. lift-*.f64N/A

      \[\leadsto \frac{\color{blue}{a \cdot x} + \left(\mathsf{neg}\left(1\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    12. *-commutativeN/A

      \[\leadsto \frac{\color{blue}{x \cdot a} + \left(\mathsf{neg}\left(1\right)\right)}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    13. metadata-evalN/A

      \[\leadsto \frac{x \cdot a + \color{blue}{-1}}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    14. lower-fma.f64N/A

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(x, a, -1\right)}}{\mathsf{neg}\left(\left(x - y\right)\right)} \]
    15. lift--.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(x, a, -1\right)}{\mathsf{neg}\left(\color{blue}{\left(x - y\right)}\right)} \]
    16. sub-negate-revN/A

      \[\leadsto \frac{\mathsf{fma}\left(x, a, -1\right)}{\color{blue}{y - x}} \]
    17. lower--.f64100.0%

      \[\leadsto \frac{\mathsf{fma}\left(x, a, -1\right)}{\color{blue}{y - x}} \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, a, -1\right)}{y - x}} \]
  4. Add Preprocessing

Alternative 2: 99.8% accurate, 1.8× speedup?

\[\frac{1}{x - y} \]
(FPCore (a x y)
  :precision binary64
  (/ 1.0 (- x y)))
double code(double a, double x, double y) {
	return 1.0 / (x - y);
}
real(8) function code(a, x, y)
    real(8), intent (in) :: a
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 1.0d0 / (x - y)
end function
public static double code(double a, double x, double y) {
	return 1.0 / (x - y);
}
def code(a, x, y):
	return 1.0 / (x - y)
function code(a, x, y)
	return Float64(1.0 / Float64(x - y))
end
function tmp = code(a, x, y)
	tmp = 1.0 / (x - y);
end
code[a_, x_, y_] := N[(1.0 / N[(x - y), $MachinePrecision]), $MachinePrecision]
\frac{1}{x - y}
Derivation
  1. Initial program 100.0%

    \[\frac{1 - a \cdot x}{x - y} \]
  2. Taylor expanded in a around 0

    \[\leadsto \frac{\color{blue}{1}}{x - y} \]
  3. Step-by-step derivation
    1. Applied rewrites99.8%

      \[\leadsto \frac{\color{blue}{1}}{x - y} \]
    2. Add Preprocessing

    Alternative 3: 97.9% accurate, 2.7× speedup?

    \[\frac{-1}{y} \]
    (FPCore (a x y)
      :precision binary64
      (/ -1.0 y))
    double code(double a, double x, double y) {
    	return -1.0 / y;
    }
    
    real(8) function code(a, x, y)
        real(8), intent (in) :: a
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        code = (-1.0d0) / y
    end function
    
    public static double code(double a, double x, double y) {
    	return -1.0 / y;
    }
    
    def code(a, x, y):
    	return -1.0 / y
    
    function code(a, x, y)
    	return Float64(-1.0 / y)
    end
    
    function tmp = code(a, x, y)
    	tmp = -1.0 / y;
    end
    
    code[a_, x_, y_] := N[(-1.0 / y), $MachinePrecision]
    
    \frac{-1}{y}
    
    Derivation
    1. Initial program 100.0%

      \[\frac{1 - a \cdot x}{x - y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{-1}{y}} \]
    3. Step-by-step derivation
      1. lower-/.f6497.9%

        \[\leadsto \frac{-1}{\color{blue}{y}} \]
    4. Applied rewrites97.9%

      \[\leadsto \color{blue}{\frac{-1}{y}} \]
    5. Add Preprocessing

    Alternative 4: 2.3% accurate, 6.5× speedup?

    \[-a \]
    (FPCore (a x y)
      :precision binary64
      (- a))
    double code(double a, double x, double y) {
    	return -a;
    }
    
    real(8) function code(a, x, y)
        real(8), intent (in) :: a
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        code = -a
    end function
    
    public static double code(double a, double x, double y) {
    	return -a;
    }
    
    def code(a, x, y):
    	return -a
    
    function code(a, x, y)
    	return Float64(-a)
    end
    
    function tmp = code(a, x, y)
    	tmp = -a;
    end
    
    code[a_, x_, y_] := (-a)
    
    -a
    
    Derivation
    1. Initial program 100.0%

      \[\frac{1 - a \cdot x}{x - y} \]
    2. Taylor expanded in x around inf

      \[\leadsto \color{blue}{-1 \cdot a} \]
    3. Step-by-step derivation
      1. lower-*.f642.3%

        \[\leadsto -1 \cdot \color{blue}{a} \]
    4. Applied rewrites2.3%

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

        \[\leadsto -1 \cdot \color{blue}{a} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(a\right) \]
      3. lower-neg.f642.3%

        \[\leadsto -a \]
    6. Applied rewrites2.3%

      \[\leadsto -a \]
    7. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 1 
    (FPCore (a x y)
      :name "(1 - a * x) / (x - y)"
      :precision binary64
      :pre (and (and (and (<= 0.0 a) (<= a 1.0)) (and (<= 0.0 x) (<= x 1000000000.0))) (and (<= -1000000000.0 y) (<= y -1000000000.0)))
      (/ (- 1.0 (* a x)) (- x y)))