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

Percentage Accurate: 88.1% → 99.8%
Time: 1.1s
Alternatives: 5
Speedup: 0.7×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ \frac{x + y \cdot \left(z - x\right)}{z} \end{array} \]
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
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
public static double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
def code(x, y, z):
	return (x + (y * (z - x))) / z
function code(x, y, z)
	return Float64(Float64(x + Float64(y * Float64(z - x))) / z)
end
function tmp = code(x, y, z)
	tmp = (x + (y * (z - x))) / z;
end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}

\\
\frac{x + y \cdot \left(z - x\right)}{z}
\end{array}

Sampling outcomes in binary64 precision:

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

\[\begin{array}{l} \\ \frac{x + y \cdot \left(z - x\right)}{z} \end{array} \]
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
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
public static double code(double x, double y, double z) {
	return (x + (y * (z - x))) / z;
}
def code(x, y, z):
	return (x + (y * (z - x))) / z
function code(x, y, z)
	return Float64(Float64(x + Float64(y * Float64(z - x))) / z)
end
function tmp = code(x, y, z)
	tmp = (x + (y * (z - x))) / z;
end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}

\\
\frac{x + y \cdot \left(z - x\right)}{z}
\end{array}

Alternative 1: 99.8% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := y \cdot \frac{z - x}{z}\\
\mathbf{if}\;y \leq -1.25 \cdot 10^{+19}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 60000000:\\
\;\;\;\;\frac{x + y \cdot \left(z - x\right)}{z}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.25e19 or 6e7 < y

    1. Initial program 71.3%

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

      \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
    4. Applied rewrites9.6%

      \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto y \cdot \color{blue}{z} \]
    6. Applied rewrites9.6%

      \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
    7. Taylor expanded in x around inf

      \[\leadsto y \cdot \left(x \cdot \left(\frac{z}{x} - \color{blue}{1}\right)\right) \]
    8. Applied rewrites99.9%

      \[\leadsto y \cdot \frac{z - x}{z} \]

    if -1.25e19 < y < 6e7

    1. Initial program 99.9%

      \[\frac{x + y \cdot \left(z - x\right)}{z} \]
    2. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 11.8% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \left(z - x\right)\\ \mathbf{if}\;z \leq -1.15 \cdot 10^{-298}:\\ \;\;\;\;y \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;x + t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (- z x)))) (if (<= z -1.15e-298) (* y t_0) (+ x t_0))))
double code(double x, double y, double z) {
	double t_0 = y * (z - x);
	double tmp;
	if (z <= -1.15e-298) {
		tmp = y * t_0;
	} else {
		tmp = x + t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = y * (z - x)
    if (z <= (-1.15d-298)) then
        tmp = y * t_0
    else
        tmp = x + t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = y * (z - x);
	double tmp;
	if (z <= -1.15e-298) {
		tmp = y * t_0;
	} else {
		tmp = x + t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = y * (z - x)
	tmp = 0
	if z <= -1.15e-298:
		tmp = y * t_0
	else:
		tmp = x + t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(y * Float64(z - x))
	tmp = 0.0
	if (z <= -1.15e-298)
		tmp = Float64(y * t_0);
	else
		tmp = Float64(x + t_0);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = y * (z - x);
	tmp = 0.0;
	if (z <= -1.15e-298)
		tmp = y * t_0;
	else
		tmp = x + t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -1.15e-298], N[(y * t$95$0), $MachinePrecision], N[(x + t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y \cdot \left(z - x\right)\\
\mathbf{if}\;z \leq -1.15 \cdot 10^{-298}:\\
\;\;\;\;y \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;x + t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.15e-298

    1. Initial program 88.3%

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

      \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
    4. Applied rewrites1.0%

      \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto y \cdot \color{blue}{z} \]
    6. Applied rewrites2.0%

      \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
    7. Taylor expanded in x around inf

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

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

    if -1.15e-298 < z

    1. Initial program 89.0%

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

      \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
    4. Applied rewrites10.6%

      \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 67.6% accurate, 1.2× speedup?

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

\\
y \cdot \frac{z - x}{z}
\end{array}
Derivation
  1. Initial program 88.7%

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

    \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
  4. Applied rewrites5.8%

    \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto y \cdot \color{blue}{z} \]
  6. Applied rewrites6.0%

    \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
  7. Taylor expanded in x around inf

    \[\leadsto y \cdot \left(x \cdot \left(\frac{z}{x} - \color{blue}{1}\right)\right) \]
  8. Applied rewrites64.0%

    \[\leadsto y \cdot \frac{z - x}{z} \]
  9. Add Preprocessing

Alternative 4: 7.4% accurate, 2.6× speedup?

\[\begin{array}{l} \\ y \cdot \left(z - x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* y (- z x)))
double code(double x, double y, double z) {
	return y * (z - x);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = y * (z - x)
end function
public static double code(double x, double y, double z) {
	return y * (z - x);
}
def code(x, y, z):
	return y * (z - x)
function code(x, y, z)
	return Float64(y * Float64(z - x))
end
function tmp = code(x, y, z)
	tmp = y * (z - x);
end
code[x_, y_, z_] := N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
y \cdot \left(z - x\right)
\end{array}
Derivation
  1. Initial program 88.7%

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

    \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
  4. Applied rewrites5.8%

    \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto y \cdot \color{blue}{z} \]
  6. Applied rewrites6.0%

    \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
  7. Add Preprocessing

Alternative 5: 3.2% accurate, 5.8× speedup?

\[\begin{array}{l} \\ z - x \end{array} \]
(FPCore (x y z) :precision binary64 (- z x))
double code(double x, double y, double z) {
	return z - x;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = z - x
end function
public static double code(double x, double y, double z) {
	return z - x;
}
def code(x, y, z):
	return z - x
function code(x, y, z)
	return Float64(z - x)
end
function tmp = code(x, y, z)
	tmp = z - x;
end
code[x_, y_, z_] := N[(z - x), $MachinePrecision]
\begin{array}{l}

\\
z - x
\end{array}
Derivation
  1. Initial program 88.7%

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

    \[\leadsto \color{blue}{y + x \cdot \left(-1 \cdot \frac{y}{z} + \frac{1}{z}\right)} \]
  4. Applied rewrites5.8%

    \[\leadsto \color{blue}{x + y \cdot \left(z - x\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto x \cdot \left(1 + -1 \cdot y\right) + \color{blue}{y \cdot z} \]
  6. Applied rewrites3.1%

    \[\leadsto z - \color{blue}{x} \]
  7. Add Preprocessing

Developer Target 1: 94.3% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \left(y + \frac{x}{z}\right) - \frac{y}{\frac{z}{x}} \end{array} \]
(FPCore (x y z) :precision binary64 (- (+ y (/ x z)) (/ y (/ z x))))
double code(double x, double y, double z) {
	return (y + (x / z)) - (y / (z / x));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (y + (x / z)) - (y / (z / x))
end function
public static double code(double x, double y, double z) {
	return (y + (x / z)) - (y / (z / x));
}
def code(x, y, z):
	return (y + (x / z)) - (y / (z / x))
function code(x, y, z)
	return Float64(Float64(y + Float64(x / z)) - Float64(y / Float64(z / x)))
end
function tmp = code(x, y, z)
	tmp = (y + (x / z)) - (y / (z / x));
end
code[x_, y_, z_] := N[(N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision] - N[(y / N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(y + \frac{x}{z}\right) - \frac{y}{\frac{z}{x}}
\end{array}

Reproduce

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

  :alt
  (! :herbie-platform default (- (+ y (/ x z)) (/ y (/ z x))))

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