Skip to content

Commit b5bc79f

Browse files
committed
animation: implement cbrt for lower python versions
1 parent 5fb6654 commit b5bc79f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Diff for: kivymd/animation.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ def run_animation(self, dt):
9595
"""
9696

9797
import math
98+
import sys
9899
import kivy.animation
99100

100101
float_epsilon = 8.3446500e-7
101102

103+
if sys.version_info < (3, 11):
104+
cbrt = lambda number: (abs(number) ** (1/3)) * (-1 if number < 0 else 1)
105+
else:
106+
cbrt = math.cbrt
102107

103108
class CubicBezier:
104109
"""Ported from Android source code"""
@@ -165,7 +170,7 @@ def find_first_cubic_root(self, p0, p1, p2, p3):
165170
t = -q2 / r
166171
cos_phi = max(-1.0, min(t, 1.0))
167172
phi = math.acos(cos_phi)
168-
t1 = 2.0 * math.cbrt(r)
173+
t1 = 2.0 * cbrt(r)
169174
root = self.clamp_range(t1 * math.cos(phi / 3.0) - a3)
170175
if not math.isnan(root):
171176
return root
@@ -179,15 +184,15 @@ def find_first_cubic_root(self, p0, p1, p2, p3):
179184
)
180185

181186
elif self.close_to(discriminant, 0.0):
182-
u1 = -math.cbrt(q2)
187+
u1 = -cbrt(q2)
183188
root = self.clamp_range(2.0 * u1 - a3)
184189
if not math.isnan(root):
185190
return root
186191
return self.clamp_range(-u1 - a3)
187192

188193
sd = math.sqrt(discriminant)
189-
u1 = math.cbrt(-q2 + sd)
190-
v1 = math.cbrt(q2 + sd)
194+
u1 = cbrt(-q2 + sd)
195+
v1 = cbrt(q2 + sd)
191196
return self.clamp_range(u1 - v1 - a3)
192197

193198
def t(self, value: float):

0 commit comments

Comments
 (0)