Average Error: 10.2 → 3.3
Time: 2.8s
Precision: binary64
\[\frac{x + y \cdot \left(z - x\right)}{z} \]
\[\mathsf{fma}\left(x, \frac{1 - y}{z}, y\right) \]
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
(FPCore (x y z) :precision binary64 (fma x (/ (- 1.0 y) z) y))
double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
double code(double x, double y, double z) {
	return fma(x, ((1.0 - y) / z), y);
}
function code(x, y, z)
	return Float64(Float64(x + Float64(y * Float64(z - x))) / z)
end
function code(x, y, z)
	return fma(x, Float64(Float64(1.0 - y) / z), y)
end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := N[(x * N[(N[(1.0 - y), $MachinePrecision] / z), $MachinePrecision] + y), $MachinePrecision]
\frac{x + y \cdot \left(z - x\right)}{z}
\mathsf{fma}\left(x, \frac{1 - y}{z}, y\right)

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.2
Target0.0
Herbie3.3
\[\left(y + \frac{x}{z}\right) - \frac{y}{\frac{z}{x}} \]

Derivation

  1. Initial program 10.2

    \[\frac{x + y \cdot \left(z - x\right)}{z} \]
  2. Simplified10.2

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}} \]
  3. Taylor expanded in y around 0 3.4

    \[\leadsto \color{blue}{\left(y + \frac{x}{z}\right) - \frac{y \cdot x}{z}} \]
  4. Simplified3.3

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{1 - y}{z}, y\right)} \]
  5. Applied egg-rr3.3

    \[\leadsto \mathsf{fma}\left(x, \color{blue}{{\left(\frac{z}{1 - y}\right)}^{-1}}, y\right) \]
  6. Applied egg-rr3.3

    \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{\frac{1}{z}}{\frac{1}{1 - y}}}, y\right) \]
  7. Applied egg-rr3.3

    \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{1 - y}{z} \cdot 1}, y\right) \]
  8. Final simplification3.3

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

Reproduce

herbie shell --seed 2022162 
(FPCore (x y z)
  :name "Diagrams.Backend.Rasterific:rasterificRadialGradient from diagrams-rasterific-1.3.1.3"
  :precision binary64

  :herbie-target
  (- (+ y (/ x z)) (/ y (/ z x)))

  (/ (+ x (* y (- z x))) z))