Average Error: 0.2 → 0.2
Time: 5.9s
Precision: binary64
Cost: 6848
\[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
\[\mathsf{fma}\left(x - y, \frac{4}{z}, -2\right) \]
(FPCore (x y z) :precision binary64 (/ (* 4.0 (- (- x y) (* z 0.5))) z))
(FPCore (x y z) :precision binary64 (fma (- x y) (/ 4.0 z) -2.0))
double code(double x, double y, double z) {
	return (4.0 * ((x - y) - (z * 0.5))) / z;
}
double code(double x, double y, double z) {
	return fma((x - y), (4.0 / z), -2.0);
}
function code(x, y, z)
	return Float64(Float64(4.0 * Float64(Float64(x - y) - Float64(z * 0.5))) / z)
end
function code(x, y, z)
	return fma(Float64(x - y), Float64(4.0 / z), -2.0)
end
code[x_, y_, z_] := N[(N[(4.0 * N[(N[(x - y), $MachinePrecision] - N[(z * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := N[(N[(x - y), $MachinePrecision] * N[(4.0 / z), $MachinePrecision] + -2.0), $MachinePrecision]
\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z}
\mathsf{fma}\left(x - y, \frac{4}{z}, -2\right)

Error

Target

Original0.2
Target0.0
Herbie0.2
\[4 \cdot \frac{x}{z} - \left(2 + 4 \cdot \frac{y}{z}\right) \]

Derivation

  1. Initial program 0.2

    \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
  2. Simplified0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(x - y, \frac{4}{z}, -2\right)} \]
    Proof
    (fma.f64 (-.f64 x y) (/.f64 4 z) -2): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (Rewrite<= metadata-eval (*.f64 1 -2))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (*.f64 (Rewrite<= *-inverses_binary64 (/.f64 (neg.f64 z) (neg.f64 z))) -2)): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (*.f64 (/.f64 (neg.f64 z) (neg.f64 z)) (Rewrite<= metadata-eval (/.f64 2 -1)))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (*.f64 (/.f64 (neg.f64 z) (neg.f64 z)) (/.f64 (Rewrite<= metadata-eval (*.f64 1/2 4)) -1))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (Rewrite<= times-frac_binary64 (/.f64 (*.f64 (neg.f64 z) (*.f64 1/2 4)) (*.f64 (neg.f64 z) -1)))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (/.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 (neg.f64 z) 1/2) 4)) (*.f64 (neg.f64 z) -1))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (/.f64 (*.f64 (*.f64 (neg.f64 z) 1/2) 4) (Rewrite=> *-commutative_binary64 (*.f64 -1 (neg.f64 z))))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (/.f64 (*.f64 (*.f64 (neg.f64 z) 1/2) 4) (Rewrite<= neg-mul-1_binary64 (neg.f64 (neg.f64 z))))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (/.f64 (*.f64 (*.f64 (neg.f64 z) 1/2) 4) (Rewrite=> remove-double-neg_binary64 z))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (-.f64 x y) (/.f64 4 z) (Rewrite<= associate-*r/_binary64 (*.f64 (*.f64 (neg.f64 z) 1/2) (/.f64 4 z)))): 16 points increase in error, 0 points decrease in error
    (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (-.f64 x y) (/.f64 4 z)) (*.f64 (*.f64 (neg.f64 z) 1/2) (/.f64 4 z)))): 0 points increase in error, 1 points decrease in error
    (Rewrite<= distribute-rgt-in_binary64 (*.f64 (/.f64 4 z) (+.f64 (-.f64 x y) (*.f64 (neg.f64 z) 1/2)))): 3 points increase in error, 2 points decrease in error
    (*.f64 (/.f64 4 z) (Rewrite<= cancel-sign-sub-inv_binary64 (-.f64 (-.f64 x y) (*.f64 z 1/2)))): 0 points increase in error, 0 points decrease in error
    (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 4 (-.f64 (-.f64 x y) (*.f64 z 1/2))) z)): 1 points increase in error, 52 points decrease in error
  3. Final simplification0.2

    \[\leadsto \mathsf{fma}\left(x - y, \frac{4}{z}, -2\right) \]

