Codec.Picture.Types:toneMapping from JuicyPixels-3.2.6.1

Percentage Accurate: 88.6% → 99.9%
Time: 7.6s
Alternatives: 10
Speedup: 1.0×

Specification

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

\\
\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1}
\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 10 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.6% accurate, 1.0× speedup?

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

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

Alternative 1: 99.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{x + 1}\\ t\_0 + t\_0 \cdot \frac{x}{y} \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ x (+ x 1.0)))) (+ t_0 (* t_0 (/ x y)))))
double code(double x, double y) {
	double t_0 = x / (x + 1.0);
	return t_0 + (t_0 * (x / y));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    t_0 = x / (x + 1.0d0)
    code = t_0 + (t_0 * (x / y))
end function
public static double code(double x, double y) {
	double t_0 = x / (x + 1.0);
	return t_0 + (t_0 * (x / y));
}
def code(x, y):
	t_0 = x / (x + 1.0)
	return t_0 + (t_0 * (x / y))
function code(x, y)
	t_0 = Float64(x / Float64(x + 1.0))
	return Float64(t_0 + Float64(t_0 * Float64(x / y)))
end
function tmp = code(x, y)
	t_0 = x / (x + 1.0);
	tmp = t_0 + (t_0 * (x / y));
end
code[x_, y_] := Block[{t$95$0 = N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 + N[(t$95$0 * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{x + 1}\\
t\_0 + t\_0 \cdot \frac{x}{y}
\end{array}
\end{array}
Derivation
  1. Initial program 90.1%

    \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. *-commutative90.1%

      \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
    2. associate-/l*99.9%

      \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  4. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  5. Step-by-step derivation
    1. *-commutative99.9%

      \[\leadsto \color{blue}{\frac{x}{x + 1} \cdot \left(\frac{x}{y} + 1\right)} \]
    2. +-commutative99.9%

      \[\leadsto \frac{x}{x + 1} \cdot \color{blue}{\left(1 + \frac{x}{y}\right)} \]
    3. distribute-lft-in99.9%

      \[\leadsto \color{blue}{\frac{x}{x + 1} \cdot 1 + \frac{x}{x + 1} \cdot \frac{x}{y}} \]
    4. *-commutative99.9%

      \[\leadsto \color{blue}{1 \cdot \frac{x}{x + 1}} + \frac{x}{x + 1} \cdot \frac{x}{y} \]
    5. *-un-lft-identity99.9%

      \[\leadsto \color{blue}{\frac{x}{x + 1}} + \frac{x}{x + 1} \cdot \frac{x}{y} \]
  6. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\frac{x}{x + 1} + \frac{x}{x + 1} \cdot \frac{x}{y}} \]
  7. Add Preprocessing

Alternative 2: 86.4% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
t_0 := 1 + \frac{x}{y}\\
\mathbf{if}\;x \leq -24500000000000:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq -4.9 \cdot 10^{-57}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{x + 1}\\

\mathbf{elif}\;x \leq 2400000:\\
\;\;\;\;\frac{x}{x + 1}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -2.45e13 or 2.4e6 < x

    1. Initial program 79.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 79.6%

      \[\leadsto \frac{x \cdot \left(\frac{x}{y} + 1\right)}{\color{blue}{x}} \]
    4. Taylor expanded in x around 0 99.7%

      \[\leadsto \color{blue}{1 + \frac{x}{y}} \]

    if -2.45e13 < x < -4.89999999999999988e-57

    1. Initial program 99.6%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Step-by-step derivation
      1. associate-/l*99.7%

        \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 82.6%

      \[\leadsto x \cdot \frac{\color{blue}{\frac{x}{y}}}{x + 1} \]

    if -4.89999999999999988e-57 < x < 2.4e6

    1. Initial program 99.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf 81.2%

      \[\leadsto \color{blue}{\frac{x}{1 + x}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification90.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -24500000000000:\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{elif}\;x \leq -4.9 \cdot 10^{-57}:\\ \;\;\;\;x \cdot \frac{\frac{x}{y}}{x + 1}\\ \mathbf{elif}\;x \leq 2400000:\\ \;\;\;\;\frac{x}{x + 1}\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{x}{y}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 86.4% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
t_0 := 1 + \frac{x}{y}\\
\mathbf{if}\;x \leq -24500000000000:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq -8.6 \cdot 10^{-57}:\\
\;\;\;\;\frac{x}{y + \frac{y}{x}}\\

\mathbf{elif}\;x \leq 15500000:\\
\;\;\;\;\frac{x}{x + 1}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -2.45e13 or 1.55e7 < x

    1. Initial program 79.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 79.6%

      \[\leadsto \frac{x \cdot \left(\frac{x}{y} + 1\right)}{\color{blue}{x}} \]
    4. Taylor expanded in x around 0 99.7%

      \[\leadsto \color{blue}{1 + \frac{x}{y}} \]

    if -2.45e13 < x < -8.60000000000000043e-57

    1. Initial program 99.6%

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

      \[\leadsto \frac{x \cdot \color{blue}{\frac{x + y}{y}}}{x + 1} \]
    4. Step-by-step derivation
      1. clear-num99.4%

        \[\leadsto \frac{x \cdot \color{blue}{\frac{1}{\frac{y}{x + y}}}}{x + 1} \]
      2. un-div-inv99.4%

        \[\leadsto \frac{\color{blue}{\frac{x}{\frac{y}{x + y}}}}{x + 1} \]
    5. Applied egg-rr99.4%

      \[\leadsto \frac{\color{blue}{\frac{x}{\frac{y}{x + y}}}}{x + 1} \]
    6. Taylor expanded in y around 0 82.2%

      \[\leadsto \color{blue}{\frac{{x}^{2}}{y \cdot \left(1 + x\right)}} \]
    7. Step-by-step derivation
      1. +-commutative82.2%

        \[\leadsto \frac{{x}^{2}}{y \cdot \color{blue}{\left(x + 1\right)}} \]
      2. *-commutative82.2%

        \[\leadsto \frac{{x}^{2}}{\color{blue}{\left(x + 1\right) \cdot y}} \]
      3. associate-/r*82.6%

        \[\leadsto \color{blue}{\frac{\frac{{x}^{2}}{x + 1}}{y}} \]
      4. unpow282.6%

        \[\leadsto \frac{\frac{\color{blue}{x \cdot x}}{x + 1}}{y} \]
      5. associate-*l/82.5%

        \[\leadsto \frac{\color{blue}{\frac{x}{x + 1} \cdot x}}{y} \]
      6. associate-/r/82.3%

        \[\leadsto \frac{\color{blue}{\frac{x}{\frac{x + 1}{x}}}}{y} \]
      7. associate-/r*82.1%

        \[\leadsto \color{blue}{\frac{x}{\frac{x + 1}{x} \cdot y}} \]
      8. *-commutative82.1%

        \[\leadsto \frac{x}{\color{blue}{y \cdot \frac{x + 1}{x}}} \]
      9. *-rgt-identity82.1%

        \[\leadsto \frac{x}{y \cdot \frac{\color{blue}{\left(x + 1\right) \cdot 1}}{x}} \]
      10. associate-*r/82.2%

        \[\leadsto \frac{x}{y \cdot \color{blue}{\left(\left(x + 1\right) \cdot \frac{1}{x}\right)}} \]
      11. distribute-lft1-in82.1%

        \[\leadsto \frac{x}{y \cdot \color{blue}{\left(x \cdot \frac{1}{x} + \frac{1}{x}\right)}} \]
      12. rgt-mult-inverse82.2%

        \[\leadsto \frac{x}{y \cdot \left(\color{blue}{1} + \frac{1}{x}\right)} \]
      13. distribute-rgt-in82.1%

        \[\leadsto \frac{x}{\color{blue}{1 \cdot y + \frac{1}{x} \cdot y}} \]
      14. *-lft-identity82.1%

        \[\leadsto \frac{x}{\color{blue}{y} + \frac{1}{x} \cdot y} \]
      15. associate-*l/82.1%

        \[\leadsto \frac{x}{y + \color{blue}{\frac{1 \cdot y}{x}}} \]
      16. *-lft-identity82.1%

        \[\leadsto \frac{x}{y + \frac{\color{blue}{y}}{x}} \]
    8. Simplified82.1%

      \[\leadsto \color{blue}{\frac{x}{y + \frac{y}{x}}} \]

    if -8.60000000000000043e-57 < x < 1.55e7

    1. Initial program 99.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf 81.2%

      \[\leadsto \color{blue}{\frac{x}{1 + x}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification90.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -24500000000000:\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{elif}\;x \leq -8.6 \cdot 10^{-57}:\\ \;\;\;\;\frac{x}{y + \frac{y}{x}}\\ \mathbf{elif}\;x \leq 15500000:\\ \;\;\;\;\frac{x}{x + 1}\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{x}{y}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 86.7% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1400 \lor \neg \left(x \leq 3100000\right):\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{x + 1}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= x -1400.0) (not (<= x 3100000.0)))
   (+ 1.0 (/ x y))
   (/ x (+ x 1.0))))
double code(double x, double y) {
	double tmp;
	if ((x <= -1400.0) || !(x <= 3100000.0)) {
		tmp = 1.0 + (x / y);
	} else {
		tmp = x / (x + 1.0);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x <= (-1400.0d0)) .or. (.not. (x <= 3100000.0d0))) then
        tmp = 1.0d0 + (x / y)
    else
        tmp = x / (x + 1.0d0)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x <= -1400.0) || !(x <= 3100000.0)) {
		tmp = 1.0 + (x / y);
	} else {
		tmp = x / (x + 1.0);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x <= -1400.0) or not (x <= 3100000.0):
		tmp = 1.0 + (x / y)
	else:
		tmp = x / (x + 1.0)
	return tmp
function code(x, y)
	tmp = 0.0
	if ((x <= -1400.0) || !(x <= 3100000.0))
		tmp = Float64(1.0 + Float64(x / y));
	else
		tmp = Float64(x / Float64(x + 1.0));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x <= -1400.0) || ~((x <= 3100000.0)))
		tmp = 1.0 + (x / y);
	else
		tmp = x / (x + 1.0);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[x, -1400.0], N[Not[LessEqual[x, 3100000.0]], $MachinePrecision]], N[(1.0 + N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1400 \lor \neg \left(x \leq 3100000\right):\\
\;\;\;\;1 + \frac{x}{y}\\

\mathbf{else}:\\
\;\;\;\;\frac{x}{x + 1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1400 or 3.1e6 < x

    1. Initial program 80.5%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 79.0%

      \[\leadsto \frac{x \cdot \left(\frac{x}{y} + 1\right)}{\color{blue}{x}} \]
    4. Taylor expanded in x around 0 98.5%

      \[\leadsto \color{blue}{1 + \frac{x}{y}} \]

    if -1400 < x < 3.1e6

    1. Initial program 99.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf 75.6%

      \[\leadsto \color{blue}{\frac{x}{1 + x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1400 \lor \neg \left(x \leq 3100000\right):\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{x + 1}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 86.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.95 \cdot 10^{-7}\right):\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= x -1.0) (not (<= x 1.95e-7))) (+ 1.0 (/ x y)) x))
double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.95e-7)) {
		tmp = 1.0 + (x / y);
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x <= (-1.0d0)) .or. (.not. (x <= 1.95d-7))) then
        tmp = 1.0d0 + (x / y)
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.95e-7)) {
		tmp = 1.0 + (x / y);
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x <= -1.0) or not (x <= 1.95e-7):
		tmp = 1.0 + (x / y)
	else:
		tmp = x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((x <= -1.0) || !(x <= 1.95e-7))
		tmp = Float64(1.0 + Float64(x / y));
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x <= -1.0) || ~((x <= 1.95e-7)))
		tmp = 1.0 + (x / y);
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.95e-7]], $MachinePrecision]], N[(1.0 + N[(x / y), $MachinePrecision]), $MachinePrecision], x]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.95 \cdot 10^{-7}\right):\\
\;\;\;\;1 + \frac{x}{y}\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1 or 1.95000000000000012e-7 < x

    1. Initial program 80.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 77.8%

      \[\leadsto \frac{x \cdot \left(\frac{x}{y} + 1\right)}{\color{blue}{x}} \]
    4. Taylor expanded in x around 0 96.8%

      \[\leadsto \color{blue}{1 + \frac{x}{y}} \]

    if -1 < x < 1.95000000000000012e-7

    1. Initial program 99.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative99.9%

        \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
      2. associate-/l*99.9%

        \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
    4. Applied egg-rr99.9%

      \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
    5. Taylor expanded in x around 0 75.4%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.95 \cdot 10^{-7}\right):\\ \;\;\;\;1 + \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 74.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1100000\right):\\ \;\;\;\;\frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= x -1.0) (not (<= x 1100000.0))) (/ x y) x))
