Octave 3.8, oct_fill_randg

?

Percentage Accurate: 99.7% → 99.8%
Time: 12.6s
Precision: binary64
Cost: 7104

?

\[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
\[a + \left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) - 0.3333333333333333\right) \]
(FPCore (a rand)
 :precision binary64
 (*
  (- a (/ 1.0 3.0))
  (+ 1.0 (* (/ 1.0 (sqrt (* 9.0 (- a (/ 1.0 3.0))))) rand))))
(FPCore (a rand)
 :precision binary64
 (+
  a
  (-
   (* 0.3333333333333333 (* (sqrt (- a 0.3333333333333333)) rand))
   0.3333333333333333)))
double code(double a, double rand) {
	return (a - (1.0 / 3.0)) * (1.0 + ((1.0 / sqrt((9.0 * (a - (1.0 / 3.0))))) * rand));
}
double code(double a, double rand) {
	return a + ((0.3333333333333333 * (sqrt((a - 0.3333333333333333)) * rand)) - 0.3333333333333333);
}
real(8) function code(a, rand)
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    code = (a - (1.0d0 / 3.0d0)) * (1.0d0 + ((1.0d0 / sqrt((9.0d0 * (a - (1.0d0 / 3.0d0))))) * rand))
end function
real(8) function code(a, rand)
    real(8), intent (in) :: a
    real(8), intent (in) :: rand
    code = a + ((0.3333333333333333d0 * (sqrt((a - 0.3333333333333333d0)) * rand)) - 0.3333333333333333d0)
end function
public static double code(double a, double rand) {
	return (a - (1.0 / 3.0)) * (1.0 + ((1.0 / Math.sqrt((9.0 * (a - (1.0 / 3.0))))) * rand));
}
public static double code(double a, double rand) {
	return a + ((0.3333333333333333 * (Math.sqrt((a - 0.3333333333333333)) * rand)) - 0.3333333333333333);
}
def code(a, rand):
	return (a - (1.0 / 3.0)) * (1.0 + ((1.0 / math.sqrt((9.0 * (a - (1.0 / 3.0))))) * rand))
def code(a, rand):
	return a + ((0.3333333333333333 * (math.sqrt((a - 0.3333333333333333)) * rand)) - 0.3333333333333333)
function code(a, rand)
	return Float64(Float64(a - Float64(1.0 / 3.0)) * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(9.0 * Float64(a - Float64(1.0 / 3.0))))) * rand)))
end
function code(a, rand)
	return Float64(a + Float64(Float64(0.3333333333333333 * Float64(sqrt(Float64(a - 0.3333333333333333)) * rand)) - 0.3333333333333333))
end
function tmp = code(a, rand)
	tmp = (a - (1.0 / 3.0)) * (1.0 + ((1.0 / sqrt((9.0 * (a - (1.0 / 3.0))))) * rand));
end
function tmp = code(a, rand)
	tmp = a + ((0.3333333333333333 * (sqrt((a - 0.3333333333333333)) * rand)) - 0.3333333333333333);
