def advance_wrap(self, steps):
assert self.is_valid()
if steps == 0:
return self
step_shift = 2 * (self.__class__.MAX_LEVEL - self.level()) + 1
if steps < 0:
min_steps = -(self.id() >> step_shift)
if steps < min_steps:
step_wrap = self.__class__.WRAP_OFFSET >> step_shift
# cannot use steps %= step_wrap as Python % different to C++
steps = int(math.fmod(steps, step_wrap))
if steps < min_steps:
steps += step_wrap
else:
max_steps = (self.__class__.WRAP_OFFSET - self.id()) >> step_shift
if steps > max_steps:
step_wrap = self.__class__.WRAP_OFFSET >> step_shift
# cannot use steps %= step_wrap as Python % different to C++
steps = int(math.fmod(steps, step_wrap))
if steps > max_steps:
steps -= step_wrap
return self.__class__(self.id() + (steps << step_shift))
python类fmod()的实例源码
def point_inside_loop(loop_coords, mous_loc):
nverts = len(loop_coords)
# vectorize our two item tuple
out = Vector(outside_loop(loop_coords))
vec_mous = Vector(mous_loc)
intersections = 0
for i in range(0, nverts):
a = Vector(loop_coords[i-1])
b = Vector(loop_coords[i])
if intersect_line_line_2d(vec_mous, out, a, b):
intersections += 1
inside = False
if fmod(intersections, 2):
inside = True
return inside
# === OpenGL drawing functions ===
spinner_advanced.py 文件源码
项目:Adafruit_CircuitPython_LIS3DH
作者: adafruit
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def get_position(self, delta):
"""Update the spinner position after the specified delta (in seconds)
has elapsed. Will return the new spinner position, a continuous value
from 0...<10.
"""
# Increment elapsed time and compute the current velocity after a
# decay of the initial velocity.
self._elapsed += delta
current_velocity = self._velocity*math.pow(self._decay, self._elapsed)
# Update position based on the current_velocity and elapsed time.
self._position += current_velocity*delta
# Make sure the position stays within values that range from 0 to <10.
self._position = math.fmod(self._position, 10.0)
if self._position < 0.0:
self._position += 10.0
return self._position
# Define animation classes. Each animation needs to have an update function
# which takes in the current spinner position and a selected primary and
# secondary color (3-tuple of RGB bytes) and will render a frame of spinner
# animation.
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def wrap360(euler_angle):
""" Wrap angle to 360
Args:
euler_angle (float): Euler angle
Returns:
Wrapped angle
"""
if euler_angle > 0.0:
return fmod(euler_angle, 360.0)
else:
euler_angle += 360.0
return fmod(euler_angle, 360.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def next_overpass (date1, path, sat):
""" provides the next overpass for path after date1
"""
date0_L5 = datetime.datetime(1985,5,4)
date0_L7 = datetime.datetime(1999,1,11)
date0_L8 = datetime.datetime(2013,5,1)
if sat == 'LT5':
date0 = date0_L5
elif sat == 'LE7':
date0 = date0_L7
elif sat == 'LC8':
date0 = date0_L8
next_day=math.fmod((date1-date0).days-cycle_day(path)+1,16)
if next_day != 0:
date_overpass = date1+datetime.timedelta(16-next_day)
else:
date_overpass = date1
return date_overpass
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testFmod(self):
self.assertRaises(TypeError, math.fmod)
self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
self.assertTrue(math.isnan(math.fmod(1., NAN)))
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
self.assertRaises(ValueError, math.fmod, 1., 0.)
self.assertRaises(ValueError, math.fmod, INF, 1.)
self.assertRaises(ValueError, math.fmod, NINF, 1.)
self.assertRaises(ValueError, math.fmod, INF, 0.)
self.assertEqual(math.fmod(3.0, INF), 3.0)
self.assertEqual(math.fmod(-3.0, INF), -3.0)
self.assertEqual(math.fmod(3.0, NINF), 3.0)
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
self.assertEqual(math.fmod(0.0, NINF), 0.0)
def drem(x, y):
"""Like fmod but rounds to nearest integer instead of floor."""
xd = decimal.Decimal(x)
yd = decimal.Decimal(y)
return float(xd.remainder_near(yd))
def AngNormalize(x):
"""reduce angle to (-180,180]"""
y = math.fmod(x, 360)
# On Windows 32-bit with python 2.7, math.fmod(-0.0, 360) = +0.0
# This fixes this bug. See also Math::AngNormalize in the C++ library.
# sincosd has a similar fix.
y = x if x == 0 else y
return (y + 360 if y <= -180 else
(y if y <= 180 else y - 360))
def sincosd(x):
"""Compute sine and cosine of x in degrees."""
r = math.fmod(x, 360)
q = Math.nan if Math.isnan(r) else int(math.floor(r / 90 + 0.5))
r -= 90 * q; r = math.radians(r)
s = math.sin(r); c = math.cos(r)
q = q % 4
if q == 1:
s, c = c, -s
elif q == 2:
s, c = -s, -c
elif q == 3:
s, c = -c, s
# Remove the minus sign on -0.0 except for sin(-0.0).
# On Windows 32-bit with python 2.7, math.fmod(-0.0, 360) = +0.0
# (x, c) here fixes this bug. See also Math::sincosd in the C++ library.
# AngNormalize has a similar fix.
s, c = (x, c) if x == 0 else (0.0+s, 0.0+c)
return s, c
polygonarea.py 文件源码
项目:qgis-shapetools-plugin
作者: NationalSecurityAgency
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def _transitdirect(lon1, lon2):
"""Count crossings of prime meridian for AddEdge."""
# We want to compute exactly
# int(floor(lon2 / 360)) - int(floor(lon1 / 360))
# Since we only need the parity of the result we can use std::remquo but
# this is buggy with g++ 4.8.3 and requires C++11. So instead we do
lon1 = math.fmod(lon1, 720.0); lon2 = math.fmod(lon2, 720.0)
return ( (0 if ((lon2 >= 0 and lon2 < 360) or lon2 < -360) else 1) -
(0 if ((lon1 >= 0 and lon1 < 360) or lon1 < -360) else 1) )
def normalizeLongitude(num):
'''Normalize the Longitude between -180 and 180 degrees'''
num += 180.0
num = math.fmod(num, 360.0)
if num < 0:
num += 180
else:
num -= 180
return num
def test_frac(self):
self._testMath(torch.frac, lambda x: math.fmod(x, 1))
def test_trunc(self):
self._testMath(torch.trunc, lambda x: x - math.fmod(x, 1))
def test_fmod(self):
m1 = torch.Tensor(10,10).uniform_(-10., 10.)
res1 = m1.clone()
q = 2.1
res1[:,3].fmod_(q)
res2 = m1.clone()
for i in range(m1.size(1)):
res2[i,3] = math.fmod(res2[i,3], q)
self.assertEqual(res1, res2)
def test_cfmod(self):
self._test_cop(torch.fmod, math.fmod)
def _func(self, x):
return 75 if (math.fmod(x, 50) <= 25) else 25
def __dxftags__(self):
def curve_point(alpha):
alpha = radians(alpha)
point = (cos(alpha) * self.rx,
sin(alpha) * self.ry)
point = rotate_2d(point, radians(self.rotation))
x, y = vadd(self.center, point)
return (x, y, zaxis)
def normalize_angle(angle):
angle = fmod(angle, 360.)
if angle < 0:
angle += 360.
return angle
zaxis = 0. if len(self.center)<3 else self.center[2]
points = []
delta = (self.endangle - self.startangle) / self.segments
for segment in xrange(self.segments):
alpha = self.startangle + delta * segment
points.append(curve_point(alpha))
polyline = Polyline(points, color=self.color, layer=self.layer,
linetype=self.linetype)
if equals_almost(self.startangle, normalize_angle(self.endangle)):
polyline.close()
return polyline.__dxftags__()
def normalize_angle(angle):
""" return an angle between 0 and 2*pi """
angle = math.fmod(angle, DOUBLE_PI)
if angle < 0:
angle += DOUBLE_PI
return angle
def next_time(t):
next_t = list(t)
if math.fmod(next_t[3], 2) == 0:
next_t[3] += 2
else:
next_t[3] += 1
next_t = next_t[:4] + [0 for i in range(5)]
return next_t
def get_position(self, delta):
# Increment elapsed time and compute the current velocity after a
# decay of the initial velocity.
self._elapsed += delta
current_velocity = self._velocity*math.pow(self._decay, self._elapsed)
self._position += current_velocity*delta
# Make sure the position stays within values that range from 0 to <10.
self._position = math.fmod(self._position, 10.0)
if self._position < 0.0:
self._position += 10.0
return self._position
# Initialize NeoPixels and accelerometer.
def normalizeLongitude(num):
'''Normalize the Longitude between -180 and 180 degrees'''
num += 180.0
num = math.fmod(num, 360.0)
if num < 0:
num += 180
else:
num -= 180
return num
def _mgrsString(zone, letters, easting, northing, precision):
""" Constructs an MGRS string from its component parts
@param zone - UTM zone
@param letters - MGRS coordinate string letters
@param easting - easting value
@param northing - northing value
@param precision - precision level of MGRS string
@returns - MGRS coordinate string
"""
mrgs = ''
if zone:
tmp = str(zone)
mgrs = tmp.zfill(3 - len(tmp))
else:
mgrs = ' '
for i in range(3):
mgrs += list(ALPHABET.keys())[list(ALPHABET.values()).index(letters[i])]
easting = math.fmod(easting + 1e-8, 100000.0)
if easting >= 99999.5:
easting = 99999.0
mgrs += str(int(easting)).rjust(5, '0')[:precision]
northing = math.fmod(northing + 1e-8, 100000.0)
if northing >= 99999.5:
northing = 99999.0
mgrs += str(int(northing)).rjust(5, '0')[:precision]
return mgrs
def test_frac(self):
self._testMath(torch.frac, lambda x: math.fmod(x, 1))