double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1100000.0)) {
		tmp = x / y;
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x <= (-1.0d0)) .or. (.not. (x <= 1100000.0d0))) then
        tmp = x / y
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1100000.0)) {
		tmp = x / y;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x <= -1.0) or not (x <= 1100000.0):
		tmp = x / y
	else:
		tmp = x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((x <= -1.0) || !(x <= 1100000.0))
		tmp = Float64(x / y);
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x <= -1.0) || ~((x <= 1100000.0)))
		tmp = x / y;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1100000.0]], $MachinePrecision]], N[(x / y), $MachinePrecision], x]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1100000\right):\\
\;\;\;\;\frac{x}{y}\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1 or 1.1e6 < x

    1. Initial program 80.5%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative80.5%

        \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
      2. associate-/l*100.0%

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

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

      \[\leadsto \color{blue}{\frac{x}{y}} \]

    if -1 < x < 1.1e6

    1. Initial program 99.9%

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative99.9%

        \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
      2. associate-/l*99.9%

        \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
    4. Applied egg-rr99.9%

      \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
    5. Taylor expanded in x around 0 73.9%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1100000\right):\\ \;\;\;\;\frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 99.9% accurate, 1.0× speedup?

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

\\
\frac{x}{x + 1} \cdot \left(1 + \frac{x}{y}\right)
\end{array}
Derivation
  1. Initial program 90.1%

    \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. *-commutative90.1%

      \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
    2. associate-/l*99.9%

      \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  4. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  5. Final simplification99.9%

    \[\leadsto \frac{x}{x + 1} \cdot \left(1 + \frac{x}{y}\right) \]
  6. Add Preprocessing

