Average Error: 9.4 → 0.1
Time: 3.8s
Precision: binary64
\[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1391710068553142 \cdot 10^{+38} \lor \neg \left(x \leq 16483647715.548288\right):\\ \;\;\;\;\left(1 + \frac{x}{y}\right) - \frac{1}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, \frac{x}{y}, x\right)}{x + 1}\\ \end{array} \]
\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1}
\begin{array}{l}
\mathbf{if}\;x \leq -1.1391710068553142 \cdot 10^{+38} \lor \neg \left(x \leq 16483647715.548288\right):\\
\;\;\;\;\left(1 + \frac{x}{y}\right) - \frac{1}{y}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, \frac{x}{y}, x\right)}{x + 1}\\


\end{array}
(FPCore (x y) :precision binary64 (/ (* x (+ (/ x y) 1.0)) (+ x 1.0)))
(FPCore (x y)
 :precision binary64
 (if (or (<= x -1.1391710068553142e+38) (not (<= x 16483647715.548288)))
   (- (+ 1.0 (/ x y)) (/ 1.0 y))
   (/ (fma x (/ x y) x) (+ x 1.0))))
double code(double x, double y) {
	return (x * ((x / y) + 1.0)) / (x + 1.0);
}
double code(double x, double y) {
	double tmp;
	if ((x <= -1.1391710068553142e+38) || !(x <= 16483647715.548288)) {
		tmp = (1.0 + (x / y)) - (1.0 / y);
	} else {
		tmp = fma(x, (x / y), x) / (x + 1.0);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original9.4
Target0.1
Herbie0.1
\[\frac{x}{1} \cdot \frac{\frac{x}{y} + 1}{x + 1} \]

Derivation

  1. Split input into 2 regimes
  2. if x < -1.1391710068553142e38 or 16483647715.5482883 < x

    1. Initial program 24.1

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Simplified24.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, \frac{x}{y}, x\right)}{x + 1}} \]
    3. Taylor expanded in x around inf 0.1

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

    if -1.1391710068553142e38 < x < 16483647715.5482883

    1. Initial program 0.2

      \[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
    2. Simplified0.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, \frac{x}{y}, x\right)}{x + 1}} \]
    3. Applied *-un-lft-identity_binary640.1

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(x, \frac{x}{y}, x\right)}}{x + 1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.1391710068553142 \cdot 10^{+38} \lor \neg \left(x \leq 16483647715.548288\right):\\ \;\;\;\;\left(1 + \frac{x}{y}\right) - \frac{1}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, \frac{x}{y}, x\right)}{x + 1}\\ \end{array} \]

Reproduce

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

  :herbie-target
  (* (/ x 1.0) (/ (+ (/ x y) 1.0) (+ x 1.0)))

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