end
code[a_, rand_] := N[(N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision] * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(9.0 * N[(a - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, rand_] := N[(a + N[(N[(0.3333333333333333 * N[(N[Sqrt[N[(a - 0.3333333333333333), $MachinePrecision]], $MachinePrecision] * rand), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right)
a + \left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) - 0.3333333333333333\right)

Local Percentage Accuracy?

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.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 99.4%

    \[\left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]
  2. Simplified99.4%

    \[\leadsto \color{blue}{\left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{1}{\sqrt{\left(a + -0.3333333333333333\right) \cdot 9}} \cdot rand\right)} \]
    Step-by-step derivation

    [Start]99.4

    \[ \left(a - \frac{1}{3}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]

    sub-neg [=>]99.4

    \[ \color{blue}{\left(a + \left(-\frac{1}{3}\right)\right)} \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]

    metadata-eval [=>]99.4

    \[ \left(a + \left(-\color{blue}{0.3333333333333333}\right)\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]

    metadata-eval [=>]99.4

    \[ \left(a + \color{blue}{-0.3333333333333333}\right) \cdot \left(1 + \frac{1}{\sqrt{9 \cdot \left(a - \frac{1}{3}\right)}} \cdot rand\right) \]

    *-commutative [=>]99.4

    \[ \left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{1}{\sqrt{\color{blue}{\left(a - \frac{1}{3}\right) \cdot 9}}} \cdot rand\right) \]

    sub-neg [=>]99.4

    \[ \left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{1}{\sqrt{\color{blue}{\left(a + \left(-\frac{1}{3}\right)\right)} \cdot 9}} \cdot rand\right) \]

    metadata-eval [=>]99.4

    \[ \left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{1}{\sqrt{\left(a + \left(-\color{blue}{0.3333333333333333}\right)\right) \cdot 9}} \cdot rand\right) \]

    metadata-eval [=>]99.4

    \[ \left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{1}{\sqrt{\left(a + \color{blue}{-0.3333333333333333}\right) \cdot 9}} \cdot rand\right) \]
  3. Taylor expanded in rand around 0 99.8%

    \[\leadsto \color{blue}{\left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) + a\right) - 0.3333333333333333} \]
  4. Simplified99.8%

    \[\leadsto \color{blue}{a + \mathsf{fma}\left(0.3333333333333333, rand \cdot \sqrt{-0.3333333333333333 + a}, -0.3333333333333333\right)} \]
    Step-by-step derivation

    [Start]99.8

    \[ \left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) + a\right) - 0.3333333333333333 \]

    +-commutative [<=]99.8

    \[ \color{blue}{\left(a + 0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right)\right)} - 0.3333333333333333 \]

    associate--l+ [=>]99.8

    \[ \color{blue}{a + \left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) - 0.3333333333333333\right)} \]

    fma-neg [=>]99.8

    \[ a + \color{blue}{\mathsf{fma}\left(0.3333333333333333, \sqrt{a - 0.3333333333333333} \cdot rand, -0.3333333333333333\right)} \]

    *-commutative [=>]99.8

    \[ a + \mathsf{fma}\left(0.3333333333333333, \color{blue}{rand \cdot \sqrt{a - 0.3333333333333333}}, -0.3333333333333333\right) \]

    sub-neg [=>]99.8

    \[ a + \mathsf{fma}\left(0.3333333333333333, rand \cdot \sqrt{\color{blue}{a + \left(-0.3333333333333333\right)}}, -0.3333333333333333\right) \]

    metadata-eval [=>]99.8

    \[ a + \mathsf{fma}\left(0.3333333333333333, rand \cdot \sqrt{a + \color{blue}{-0.3333333333333333}}, -0.3333333333333333\right) \]

    +-commutative [=>]99.8

    \[ a + \mathsf{fma}\left(0.3333333333333333, rand \cdot \sqrt{\color{blue}{-0.3333333333333333 + a}}, -0.3333333333333333\right) \]

    metadata-eval [=>]99.8

    \[ a + \mathsf{fma}\left(0.3333333333333333, rand \cdot \sqrt{-0.3333333333333333 + a}, \color{blue}{-0.3333333333333333}\right) \]
  5. Taylor expanded in rand around 0 99.8%

    \[\leadsto a + \color{blue}{\left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) - 0.3333333333333333\right)} \]
  6. Final simplification99.8%

    \[\leadsto a + \left(0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) - 0.3333333333333333\right) \]

Alternatives

Alternative 1
Accuracy92.3%
Cost7113
\[\begin{array}{l} \mathbf{if}\;rand \leq -2.1 \cdot 10^{+65} \lor \neg \left(rand \leq 3.2 \cdot 10^{+74}\right):\\ \;\;\;\;0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right)\\ \mathbf{else}:\\ \;\;\;\;a - 0.3333333333333333\\ \end{array} \]
Alternative 2
Accuracy92.3%
Cost7113
\[\begin{array}{l} \mathbf{if}\;rand \leq -1.1 \cdot 10^{+66} \lor \neg \left(rand \leq 3.8 \cdot 10^{+73}\right):\\ \;\;\;\;\sqrt{a + -0.3333333333333333} \cdot \left(0.3333333333333333 \cdot rand\right)\\ \mathbf{else}:\\ \;\;\;\;a - 0.3333333333333333\\ \end{array} \]
Alternative 3
Accuracy98.8%
Cost7104
\[\left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{rand}{\sqrt{a \cdot 9}}\right) \]
Alternative 4
Accuracy98.8%
Cost7104
\[\left(a + -0.3333333333333333\right) \cdot \left(1 + \frac{rand}{\sqrt{a} \cdot 3}\right) \]
Alternative 5
Accuracy68.8%
Cost6980
\[\begin{array}{l} \mathbf{if}\;rand \leq 1.1 \cdot 10^{+74}:\\ \;\;\;\;a - 0.3333333333333333\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(rand \cdot rand\right) \cdot \left(a \cdot 0.1111111111111111\right)}\\ \end{array} \]
Alternative 6
Accuracy98.6%
Cost6976
\[a + 0.3333333333333333 \cdot \left(\sqrt{a - 0.3333333333333333} \cdot rand\right) \]
Alternative 7
Accuracy98.6%
Cost6976
\[a + rand \cdot \left(0.3333333333333333 \cdot \sqrt{a + -0.3333333333333333}\right) \]
Alternative 8
Accuracy62.9%
Cost192
\[a - 0.3333333333333333 \]
Alternative 9
Accuracy61.8%
Cost64
\[a \]

Error

Reproduce?

herbie shell --seed 2023160 
(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))))