| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2b8c313 commit e9149a8
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | from ltypes import i32, f64, ccall | |
| 2 | 2 | ||
| 3 | 3 | e: f64 = 2.718281828459045235360287471352662497757 | |
| 4 | + eps: f64 = 1e-16 | ||
| 4 | 5 | ||
| 5 | 6 | #: TODO: Call `log` from C directly until we fix the multiple import issue | |
| 6 | 7 | def _log(x: f64) -> f64: | |
@@ -16,6 +17,11 @@ def _exp(x: f64) -> f64: | |||
| 16 | 17 | def _sqrt(x: f64) -> f64: | |
| 17 | 18 | return x**(1/2) | |
| 18 | 19 | ||
| 20 | + def _abs(x: f64) -> f64: | ||
| 21 | + if x < 0.0: | ||
| 22 | + return -x | ||
| 23 | + return x | ||
| 24 | + | ||
| 19 | 25 | def random() -> f64: | |
| 20 | 26 | """ | |
| 21 | 27 | Returns a random floating point number in the range [0.0, 1.0) | |
@@ -65,13 +71,13 @@ def expovariate(l: f64) -> f64: | |||
| 65 | 71 | Return a random number from an exponential distribution with parameter | |
| 66 | 72 | `l` (lambda). | |
| 67 | 73 | """ | |
| 68 | - assert l != 0.0 | ||
| 74 | + assert _abs(l) > eps | ||
| 69 | 75 | return -_log(1.0 - random()) / l | |
| 70 | 76 | ||
| 71 | 77 | def weibullvariate(alpha: f64, beta: f64) -> f64: | |
| 72 | 78 | """ | |
| 73 | 79 | Return a random number from a Weibull distribution with parameters `alpha` | |
| 74 | 80 | and `beta`. | |
| 75 | 81 | """ | |
| 76 | - assert beta != 0.0 | ||
| 82 | + assert _abs(beta) > eps | ||
| 77 | 83 | return alpha * (-_log(1.0 - random())) ** (1.0 / beta) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments