def compare_as_floats(xs, ys, error):
def f(x):
try:
y = float(x)
if not math.isfinite(y):
log.warning('not an real number found: %f', y)
return y
except ValueError:
return x
xs = list(map(f, xs.split()))
ys = list(map(f, ys.split()))
if len(xs) != len(ys):
return False
for x, y in zip(xs, ys):
if isinstance(x, float) and isinstance(y, float):
if not math.isclose(x, y, rel_tol=error, abs_tol=error):
return False
else:
if x != y:
return False
return True
python类isfinite()的实例源码
def rect(r, phi):
_rect_special = [
[inf+nanj, None, -inf, complex(-float("inf"), -0.0), None, inf+nanj, inf+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[0, None, complex(-0.0, 0.0), complex(-0.0, -0.0), None, 0, 0],
[0, None, complex(0.0, -0.0), 0, None, 0, 0],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[inf+nanj, None, complex(float("inf"), -0.0), inf, None, inf+nanj, inf+nanj],
[nan+nanj, nan+nanj, nan, nan, nan+nanj, nan+nanj, nan+nanj]
]
if not math.isfinite(r) or not math.isfinite(phi):
if math.isinf(phi) and not math.isnan(r) and r != 0:
raise ValueError
if math.isinf(r) and math.isfinite(phi) and phi != 0:
if r > 0:
return complex(math.copysign(inf, math.cos(phi)),
math.copysign(inf, math.sin(phi)))
return complex(-math.copysign(inf, math.cos(phi)),
-math.copysign(inf, math.sin(phi)))
return _rect_special[_special_type(r)][_special_type(phi)]
return complex(r*math.cos(phi), r*math.sin(phi))
def initial_wait_time(self):
"""Return the initial wait time if config setting exists and is valid,
otherwise return the default.
The initial wait time is used during the bootstrapping of the block-
chain to compute the local mean for wait timers until there are at
least population_estimate_sample_size PoET blocks in the blockchain.
"""
if self._initial_wait_time is None:
self._initial_wait_time = \
self._get_config_setting(
name='sawtooth.poet.initial_wait_time',
value_type=float,
default_value=PoetSettingsView._INITIAL_WAIT_TIME_,
validate_function=lambda value:
math.isfinite(value) and value >= 0)
return self._initial_wait_time
def minimum_wait_time(self):
"""Return the minimum wait time if config setting exists and is valid,
otherwise return the default.
The minimum wait time is used as a lower bound for the minimum amount
of time a validator must want before attempting to claim a block.
"""
if self._minimum_wait_time is None:
self._minimum_wait_time = \
self._get_config_setting(
name='sawtooth.poet.minimum_wait_time',
value_type=float,
default_value=PoetSettingsView._MINIMUM_WAIT_TIME_,
validate_function=lambda value:
math.isfinite(value) and value > 0)
return self._minimum_wait_time
def target_wait_time(self):
"""Return the target wait time if config setting exists and is valid,
otherwise return the default.
The target wait time is the desired average amount of time, across all
validators in the network, a validator must wait before attempting to
claim a block.
"""
if self._target_wait_time is None:
self._target_wait_time = \
self._get_config_setting(
name='sawtooth.poet.target_wait_time',
value_type=float,
default_value=PoetSettingsView._TARGET_WAIT_TIME_,
validate_function=lambda value:
math.isfinite(value) and value > 0)
return self._target_wait_time
def test_isfinite(self):
real_vals = [float('-inf'), -2.3, -0.0,
0.0, 2.3, float('inf'), float('nan')]
for x in real_vals:
for y in real_vals:
z = complex(x, y)
self.assertEqual(cmath.isfinite(z),
math.isfinite(x) and math.isfinite(y))
def testIsfinite(self):
self.assertTrue(math.isfinite(0.0))
self.assertTrue(math.isfinite(-0.0))
self.assertTrue(math.isfinite(1.0))
self.assertTrue(math.isfinite(-1.0))
self.assertFalse(math.isfinite(float("nan")))
self.assertFalse(math.isfinite(float("inf")))
self.assertFalse(math.isfinite(float("-inf")))
def test_isfinite(self):
real_vals = [float('-inf'), -2.3, -0.0,
0.0, 2.3, float('inf'), float('nan')]
for x in real_vals:
for y in real_vals:
z = complex(x, y)
self.assertEqual(cmath.isfinite(z),
math.isfinite(x) and math.isfinite(y))
def testIsfinite(self):
self.assertTrue(math.isfinite(0.0))
self.assertTrue(math.isfinite(-0.0))
self.assertTrue(math.isfinite(1.0))
self.assertTrue(math.isfinite(-1.0))
self.assertFalse(math.isfinite(float("nan")))
self.assertFalse(math.isfinite(float("inf")))
self.assertFalse(math.isfinite(float("-inf")))
def drive_to_wall(self, initial_call):
if initial_call:
self.profilefollower.stop()
if self.target == Targets.Centre:
peg_range = self.centre_airship_distance
else:
peg_range = 1.5
peg_range -= self.centre_to_front_bumper
peg_range += 0.3
r = self.range_filter.range
print("AUTO DRIVE WALL RANGE: %s" % (self.range_finder.getDistance()))
print("AUTO DRIVE WALL FILTER RANGE: %s" % (self.range_filter.range))
# 40 is range finder max distance, better failure mode than inf or really small
if not math.isfinite(r):
r = 40
elif r < 0.5:
r = 40
to_peg = None
if r > (2 if self.target != Targets.Centre else 4):
print("DEAD RECKON AUTO")
to_peg = generate_trapezoidal_trajectory(
0, 0, peg_range + 0.1, 0, self.displace_velocity,
self.displace_accel, -self.displace_decel,
Chassis.motion_profile_freq)
else:
print("RANGE AUTO")
to_peg = generate_trapezoidal_trajectory(0, 0,
self.range_finder.getDistance() - self.lidar_to_front_bumper + 0.1,
0, self.displace_velocity, self.displace_accel, -self.displace_decel,
Chassis.motion_profile_freq)
self.profilefollower.modify_queue(self.bno055.getHeading(),
linear=to_peg, overwrite=True)
self.profilefollower.execute_queue()
self.manipulategear.engage()
if not self.profilefollower.executing:
self.next_state("deploying_gear")
def _special_type(x):
ST_NINF, ST_NEG, ST_NZERO, ST_PZERO, ST_POS, ST_PINF, ST_NAN = range(7)
if math.isnan(x):
return ST_NAN
if math.isfinite(x):
if x != 0:
if math.copysign(1, x) == 1:
return ST_POS
return ST_NEG
if math.copysign(1, x) == 1:
return ST_PZERO
return ST_NZERO
if math.copysign(1, x) == 1:
return ST_PINF
return ST_NINF
def exp(x):
z = _make_complex(x)
exp_special = [
[0+0j, None, complex(0, -0.0), 0+0j, None, 0+0j, 0+0j],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[nan+nanj, None, 1-0j, 1+0j, None, nan+nanj, nan+nanj],
[nan+nanj, None, 1-0j, 1+0j, None, nan+nanj, nan+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[inf+nanj, None, complex(float("inf"), -0.0), inf, None, inf+nanj, inf+nanj],
[nan+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, nan+nanj, nan+nanj]
]
if not isfinite(z):
if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
if z.real > 0:
ret = complex(math.copysign(inf, math.cos(z.imag)),
math.copysign(inf, math.sin(z.imag)))
else:
ret = complex(math.copysign(0, math.cos(z.imag)),
math.copysign(0, math.sin(z.imag)))
else:
ret = exp_special[_special_type(z.real)][_special_type(z.imag)]
if math.isinf(z.imag) and (math.isfinite(z.real) or
(math.isinf(z.real) and z.real > 0)):
raise ValueError
return ret
if z.real > _LOG_LARGE_DOUBLE:
ret = e * rect(math.exp(z.real - 1), z.imag)
else:
ret = rect(math.exp(z.real), z.imag)
if math.isinf(ret.real) or math.isinf(ret.imag):
raise OverflowError
return ret
def acos(x):
_acos_special = [
[3*pi/4+infj, pi+infj, pi+infj, pi-infj, pi-infj, 3*pi/4-infj, nan+infj],
[pi/2+infj, None, None, None, None, pi/2-infj, nan+nanj],
[pi/2+infj, None, None, None, None, pi/2-infj, pi/2+nanj],
[pi/2+infj, None, None, None, None, pi/2-infj, pi/2+nanj],
[pi/2+infj, None, None, None, None, pi/2-infj, nan+nanj],
[pi/4+infj, infj, infj, 0.0-infj, 0.0-infj, pi/4-infj, nan+infj],
[nan+infj, nan+nanj, nan+nanj, nan+nanj, nan+nanj, nan-infj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
return _acos_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LARGE_DOUBLE or abs(z.imag) > _LARGE_DOUBLE:
if z.real < 0:
imag = -math.copysign(math.log(math.hypot(z.real/2, z.imag/2)) +
2 * _LOG_2, z.imag)
else:
imag = math.copysign(math.log(math.hypot(z.real/2, z.imag/2)) +
2 * _LOG_2, -z.imag)
return complex(math.atan2(abs(z.imag), z.real), imag)
s1 = sqrt(complex(1.0 - z.real, -z.imag))
s2 = sqrt(complex(1.0 + z.real, z.imag))
return complex(2 * math.atan2(s1.real, s2.real),
math.asinh(s2.real*s1.imag - s2.imag*s1.real))
def asinh(x):
_asinh_special = [
[-inf-1j*pi/4, complex(-float("inf"), -0.0), complex(-float("inf"), -0.0),
complex(-float("inf"), 0.0), complex(-float("inf"), 0.0), -inf+1j*pi/4, -inf+nanj],
[-inf-1j*pi/2, None, None, None, None, -inf+1j*pi/2, nan+nanj],
[-inf-1j*pi/2, None, None, None, None, -inf+1j*pi/2, nan+nanj],
[inf-1j*pi/2, None, None, None, None, inf+1j*pi/2, nan+nanj],
[inf-1j*pi/2, None, None, None, None, inf+1j*pi/2, nan+nanj],
[inf-1j*pi/4, complex(float("inf"), -0.0), complex(float("inf"), -0.0),
inf, inf, inf+1j*pi/4, inf+nanj],
[inf+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, inf+nanj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
return _asinh_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LARGE_DOUBLE or abs(z.imag) > _LARGE_DOUBLE:
if z.imag >= 0:
real = math.copysign(math.log(math.hypot(z.imag/2, z.real/2)) +
2 * _LOG_2, z.real)
else:
real = -math.copysign(math.log(math.hypot(z.imag/2, z.real/2)) +
2 * _LOG_2, -z.real)
return complex(real, math.atan2(z.imag, abs(z.real)))
s1 = sqrt(complex(1+z.imag, -z.real))
s2 = sqrt(complex(1-z.imag, z.real))
return complex(math.asinh(s1.real*s2.imag-s2.real*s1.imag),
math.atan2(z.imag, s1.real*s2.real - s1.imag*s2.imag))
def cosh(x):
_cosh_special = [
[inf+nanj, None, inf, complex(float("inf"), -0.0), None, inf+nanj, inf+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[nan, None, 1, complex(1, -0.0), None, nan, nan],
[nan, None, complex(1, -0.0), 1, None, nan, nan],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[inf+nanj, None, complex(float("inf"), -0.0), inf, None, inf+nanj, inf+nanj],
[nan+nanj, nan+nanj, nan, nan, nan+nanj, nan+nanj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
if math.isinf(z.imag) and not math.isnan(z.real):
raise ValueError
if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
if z.real > 0:
return complex(math.copysign(inf, math.cos(z.imag)),
math.copysign(inf, math.sin(z.imag)))
return complex(math.copysign(inf, math.cos(z.imag)),
-math.copysign(inf, math.sin(z.imag)))
return _cosh_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LOG_LARGE_DOUBLE:
x_minus_one = z.real - math.copysign(1, z.real)
ret = complex(e * math.cos(z.imag) * math.cosh(x_minus_one),
e * math.sin(z.imag) * math.sinh(x_minus_one))
else:
ret = complex(math.cos(z.imag) * math.cosh(z.real),
math.sin(z.imag) * math.sinh(z.real))
if math.isinf(ret.real) or math.isinf(ret.imag):
raise OverflowError
return ret
def sinh(x):
_sinh_special = [
[inf+nanj, None, complex(-float("inf"), -0.0), -inf, None, inf+nanj, inf+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[nanj, None, complex(-0.0, -0.0), complex(-0.0, 0.0), None, nanj, nanj],
[nanj, None, complex(0.0, -0.0), complex(0.0, 0.0), None, nanj, nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[inf+nanj, None, complex(float("inf"), -0.0), inf, None, inf+nanj, inf+nanj],
[nan+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, nan+nanj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
if math.isinf(z.imag) and not math.isnan(z.real):
raise ValueError
if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
if z.real > 0:
return complex(math.copysign(inf, math.cos(z.imag)),
math.copysign(inf, math.sin(z.imag)))
return complex(-math.copysign(inf, math.cos(z.imag)),
math.copysign(inf, math.sin(z.imag)))
return _sinh_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LOG_LARGE_DOUBLE:
x_minus_one = z.real - math.copysign(1, z.real)
return complex(math.cos(z.imag) * math.sinh(x_minus_one) * e,
math.sin(z.imag) * math.cosh(x_minus_one) * e)
return complex(math.cos(z.imag) * math.sinh(z.real),
math.sin(z.imag) * math.cosh(z.real))
def tanh(x):
_tanh_special = [
[-1, None, complex(-1, -0.0), -1, None, -1, -1],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[nan+nanj, None, complex(-0.0, -0.0), complex(-0.0, 0.0), None, nan+nanj, nan+nanj],
[nan+nanj, None, complex(0.0, -0.0), 0.0, None, nan+nanj, nan+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[1, None, complex(1, -0.0), 1, None, 1, 1],
[nan+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, nan+nanj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
if math.isinf(z.imag) and math.isfinite(z.real):
raise ValueError
if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
if z.real > 0:
return complex(1, math.copysign(0.0, math.sin(z.imag)
* math.cos(z.imag)))
return complex(-1, math.copysign(0.0, math.sin(z.imag)
* math.cos(z.imag)))
return _tanh_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LOG_LARGE_DOUBLE:
return complex(
math.copysign(1, z.real),
4*math.sin(z.imag)*math.cos(z.imag)*math.exp(-2*abs(z.real))
)
tanh_x = math.tanh(z.real)
tan_y = math.tan(z.imag)
cx = 1/math.cosh(z.real)
denom = 1 + tanh_x * tanh_x * tan_y * tan_y
return complex(tanh_x * (1 + tan_y*tan_y)/denom,
((tan_y / denom) * cx) * cx)
def isfinite(x):
return math.isfinite(x.real) and math.isfinite(x.imag)
def _isfinite(x):
try:
return x.is_finite() # Likely a Decimal.
except AttributeError:
return math.isfinite(x) # Coerces to float first.
def test_isfinite(self):
real_vals = [float('-inf'), -2.3, -0.0,
0.0, 2.3, float('inf'), float('nan')]
for x in real_vals:
for y in real_vals:
z = complex(x, y)
self.assertEqual(cmath.isfinite(z),
math.isfinite(x) and math.isfinite(y))
def testIsfinite(self):
self.assertTrue(math.isfinite(0.0))
self.assertTrue(math.isfinite(-0.0))
self.assertTrue(math.isfinite(1.0))
self.assertTrue(math.isfinite(-1.0))
self.assertFalse(math.isfinite(float("nan")))
self.assertFalse(math.isfinite(float("inf")))
self.assertFalse(math.isfinite(float("-inf")))
def test_isfinite(self):
real_vals = [float('-inf'), -2.3, -0.0,
0.0, 2.3, float('inf'), float('nan')]
for x in real_vals:
for y in real_vals:
z = complex(x, y)
self.assertEqual(cmath.isfinite(z),
math.isfinite(x) and math.isfinite(y))
def _exact_ratio(x):
"""Convert Real number x exactly to (numerator, denominator) pair.
>>> _exact_ratio(0.25)
(1, 4)
x is expected to be an int, Fraction, Decimal or float.
"""
try:
try:
# int, Fraction
return (x.numerator, x.denominator)
except AttributeError:
# float
try:
return x.as_integer_ratio()
except AttributeError:
# Decimal
try:
return _decimal_to_ratio(x)
except AttributeError:
msg = "can't convert type '{}' to numerator/denominator"
raise TypeError(msg.format(type(x).__name__)) from None
except (OverflowError, ValueError):
# INF or NAN
if __debug__:
# Decimal signalling NANs cannot be converted to float :-(
if isinstance(x, Decimal):
assert not x.is_finite()
else:
assert not math.isfinite(x)
return (x, None)
# FIXME This is faster than Fraction.from_decimal, but still too slow.
def test_isfinite(self):
real_vals = [float('-inf'), -2.3, -0.0,
0.0, 2.3, float('inf'), float('nan')]
for x in real_vals:
for y in real_vals:
z = complex(x, y)
self.assertEqual(cmath.isfinite(z),
math.isfinite(x) and math.isfinite(y))
def testIsfinite(self):
self.assertTrue(math.isfinite(0.0))
self.assertTrue(math.isfinite(-0.0))
self.assertTrue(math.isfinite(1.0))
self.assertTrue(math.isfinite(-1.0))
self.assertFalse(math.isfinite(float("nan")))
self.assertFalse(math.isfinite(float("inf")))
self.assertFalse(math.isfinite(float("-inf")))
def isfinite(x):
if math.isinf(x) or math.isnan(x):
return False
return True
def test_isfinite():
a = SparseArray.fromlist(random_lst(p=0.5))
b = SparseArray.fromlist(random_lst(p=0.5))
c = a / b
assert a.isfinite()
assert b.isfinite()
assert not c.isfinite()
def test_finite():
a = SparseArray.fromlist(random_lst(p=0.5))
b = SparseArray.fromlist(random_lst(p=0.5))
c = a / b
res = [i for i in c.data if isfinite(i)]
d = c.finite()
[assert_almost_equals(v, w) for v, w in zip([x for x in res if x != 0],
d.data)]
def test_finite_inplace():
a = SparseArray.fromlist(random_lst())
b = SparseArray.fromlist(random_lst())
c = a / b
d = c.finite()
c.finite(inplace=True)
assert c.isfinite()
assert c.SSE(d) == 0
assert len(c.index) == len(d.index)
def ztest_maximum_win_deviation(self):
"""Return the zTest maximum win deviation if config setting exists and
is valid, otherwise return the default.
The zTest maximum win deviation specifies the maximum allowed
deviation from the expected win frequency for a particular validator
before the zTest will fail and the claimed block will be rejected.
The deviation corresponds to a confidence interval (i.e., how
confident we are that we have truly detected a validator winning at
a frequency we consider too frequent):
3.075 ==> 99.9%
2.575 ==> 99.5%
2.321 ==> 99%
1.645 ==> 95%
"""
if self._ztest_maximum_win_deviation is None:
self._ztest_maximum_win_deviation = \
self._get_config_setting(
name='sawtooth.poet.ztest_maximum_win_deviation',
value_type=float,
default_value=PoetSettingsView.
_ZTEST_MAXIMUM_WIN_DEVIATION_,
validate_function=lambda value:
math.isfinite(value) and value > 0)
return self._ztest_maximum_win_deviation