Octave 3.8, oct_fill_randg

Percentage Accurate: 99.7% → 99.8%
Time: 2.9s
Alternatives: 1
Speedup: 1.2×

Specification

?
\[\begin{array}{l} t_0 := a - \frac{1}{3}\\ t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right) \end{array} \]
(FPCore (a rand)
 :precision binary64
 (let* ((t_0 (- a (/ 1.0 3.0))))
   (* t_0 (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 t_0))) rand)))))
double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(a, rand)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    real(8) :: t_0
    t_0 = a - (1.0d0 / 3.0d0)
    code = t_0 * (1.0d0 + ((1.0d0 / sqrt((9.0d0 * t_0))) * rand))
end function
public static double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / Math.sqrt((9.0 * t_0))) * rand));
}
def code(a, rand):
	t_0 = a - (1.0 / 3.0)
	return t_0 * (1.0 + ((1.0 / math.sqrt((9.0 * t_0))) * rand))
function code(a, rand)
	t_0 = Float64(a - Float64(1.0 / 3.0))
	return Float64(t_0 * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(9.0 * t_0))) * rand)))
end
function tmp = code(a, rand)
	t_0 = a - (1.0 / 3.0);
	tmp = t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
end
code[a_, rand_] := Block[{t$95$0 = N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(9.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
t_0 := a - \frac{1}{3}\\
t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\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.

Accuracy vs Speed?

Herbie found 1 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: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} t_0 := a - \frac{1}{3}\\ t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right) \end{array} \]
(FPCore (a rand)
 :precision binary64
 (let* ((t_0 (- a (/ 1.0 3.0))))
   (* t_0 (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 t_0))) rand)))))
double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(a, rand)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    real(8) :: t_0
    t_0 = a - (1.0d0 / 3.0d0)
    code = t_0 * (1.0d0 + ((1.0d0 / sqrt((9.0d0 * t_0))) * rand))
end function
public static double code(double a, double rand) {
	double t_0 = a - (1.0 / 3.0);
	return t_0 * (1.0 + ((1.0 / Math.sqrt((9.0 * t_0))) * rand));
}
def code(a, rand):
	t_0 = a - (1.0 / 3.0)
	return t_0 * (1.0 + ((1.0 / math.sqrt((9.0 * t_0))) * rand))
function code(a, rand)
	t_0 = Float64(a - Float64(1.0 / 3.0))
	return Float64(t_0 * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(9.0 * t_0))) * rand)))
end
function tmp = code(a, rand)
	t_0 = a - (1.0 / 3.0);
	tmp = t_0 * (1.0 + ((1.0 / sqrt((9.0 * t_0))) * rand));
end
code[a_, rand_] := Block[{t$95$0 = N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(9.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
t_0 := a - \frac{1}{3}\\
t\_0 \cdot \left(1 + \frac{1}{\sqrt{9 \cdot t\_0}} \cdot rand\right)
\end{array}

Alternative 1: 99.8% accurate, 1.2× speedup?

\[\left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, -3\right)}} \cdot rand\right) \]
(FPCore (a rand)
 :precision binary64
 (* (- a (/ 1.0 3.0)) (+ 1.0 (* (sqrt (/ 1.0 (fma 9.0 a -3.0))) rand))))
double code(double a, double rand) {
	return (a - (1.0 / 3.0)) * (1.0 + (sqrt((1.0 / fma(9.0, a, -3.0))) * rand));
}
function code(a, rand)
	return Float64(Float64(a - Float64(1.0 / 3.0)) * Float64(1.0 + Float64(sqrt(Float64(1.0 / fma(9.0, a, -3.0))) * rand)))
end
code[a_, rand_] := N[(N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision] * N[(1.0 + N[(N[Sqrt[N[(1.0 / N[(9.0 * a + -3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, -3\right)}} \cdot rand\right)
Derivation
  1. Initial program 99.7%

    \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
  2. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \color{blue}{\frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    2. metadata-evalN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{\color{blue}{\sqrt{1}}}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
    3. lift-sqrt.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{\sqrt{1}}{\color{blue}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    4. sqrt-undivN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \color{blue}{\sqrt{\frac{1}{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    5. lower-sqrt.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \color{blue}{\sqrt{\frac{1}{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    6. lower-/.f6499.8%

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\color{blue}{\frac{1}{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    7. lift-*.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\color{blue}{9 \cdot \left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    8. lift--.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{9 \cdot \color{blue}{\left(a - \frac{1}{3}\right)}}} \cdot rand\right) \]
    9. sub-flipN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{9 \cdot \color{blue}{\left(a + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right)}}} \cdot rand\right) \]
    10. distribute-lft-inN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\color{blue}{9 \cdot a + 9 \cdot \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}}} \cdot rand\right) \]
    11. *-commutativeN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{9 \cdot a + \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right) \cdot 9}}} \cdot rand\right) \]
    12. lower-fma.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\color{blue}{\mathsf{fma}\left(9, a, \left(\mathsf{neg}\left(\frac{1}{3}\right)\right) \cdot 9\right)}}} \cdot rand\right) \]
    13. lift-/.f64N/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, \left(\mathsf{neg}\left(\color{blue}{\frac{1}{3}}\right)\right) \cdot 9\right)}} \cdot rand\right) \]
    14. metadata-evalN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, \left(\mathsf{neg}\left(\color{blue}{\frac{1}{3}}\right)\right) \cdot 9\right)}} \cdot rand\right) \]
    15. metadata-evalN/A

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, \color{blue}{\frac{-1}{3}} \cdot 9\right)}} \cdot rand\right) \]
    16. metadata-eval99.8%

      \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \sqrt{\frac{1}{\mathsf{fma}\left(9, a, \color{blue}{-3}\right)}} \cdot rand\right) \]
  3. Applied rewrites99.8%

    \[\leadsto \left(a - \frac{1}{3}\right) \cdot \left(1 + \color{blue}{\sqrt{\frac{1}{\mathsf{fma}\left(9, a, -3\right)}}} \cdot rand\right) \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2025192 
(FPCore (a rand)
  :name "Octave 3.8, oct_fill_randg"
  :precision binary64
  (* (- a (/ 1.0 3.0)) (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 (- a (/ 1.0 3.0))))) rand))))