Average Error: 9.0 → 0.1
Time: 3.4s
Precision: binary64
\[\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -4.747139500856354 \cdot 10^{+27} \lor \neg \left(x \leq 4590262224990375\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 -4.747139500856354 \cdot 10^{+27} \lor \neg \left(x \leq 4590262224990375\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 -4.747139500856354e+27) (not (<= x 4590262224990375.0)))
   (- (+ 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 <= -4.747139500856354e+27) || !(x <= 4590262224990375.0)) {
		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.0
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 < -4.7471395008563539e27 or 4590262224990375 < x

    1. Initial program 22.6

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

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

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

    if -4.7471395008563539e27 < x < 4590262224990375

    1. Initial program 0.2

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

      \[\leadsto \color{blue}{\frac{\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 -4.747139500856354 \cdot 10^{+27} \lor \neg \left(x \leq 4590262224990375\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 2021313 
(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)))