Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3

?

Percentage Accurate: 87.7% → 99.4%
Time: 9.2s
Precision: binary64
Cost: 841

?

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

\mathbf{else}:\\
\;\;\;\;\frac{x}{z} \cdot \left(\left(y + 1\right) - z\right)\\


\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

Original87.7%
Target99.4%
Herbie99.4%
\[\begin{array}{l} \mathbf{if}\;x < -2.71483106713436 \cdot 10^{-162}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \mathbf{elif}\;x < 3.874108816439546 \cdot 10^{-197}:\\ \;\;\;\;\left(x \cdot \left(\left(y - z\right) + 1\right)\right) \cdot \frac{1}{z}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if z < -2.09999999999999989e94 or 1.4e16 < z

    1. Initial program 76.4%

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified93.6%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x} \]
      Step-by-step derivation

      [Start]76.4%

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

      +-commutative [=>]76.4%

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

      sub-neg [=>]76.4%

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

      +-commutative [=>]76.4%

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

      associate-+r+ [=>]76.4%

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

      unsub-neg [=>]76.4%

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

      associate-+l- [=>]76.4%

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

      distribute-lft-out-- [<=]76.4%

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

      *-rgt-identity [=>]76.4%

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

      distribute-rgt-out-- [<=]76.1%

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

      sub-neg [=>]76.1%

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

      +-commutative [=>]76.1%

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

      associate--r+ [=>]76.1%

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

      div-sub [=>]76.1%

      \[ \color{blue}{\frac{x - \left(-y \cdot x\right)}{z} - \frac{z \cdot x}{z}} \]
    3. Taylor expanded in y around inf 93.6%

      \[\leadsto \color{blue}{\frac{y \cdot x}{z}} - x \]
    4. Simplified100.0%

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

      [Start]93.6%

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

      associate-/l* [=>]93.8%

      \[ \color{blue}{\frac{y}{\frac{z}{x}}} - x \]

      associate-/r/ [=>]100.0%

      \[ \color{blue}{\frac{y}{z} \cdot x} - x \]

    if -2.09999999999999989e94 < z < 1.4e16

    1. Initial program 98.1%

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified91.4%

      \[\leadsto \color{blue}{\frac{x}{\frac{z}{\left(y - z\right) + 1}}} \]
      Step-by-step derivation

      [Start]98.1%

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

      associate-/l* [=>]91.4%

      \[ \color{blue}{\frac{x}{\frac{z}{\left(y - z\right) + 1}}} \]
    3. Taylor expanded in x around 0 98.1%

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

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

      [Start]98.1%

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

      associate-*l/ [<=]99.9%

      \[ \color{blue}{\frac{x}{z} \cdot \left(\left(1 + y\right) - z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -2.1 \cdot 10^{+94} \lor \neg \left(z \leq 1.4 \cdot 10^{+16}\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(\left(y + 1\right) - z\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.4%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -2.1 \cdot 10^{+94} \lor \neg \left(z \leq 1.4 \cdot 10^{+16}\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(\left(y + 1\right) - z\right)\\ \end{array} \]
Alternative 2
Accuracy97.9%
Cost6980
\[\begin{array}{l} \mathbf{if}\;z \leq -2.6 \cdot 10^{+36}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \end{array} \]
Alternative 3
Accuracy98.1%
Cost1220
\[\begin{array}{l} t_0 := \left(y - z\right) + 1\\ \mathbf{if}\;\frac{x \cdot t_0}{z} \leq 10^{+21}:\\ \;\;\;\;\frac{x}{\frac{z}{t_0}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(\left(y + 1\right) - z\right)\\ \end{array} \]
Alternative 4
Accuracy65.9%
Cost1112
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;z \leq -7.2 \cdot 10^{+63}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -1.05 \cdot 10^{-163}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -1.25 \cdot 10^{-229}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq -4 \cdot 10^{-275}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.35 \cdot 10^{-88}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq 110000000000:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 5
Accuracy65.5%
Cost1112
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;z \leq -3.1 \cdot 10^{+63}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -4.5 \cdot 10^{-165}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -4.1 \cdot 10^{-228}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq -7 \cdot 10^{-275}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 3.6 \cdot 10^{-89}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq 14000000000:\\ \;\;\;\;\frac{y}{z} \cdot x\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 6
Accuracy98.8%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(y + 1\right)\\ \end{array} \]
Alternative 7
Accuracy86.6%
Cost712
\[\begin{array}{l} \mathbf{if}\;z \leq -3.1 \cdot 10^{+63}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 1.65 \cdot 10^{-5}:\\ \;\;\;\;\frac{x}{z} \cdot \left(y + 1\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 8
Accuracy85.2%
Cost585
\[\begin{array}{l} \mathbf{if}\;y \leq -8.5 \cdot 10^{+17} \lor \neg \left(y \leq 51600\right):\\ \;\;\;\;y \cdot \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 9
Accuracy85.0%
Cost585
\[\begin{array}{l} \mathbf{if}\;y \leq -35000000000000 \lor \neg \left(y \leq 51600\right):\\ \;\;\;\;\frac{y \cdot x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 10
Accuracy65.3%
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -11.6:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 2300000:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 11
Accuracy39.0%
Cost128
\[-x \]

Reproduce?

herbie shell --seed 2023271 
(FPCore (x y z)
  :name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< x -2.71483106713436e-162) (- (* (+ 1.0 y) (/ x z)) x) (if (< x 3.874108816439546e-197) (* (* x (+ (- y z) 1.0)) (/ 1.0 z)) (- (* (+ 1.0 y) (/ x z)) x)))

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