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

?

Percentage Accurate: 88.4% → 99.9%
Time: 10.5s
Precision: binary64
Cost: 840

?

\[\frac{x + y \cdot \left(z - x\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-209}:\\ \;\;\;\;\frac{x}{z} + y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 5 \cdot 10^{+15}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{z - x}}\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
(FPCore (x y z)
 :precision binary64
 (if (<= y -1e-209)
   (+ (/ x z) (* y (- 1.0 (/ x z))))
   (if (<= y 5e+15) (/ (+ x (* y (- z x))) z) (/ y (/ z (- z x))))))
double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-209) {
		tmp = (x / z) + (y * (1.0 - (x / z)));
	} else if (y <= 5e+15) {
		tmp = (x + (y * (z - x))) / z;
	} else {
		tmp = y / (z / (z - x));
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x + (y * (z - x))) / z
end function
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-1d-209)) then
        tmp = (x / z) + (y * (1.0d0 - (x / z)))
    else if (y <= 5d+15) then
        tmp = (x + (y * (z - x))) / z
    else
        tmp = y / (z / (z - x))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-209) {
		tmp = (x / z) + (y * (1.0 - (x / z)));
	} else if (y <= 5e+15) {
		tmp = (x + (y * (z - x))) / z;
	} else {
		tmp = y / (z / (z - x));
	}
	return tmp;
}
def code(x, y, z):
	return (x + (y * (z - x))) / z
def code(x, y, z):
	tmp = 0
	if y <= -1e-209:
		tmp = (x / z) + (y * (1.0 - (x / z)))
	elif y <= 5e+15:
		tmp = (x + (y * (z - x))) / z
	else:
		tmp = y / (z / (z - x))
	return tmp
function code(x, y, z)
	return Float64(Float64(x + Float64(y * Float64(z - x))) / z)
end
function code(x, y, z)
	tmp = 0.0
	if (y <= -1e-209)
		tmp = Float64(Float64(x / z) + Float64(y * Float64(1.0 - Float64(x / z))));
	elseif (y <= 5e+15)
		tmp = Float64(Float64(x + Float64(y * Float64(z - x))) / z);
	else
		tmp = Float64(y / Float64(z / Float64(z - x)));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = (x + (y * (z - x))) / z;
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -1e-209)
		tmp = (x / z) + (y * (1.0 - (x / z)));
	elseif (y <= 5e+15)
		tmp = (x + (y * (z - x))) / z;
	else
		tmp = y / (z / (z - x));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[y, -1e-209], N[(N[(x / z), $MachinePrecision] + N[(y * N[(1.0 - N[(x / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 5e+15], N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision], N[(y / N[(z / N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{x + y \cdot \left(z - x\right)}{z}
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-209}:\\
\;\;\;\;\frac{x}{z} + y \cdot \left(1 - \frac{x}{z}\right)\\

\mathbf{elif}\;y \leq 5 \cdot 10^{+15}:\\
\;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\

\mathbf{else}:\\
\;\;\;\;\frac{y}{\frac{z}{z - x}}\\


\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

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation?

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

    1. Initial program 92.4%

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

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

    if -1e-209 < y < 5e15

    1. Initial program 99.9%

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

    if 5e15 < y

    1. Initial program 86.6%

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

      \[\leadsto \color{blue}{\frac{y \cdot \left(z - x\right)}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{y}{\frac{z}{z - x}}} \]
      Step-by-step derivation

      [Start]86.6%

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

      associate-/l* [=>]100.0%

      \[ \color{blue}{\frac{y}{\frac{z}{z - x}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-209}:\\ \;\;\;\;\frac{x}{z} + y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 5 \cdot 10^{+15}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{z - x}}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost840
\[\begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-209}:\\ \;\;\;\;\frac{x}{z} + y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 5 \cdot 10^{+15}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{z - x}}\\ \end{array} \]
Alternative 2
Accuracy78.3%
Cost1178
\[\begin{array}{l} t_0 := \frac{y}{z} \cdot \left(-x\right)\\ \mathbf{if}\;y \leq -1.1 \cdot 10^{+228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 9600000000000:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{elif}\;y \leq 5.8 \cdot 10^{+50} \lor \neg \left(y \leq 9.6 \cdot 10^{+126} \lor \neg \left(y \leq 8.2 \cdot 10^{+193}\right) \land y \leq 1.5 \cdot 10^{+253}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 3
Accuracy78.7%
Cost1178
\[\begin{array}{l} t_0 := \frac{y}{\frac{-z}{x}}\\ \mathbf{if}\;y \leq -4.2 \cdot 10^{+228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 220000000000:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{elif}\;y \leq 9 \cdot 10^{+49} \lor \neg \left(y \leq 9.6 \cdot 10^{+126} \lor \neg \left(y \leq 9.5 \cdot 10^{+193}\right) \land y \leq 1.95 \cdot 10^{+251}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 4
Accuracy98.9%
Cost840
\[\begin{array}{l} \mathbf{if}\;y \leq -3.9 \cdot 10^{+107}:\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 9.4 \cdot 10^{+14}:\\ \;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{z - x}}\\ \end{array} \]
Alternative 5
Accuracy98.9%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -1 \lor \neg \left(y \leq 4.1 \cdot 10^{-10}\right):\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{else}:\\ \;\;\;\;y + \frac{x}{z}\\ \end{array} \]
Alternative 6
Accuracy98.9%
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot \left(1 - \frac{x}{z}\right)\\ \mathbf{elif}\;y \leq 4.1 \cdot 10^{-10}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{z}{z - x}}\\ \end{array} \]
Alternative 7
Accuracy63.6%
Cost585
\[\begin{array}{l} \mathbf{if}\;y \leq -3.05 \cdot 10^{-24} \lor \neg \left(y \leq 10^{-10}\right):\\ \;\;\;\;z \cdot \frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z}\\ \end{array} \]
Alternative 8
Accuracy61.7%
Cost456
\[\begin{array}{l} \mathbf{if}\;y \leq -3.1 \cdot 10^{-24}:\\ \;\;\;\;y\\ \mathbf{elif}\;y \leq 5.5 \cdot 10^{-14}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;y\\ \end{array} \]
Alternative 9
Accuracy79.3%
Cost452
\[\begin{array}{l} \mathbf{if}\;y \leq 0.0106:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \frac{y}{z}\\ \end{array} \]
Alternative 10
Accuracy81.5%
Cost452
\[\begin{array}{l} \mathbf{if}\;y \leq 4.1 \cdot 10^{-10}:\\ \;\;\;\;y + \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;y - \frac{x}{z}\\ \end{array} \]
Alternative 11
Accuracy40.6%
Cost64
\[y \]

Reproduce?

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