Alternatives

Alternative 1
Error30.3
Cost980
\[\begin{array}{l} t_0 := \frac{4}{\frac{z}{x}}\\ t_1 := y \cdot \frac{-4}{z}\\ \mathbf{if}\;z \leq -2.871659831047258 \cdot 10^{+76}:\\ \;\;\;\;-2\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.25 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.1608054538718934 \cdot 10^{+116}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-2\\ \end{array} \]
Alternative 2
Error30.3
Cost980
\[\begin{array}{l} t_0 := x \cdot \frac{4}{z}\\ t_1 := y \cdot \frac{-4}{z}\\ \mathbf{if}\;z \leq -2.871659831047258 \cdot 10^{+76}:\\ \;\;\;\;-2\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.25 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.1608054538718934 \cdot 10^{+116}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-2\\ \end{array} \]
Alternative 3
Error30.3
Cost980
\[\begin{array}{l} t_0 := x \cdot \frac{4}{z}\\ t_1 := \frac{-4}{\frac{z}{y}}\\ \mathbf{if}\;z \leq -2.871659831047258 \cdot 10^{+76}:\\ \;\;\;\;-2\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.25 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.1608054538718934 \cdot 10^{+116}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-2\\ \end{array} \]
Alternative 4
Error30.3
Cost980
\[\begin{array}{l} t_0 := x \cdot \frac{4}{z}\\ t_1 := -4 \cdot \frac{y}{z}\\ \mathbf{if}\;z \leq -2.871659831047258 \cdot 10^{+76}:\\ \;\;\;\;-2\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.25 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.1608054538718934 \cdot 10^{+116}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-2\\ \end{array} \]
Alternative 5
Error30.2
Cost980
\[\begin{array}{l} t_0 := 4 \cdot \frac{x}{z}\\ t_1 := -4 \cdot \frac{y}{z}\\ \mathbf{if}\;z \leq -2.871659831047258 \cdot 10^{+76}:\\ \;\;\;\;-2\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.25 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.1608054538718934 \cdot 10^{+116}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-2\\ \end{array} \]
Alternative 6
Error14.9
Cost976
\[\begin{array}{l} t_0 := 4 \cdot \frac{x}{z}\\ t_1 := -2 + -4 \cdot \frac{y}{z}\\ \mathbf{if}\;x \leq -1.2 \cdot 10^{+148}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 6.722002239345792 \cdot 10^{-16}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 6.315436614856788 \cdot 10^{+45}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.55 \cdot 10^{+199}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error12.3
Cost712
\[\begin{array}{l} t_0 := -2 + -4 \cdot \frac{y}{z}\\ \mathbf{if}\;z \leq -7.855688326229753 \cdot 10^{-7}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 1.3483886699686101 \cdot 10^{+107}:\\ \;\;\;\;4 \cdot \frac{x - y}{z}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error9.0
Cost712
\[\begin{array}{l} t_0 := -2 + -4 \cdot \frac{y}{z}\\ \mathbf{if}\;y \leq -4.0069046279066546 \cdot 10^{+45}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.1780601419567107 \cdot 10^{+60}:\\ \;\;\;\;-2 + 4 \cdot \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error0.2
Cost704
\[\frac{4 \cdot \left(\left(x - y\right) + z \cdot -0.5\right)}{z} \]
Alternative 10
Error29.8
Cost584
\[\begin{array}{l} t_0 := y \cdot \frac{-4}{z}\\ \mathbf{if}\;y \leq -4.4108964075520585 \cdot 10^{+27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.35 \cdot 10^{+120}:\\ \;\;\;\;-2\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 11
Error36.8
Cost64
\[-2 \]

Error

Reproduce

herbie shell --seed 2022300 
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.ColorRamp:rampColorHotToCold from repa-algorithms-3.4.0.1, B"
  :precision binary64

  :herbie-target
  (- (* 4.0 (/ x z)) (+ 2.0 (* 4.0 (/ y z))))

  (/ (* 4.0 (- (- x y) (* z 0.5))) z))