Skip to content

[WIP] Cast power function results to float #5988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions test-data/stdlib-samples/3.2/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def triangular(self, low: float = 0.0, high: float = 1.0,
u = 1.0 - u
c = 1.0 - c
low, high = high, low
return low + (high - low) * (u * c) ** 0.5
return low + (high - low) * cast(float, (u * c) ** 0.5)

## -------------------- normal distribution --------------------

Expand Down Expand Up @@ -531,12 +531,12 @@ def gammavariate(self, alpha: float, beta: float) -> float:
b = (_e + alpha)/_e
p = b*u
if p <= 1.0:
x = p ** (1.0/alpha)
x = cast(float, p ** (1.0/alpha))
else:
x = -_log((b-p)/alpha)
u1 = random()
if p > 1.0:
if u1 <= x ** (alpha - 1.0):
if u1 <= cast(float, x ** (alpha - 1.0)):
break
elif u1 <= _exp(-x):
break
Expand Down Expand Up @@ -620,7 +620,7 @@ def paretovariate(self, alpha: float) -> float:
# Jain, pg. 495

u = 1.0 - self.random()
return 1.0 / u ** (1.0/alpha)
return 1.0 / cast(float, u ** (1.0/alpha))

## -------------------- Weibull --------------------

Expand All @@ -633,7 +633,7 @@ def weibullvariate(self, alpha: float, beta: float) -> float:
# Jain, pg. 499; bug fix courtesy Bill Arms

u = 1.0 - self.random()
return alpha * (-_log(u)) ** (1.0/beta)
return alpha * cast(float, (-_log(u)) ** (1.0/beta))

## --------------- Operating System Random Source ------------------

Expand Down
4 changes: 2 additions & 2 deletions test-data/stdlib-samples/3.2/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,13 @@ def test_randrange_bug_1590891(self) -> None:
self.assertTrue(stop < x <= start)
self.assertEqual((x+stop)%step, 0)

def gamma(z: float, sqrt2pi: float = (2.0*pi)**0.5) -> float:
def gamma(z: float, sqrt2pi: float = cast(float, (2.0*pi)**0.5)) -> float:
# Reflection to right half of complex plane
if z < 0.5:
return pi / sin(pi*z) / gamma(1.0-z)
# Lanczos approximation with g=7
az = z + (7.0 - 0.5)
return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
return cast(float, az ** (z-0.5)) / exp(az) * sqrt2pi * fsum([
0.9999999999995183,
676.5203681218835 / z,
-1259.139216722289 / (z+1.0),
Expand Down