Alternative 8: 99.9% accurate, 1.0× speedup?

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

\\
x \cdot \frac{1 + \frac{x}{y}}{x + 1}
\end{array}
Derivation
  1. Initial program 90.1%

    \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
  2. Step-by-step derivation
    1. associate-/l*99.9%

      \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
  4. Add Preprocessing
  5. Final simplification99.9%

    \[\leadsto x \cdot \frac{1 + \frac{x}{y}}{x + 1} \]
  6. Add Preprocessing

Alternative 9: 39.2% accurate, 11.0× speedup?

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

\\
x
\end{array}
Derivation
  1. Initial program 90.1%

    \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. *-commutative90.1%

      \[\leadsto \frac{\color{blue}{\left(\frac{x}{y} + 1\right) \cdot x}}{x + 1} \]
    2. associate-/l*99.9%

      \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  4. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\left(\frac{x}{y} + 1\right) \cdot \frac{x}{x + 1}} \]
  5. Taylor expanded in x around 0 38.5%

    \[\leadsto \color{blue}{x} \]
  6. Add Preprocessing

Alternative 10: 3.1% accurate, 11.0× speedup?

\[\begin{array}{l} \\ -1 \end{array} \]
(FPCore (x y) :precision binary64 -1.0)
double code(double x, double y) {
	return -1.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = -1.0d0
end function
public static double code(double x, double y) {
	return -1.0;
}
def code(x, y):
	return -1.0
function code(x, y)
	return -1.0
end
function tmp = code(x, y)
	tmp = -1.0;
end
code[x_, y_] := -1.0
\begin{array}{l}

\\
-1
\end{array}
Derivation
  1. Initial program 90.1%

    \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
  2. Step-by-step derivation
    1. associate-/l*99.9%

      \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot \frac{\frac{x}{y} + 1}{x + 1}} \]
  4. Add Preprocessing
  5. Taylor expanded in x around 0 38.5%

    \[\leadsto x \cdot \color{blue}{1} \]
  6. Step-by-step derivation
    1. expm1-log1p-u37.8%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(x \cdot 1\right)\right)} \]
    2. *-rgt-identity37.8%

      \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{x}\right)\right) \]
    3. log1p-define4.1%

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\log \left(1 + x\right)}\right) \]
    4. +-commutative4.1%

      \[\leadsto \mathsf{expm1}\left(\log \color{blue}{\left(x + 1\right)}\right) \]
    5. expm1-undefine4.1%

      \[\leadsto \color{blue}{e^{\log \left(x + 1\right)} - 1} \]
    6. add-exp-log4.9%

      \[\leadsto \color{blue}{\left(x + 1\right)} - 1 \]
  7. Applied egg-rr4.9%

    \[\leadsto \color{blue}{\left(x + 1\right) - 1} \]
  8. Taylor expanded in x around inf 3.8%

    \[\leadsto \color{blue}{x} - 1 \]
  9. Taylor expanded in x around 0 3.1%

    \[\leadsto \color{blue}{-1} \]
  10. Add Preprocessing

Developer Target 1: 99.9% accurate, 0.8× speedup?

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

\\
\frac{x}{1} \cdot \frac{\frac{x}{y} + 1}{x + 1}
\end{array}

Reproduce

?
herbie shell --seed 2024136 
(FPCore (x y)
  :name "Codec.Picture.Types:toneMapping from JuicyPixels-3.2.6.1"
  :precision binary64

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

  (/ (* x (+ (/ x y) 1.0)) (+ x 1.0)))