FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Apply suggestions · lcompilers/lpython@e9149a8 · GitHub

Commit e9149a8

Browse files
committed
Apply suggestions
1 parent 2b8c313 commit e9149a8

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎src/runtime/random.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ltypes import i32, f64, ccall
22

33
e: f64 = 2.718281828459045235360287471352662497757
4+
eps: f64 = 1e-16
45

56
#: TODO: Call `log` from C directly until we fix the multiple import issue
67
def _log(x: f64) -> f64:
@@ -16,6 +17,11 @@ def _exp(x: f64) -> f64:
1617
def _sqrt(x: f64) -> f64:
1718
return x**(1/2)
1819

20+
def _abs(x: f64) -> f64:
21+
if x < 0.0:
22+
return -x
23+
return x
24+
1925
def random() -> f64:
2026
"""
2127
Returns a random floating point number in the range [0.0, 1.0)
@@ -65,13 +71,13 @@ def expovariate(l: f64) -> f64:
6571
Return a random number from an exponential distribution with parameter
6672
`l` (lambda).
6773
"""
68-
assert l != 0.0
74+
assert _abs(l) > eps
6975
return -_log(1.0 - random()) / l
7076

7177
def weibullvariate(alpha: f64, beta: f64) -> f64:
7278
"""
7379
Return a random number from a Weibull distribution with parameters `alpha`
7480
and `beta`.
7581
"""
76-
assert beta != 0.0
82+
assert _abs(beta) > eps
7783
return alpha * (-_log(1.0 - random())) ** (1.0 / beta)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL