Crypto.Random.Test:calculate from crypto-random-0.0.9

Percentage Accurate: 92.9% → 99.9%
Time: 1.3s
Alternatives: 3
Speedup: 1.1×

Specification

?
\[x + \frac{y \cdot y}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ x (/ (* y y) z)))
double code(double x, double y, double z) {
	return x + ((y * y) / z);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + ((y * y) / z)
end function
public static double code(double x, double y, double z) {
	return x + ((y * y) / z);
}
def code(x, y, z):
	return x + ((y * y) / z)
function code(x, y, z)
	return Float64(x + Float64(Float64(y * y) / z))
end
function tmp = code(x, y, z)
	tmp = x + ((y * y) / z);
end
code[x_, y_, z_] := N[(x + N[(N[(y * y), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	x + ((y * y) / z)
END code
x + \frac{y \cdot y}{z}

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

\[x + \frac{y \cdot y}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ x (/ (* y y) z)))
double code(double x, double y, double z) {
	return x + ((y * y) / z);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + ((y * y) / z)
end function
public static double code(double x, double y, double z) {
	return x + ((y * y) / z);
}
def code(x, y, z):
	return x + ((y * y) / z)
function code(x, y, z)
	return Float64(x + Float64(Float64(y * y) / z))
end
function tmp = code(x, y, z)
	tmp = x + ((y * y) / z);
end
code[x_, y_, z_] := N[(x + N[(N[(y * y), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	x + ((y * y) / z)
END code
x + \frac{y \cdot y}{z}

Alternative 1: 99.9% accurate, 0.6× speedup?

\[\mathsf{fma}\left(0.5, \frac{y + y}{\frac{z}{y}}, x\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma 0.5 (/ (+ y y) (/ z y)) x))
double code(double x, double y, double z) {
	return fma(0.5, ((y + y) / (z / y)), x);
}
function code(x, y, z)
	return fma(0.5, Float64(Float64(y + y) / Float64(z / y)), x)
end
code[x_, y_, z_] := N[(0.5 * N[(N[(y + y), $MachinePrecision] / N[(z / y), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	((5e-1) * ((y + y) / (z / y))) + x
END code
\mathsf{fma}\left(0.5, \frac{y + y}{\frac{z}{y}}, x\right)
Derivation
  1. Initial program 92.9%

    \[x + \frac{y \cdot y}{z} \]
  2. Step-by-step derivation
    1. Applied rewrites99.8%

      \[\leadsto \mathsf{fma}\left(\sqrt{\left|y\right|}, \sqrt{\left|y\right|} \cdot \frac{\left|y\right|}{z}, x\right) \]
    2. Step-by-step derivation
      1. Applied rewrites96.9%

        \[\leadsto \mathsf{fma}\left(\sqrt{\left|y\right|} \cdot \left|y\right|, \frac{\sqrt{\left|y\right|}}{z}, x\right) \]
      2. Step-by-step derivation
        1. Applied rewrites99.8%

          \[\leadsto \mathsf{fma}\left(0.5, \frac{\frac{y}{z}}{\frac{0.5}{y}}, x\right) \]
        2. Step-by-step derivation
          1. Applied rewrites99.9%

            \[\leadsto \mathsf{fma}\left(0.5, \frac{y + y}{\frac{z}{y}}, x\right) \]
          2. Add Preprocessing

          Alternative 2: 99.9% accurate, 0.8× speedup?

          \[\mathsf{fma}\left(y, y \cdot \frac{1}{z}, x\right) \]
          (FPCore (x y z)
            :precision binary64
            :pre TRUE
            (fma y (* y (/ 1.0 z)) x))
          double code(double x, double y, double z) {
          	return fma(y, (y * (1.0 / z)), x);
          }
          
          function code(x, y, z)
          	return fma(y, Float64(y * Float64(1.0 / z)), x)
          end
          
          code[x_, y_, z_] := N[(y * N[(y * N[(1.0 / z), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]
          
          f(x, y, z):
          	x in [-inf, +inf],
          	y in [-inf, +inf],
          	z in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x, y, z: real): real =
          	(y * (y * ((1) / z))) + x
          END code
          \mathsf{fma}\left(y, y \cdot \frac{1}{z}, x\right)
          
          Derivation
          1. Initial program 92.9%

            \[x + \frac{y \cdot y}{z} \]
          2. Step-by-step derivation
            1. Applied rewrites99.9%

              \[\leadsto \mathsf{fma}\left(y, \frac{y}{z}, x\right) \]
            2. Step-by-step derivation
              1. Applied rewrites99.9%

                \[\leadsto \mathsf{fma}\left(y, y \cdot \frac{1}{z}, x\right) \]
              2. Add Preprocessing

              Alternative 3: 99.9% accurate, 1.1× speedup?

              \[\mathsf{fma}\left(y, \frac{y}{z}, x\right) \]
              (FPCore (x y z)
                :precision binary64
                :pre TRUE
                (fma y (/ y z) x))
              double code(double x, double y, double z) {
              	return fma(y, (y / z), x);
              }
              
              function code(x, y, z)
              	return fma(y, Float64(y / z), x)
              end
              
              code[x_, y_, z_] := N[(y * N[(y / z), $MachinePrecision] + x), $MachinePrecision]
              
              f(x, y, z):
              	x in [-inf, +inf],
              	y in [-inf, +inf],
              	z in [-inf, +inf]
              code: THEORY
              BEGIN
              f(x, y, z: real): real =
              	(y * (y / z)) + x
              END code
              \mathsf{fma}\left(y, \frac{y}{z}, x\right)
              
              Derivation
              1. Initial program 92.9%

                \[x + \frac{y \cdot y}{z} \]
              2. Step-by-step derivation
                1. Applied rewrites99.9%

                  \[\leadsto \mathsf{fma}\left(y, \frac{y}{z}, x\right) \]
                2. Add Preprocessing

                Reproduce

                ?
                herbie shell --seed 2026092 
                (FPCore (x y z)
                  :name "Crypto.Random.Test:calculate from crypto-random-0.0.9"
                  :precision binary64
                  (+ x (/ (* y y) z)))