Diagrams.Backend.Rasterific:rasterificRadialGradient from diagrams-rasterific-1.3.1.3

?

Percentage Accurate: 88.3% → 99.9%
Time: 11.0s
Precision: binary64
Cost: 7112

?

\[\frac{x + y \cdot \left(z - x\right)}{z} \]
\[\begin{array}{l} t_0 := y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{if}\;y \leq -1 \cdot 10^{-206}:\\ \;\;\;\;\frac{x}{z} + t_0\\ \mathbf{elif}\;y \leq 96000000000000:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (- 1.0 (/ x z)))))
   (if (<= y -1e-206)
     (+ (/ x z) t_0)
     (if (<= y 96000000000000.0) (/ (fma y (- z x) x) z) t_0))))
double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
double code(double x, double y, double z) {
	double t_0 = y * (1.0 - (x / z));
	double tmp;
	if (y <= -1e-206) {
		tmp = (x / z) + t_0;
	} else if (y <= 96000000000000.0) {
		tmp = fma(y, (z - x), x) / z;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(x + Float64(y * Float64(z - x))) / z)
end
function code(x, y, z)
	t_0 = Float64(y * Float64(1.0 - Float64(x / z)))
	tmp = 0.0
	if (y <= -1e-206)
		tmp = Float64(Float64(x / z) + t_0);
	elseif (y <= 96000000000000.0)
		tmp = Float64(fma(y, Float64(z - x), x) / z);
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(1.0 - N[(x / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1e-206], N[(N[(x / z), $MachinePrecision] + t$95$0), $MachinePrecision], If[LessEqual[y, 96000000000000.0], N[(N[(y * N[(z - x), $MachinePrecision] + x), $MachinePrecision] / z), $MachinePrecision], t$95$0]]]
\frac{x + y \cdot \left(z - x\right)}{z}
\begin{array}{l}
t_0 := y \cdot \left(1 - \frac{x}{z}\right)\\
\mathbf{if}\;y \leq -1 \cdot 10^{-206}:\\
\;\;\;\;\frac{x}{z} + t_0\\

\mathbf{elif}\;y \leq 96000000000000:\\
\;\;\;\;\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

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.

Herbie found 11 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Target

Original88.3%
Target93.4%
Herbie99.9%
\[\left(y + \frac{x}{z}\right) - \frac{y}{\frac{z}{x}} \]

Derivation?

  1. Split input into 3 regimes
  2. if y < -1.00000000000000003e-206

    1. Initial program 83.9%

      \[\frac{x + y \cdot \left(z - x\right)}{z} \]
    2. Taylor expanded in y around 0 99.9%

      \[\leadsto \color{blue}{\left(1 - \frac{x}{z}\right) \cdot y + \frac{x}{z}} \]

    if -1.00000000000000003e-206 < y < 9.6e13

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}} \]
      Step-by-step derivation

      [Start]100.0%

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

      +-commutative [=>]100.0%

      \[ \frac{\color{blue}{y \cdot \left(z - x\right) + x}}{z} \]

      fma-def [=>]100.0%

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

    if 9.6e13 < y

    1. Initial program 78.8%

      \[\frac{x + y \cdot \left(z - x\right)}{z} \]
    2. Taylor expanded in y around 0 91.0%

      \[\leadsto \color{blue}{\left(1 - \frac{x}{z}\right) \cdot y + \frac{x}{z}} \]
    3. Taylor expanded in y around inf 99.9%

      \[\leadsto \color{blue}{\left(1 - \frac{x}{z}\right) \cdot y} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-206}:\\ \;\;\;\;\frac{x}{z} + y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 96000000000000:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost7112
\[\begin{array}{l} t_0 := y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{if}\;y \leq -1 \cdot 10^{-206}:\\ \;\;\;\;\frac{x}{z} + t_0\\ \mathbf{elif}\;y \leq 96000000000000:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, z - x, x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Accuracy99.7%
Cost841
\[\begin{array}{l} \mathbf{if}\;y \leq -4.2 \cdot 10^{+48} \lor \neg \left(y \leq 3.4 \cdot 10^{+18}\right):\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \end{array} \]
Alternative 3
Accuracy99.9%
Cost840
\[\begin{array}{l} t_0 := y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{if}\;y \leq -1 \cdot 10^{-206}:\\ \;\;\;\;\frac{x}{z} + t_0\\ \mathbf{elif}\;y \leq 2 \cdot 10^{+16}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy78.6%
Cost781
\[\begin{array}{l} \mathbf{if}\;y \leq 1.9 \cdot 10^{+45}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{elif}\;y \leq 1.55 \cdot 10^{+83} \lor \neg \left(y \leq 2.85 \cdot 10^{+134}\right):\\ \;\;\;\;x \cdot \frac{-y}{z}\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 5
Accuracy78.7%
Cost781
\[\begin{array}{l} \mathbf{if}\;y \leq 2.2 \cdot 10^{+45}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{elif}\;y \leq 7.2 \cdot 10^{+82} \lor \neg \left(y \leq 7 \cdot 10^{+135}\right):\\ \;\;\;\;\frac{-x}{\frac{z}{y}}\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 6
Accuracy78.8%
Cost780
\[\begin{array}{l} \mathbf{if}\;y \leq 2.2 \cdot 10^{+45}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{elif}\;y \leq 7 \cdot 10^{+74}:\\ \;\;\;\;\frac{y \cdot \left(-x\right)}{z}\\ \mathbf{elif}\;y \leq 7 \cdot 10^{+135}:\\ \;\;\;\;y - \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{-x}{\frac{z}{y}}\\ \end{array} \]
Alternative 7
Accuracy98.7%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -6.5 \cdot 10^{+22} \lor \neg \left(y \leq 2.3 \cdot 10^{-5}\right):\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{else}:\\ \;\;\;\;y + \frac{x}{z}\\ \end{array} \]
Alternative 8
Accuracy57.5%
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -1.45 \cdot 10^{+33}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;y\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z}\\ \end{array} \]
Alternative 9
Accuracy81.9%
Cost452
\[\begin{array}{l} \mathbf{if}\;y \leq 2.3 \cdot 10^{-5}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 10
Accuracy78.3%
Cost320
\[y + \frac{x}{z} \]
Alternative 11
Accuracy41.0%
Cost64
\[y \]

Reproduce?

herbie shell --seed 2023263 
(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))