本文整理汇总了Python中pypy.rlib.rfloat.copysign函数的典型用法代码示例。如果您正苦于以下问题:Python copysign函数的具体用法?Python copysign怎么用?Python copysign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copysign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: c_atanh
def c_atanh(x, y):
if not isfinite(x) or not isfinite(y):
return atanh_special_values[special_type(x)][special_type(y)]
# Reduce to case where x >= 0., using atanh(z) = -atanh(-z).
if x < 0.:
return c_neg(*c_atanh(*c_neg(x, y)))
ay = fabs(y)
if x > CM_SQRT_LARGE_DOUBLE or ay > CM_SQRT_LARGE_DOUBLE:
# if abs(z) is large then we use the approximation
# atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
# of y
h = math.hypot(x/2., y/2.) # safe from overflow
real = x/4./h/h
# the two negations in the next line cancel each other out
# except when working with unsigned zeros: they're there to
# ensure that the branch cut has the correct continuity on
# systems that don't support signed zeros
imag = -copysign(math.pi/2., -y)
elif x == 1. and ay < CM_SQRT_DBL_MIN:
# C99 standard says: atanh(1+/-0.) should be inf +/- 0i
if ay == 0.:
raise ValueError("math domain error")
#real = INF
#imag = y
else:
real = -math.log(math.sqrt(ay)/math.sqrt(math.hypot(ay, 2.)))
imag = copysign(math.atan2(2., -ay) / 2, y)
else:
real = log1p(4.*x/((1-x)*(1-x) + ay*ay))/4.
imag = -math.atan2(-2.*y, (1-x)*(1+x) - ay*ay) / 2.
return (real, imag)
示例2: c_exp
def c_exp(x, y):
if not isfinite(x) or not isfinite(y):
if isinf(x) and isfinite(y) and y != 0.:
if x > 0:
real = copysign(INF, math.cos(y))
imag = copysign(INF, math.sin(y))
else:
real = copysign(0., math.cos(y))
imag = copysign(0., math.sin(y))
r = (real, imag)
else:
r = exp_special_values[special_type(x)][special_type(y)]
# need to raise ValueError if y is +/- infinity and x is not
# a NaN and not -infinity
if isinf(y) and (isfinite(x) or (isinf(x) and x > 0)):
raise ValueError("math domain error")
return r
if x > CM_LOG_LARGE_DOUBLE:
l = math.exp(x-1.)
real = l * math.cos(y) * math.e
imag = l * math.sin(y) * math.e
else:
l = math.exp(x)
real = l * math.cos(y)
imag = l * math.sin(y)
if isinf(real) or isinf(imag):
raise OverflowError("math range error")
return real, imag
示例3: c_rect
def c_rect(r, phi):
if not isfinite(r) or not isfinite(phi):
# if r is +/-infinity and phi is finite but nonzero then
# result is (+-INF +-INF i), but we need to compute cos(phi)
# and sin(phi) to figure out the signs.
if isinf(r) and isfinite(phi) and phi != 0.:
if r > 0:
real = copysign(INF, math.cos(phi))
imag = copysign(INF, math.sin(phi))
else:
real = -copysign(INF, math.cos(phi))
imag = -copysign(INF, math.sin(phi))
z = (real, imag)
else:
z = rect_special_values[special_type(r)][special_type(phi)]
# need to raise ValueError if r is a nonzero number and phi
# is infinite
if r != 0. and not isnan(r) and isinf(phi):
raise ValueError("math domain error")
return z
real = r * math.cos(phi)
imag = r * math.sin(phi)
return real, imag
示例4: c_sinh
def c_sinh(x, y):
# special treatment for sinh(+/-inf + iy) if y is finite and nonzero
if not isfinite(x) or not isfinite(y):
if isinf(x) and isfinite(y) and y != 0.:
if x > 0:
real = copysign(INF, math.cos(y))
imag = copysign(INF, math.sin(y))
else:
real = -copysign(INF, math.cos(y))
imag = copysign(INF, math.sin(y))
r = (real, imag)
else:
r = sinh_special_values[special_type(x)][special_type(y)]
# need to raise ValueError if y is +/- infinity and x is not
# a NaN
if isinf(y) and not isnan(x):
raise ValueError("math domain error")
return r
if fabs(x) > CM_LOG_LARGE_DOUBLE:
x_minus_one = x - copysign(1., x)
real = math.cos(y) * math.sinh(x_minus_one) * math.e
imag = math.sin(y) * math.cosh(x_minus_one) * math.e
else:
real = math.cos(y) * math.sinh(x)
imag = math.sin(y) * math.cosh(x)
if isinf(real) or isinf(imag):
raise OverflowError("math range error")
return real, imag
示例5: test_nan_and_special_values
def test_nan_and_special_values():
from pypy.rlib.rfloat import isnan, isinf, isfinite, copysign
inf = 1e300 * 1e300
assert isinf(inf)
nan = inf/inf
assert isnan(nan)
for value, checker in [
(inf, lambda x: isinf(x) and x > 0.0),
(-inf, lambda x: isinf(x) and x < 0.0),
(nan, isnan),
(42.0, isfinite),
(0.0, lambda x: not x and copysign(1., x) == 1.),
(-0.0, lambda x: not x and copysign(1., x) == -1.),
]:
def f():
return value
f1 = compile(f, [])
res = f1()
assert checker(res)
l = [value]
def g(x):
return l[x]
g2 = compile(g, [int])
res = g2(0)
assert checker(res)
l2 = [(-value, -value), (value, value)]
def h(x):
return l2[x][1]
h3 = compile(h, [int])
res = h3(1)
assert checker(res)
示例6: str__Complex
def str__Complex(space, w_complex):
if w_complex.realval == 0 and copysign(1., w_complex.realval) == 1.:
return space.wrap(str_format(w_complex.imagval) + 'j')
sign = (copysign(1., w_complex.imagval) == 1. or
isnan(w_complex.imagval)) and '+' or ''
return space.wrap('(' + str_format(w_complex.realval)
+ sign + str_format(w_complex.imagval) + 'j)')
示例7: c_cosh
def c_cosh(x, y):
if not isfinite(x) or not isfinite(y):
if isinf(x) and isfinite(y) and y != 0.:
if x > 0:
real = copysign(INF, math.cos(y))
imag = copysign(INF, math.sin(y))
else:
real = copysign(INF, math.cos(y))
imag = -copysign(INF, math.sin(y))
r = (real, imag)
else:
r = cosh_special_values[special_type(x)][special_type(y)]
# need to raise ValueError if y is +/- infinity and x is not
# a NaN
if isinf(y) and not isnan(x):
raise ValueError("math domain error")
return r
if fabs(x) > CM_LOG_LARGE_DOUBLE:
# deal correctly with cases where cosh(x) overflows but
# cosh(z) does not.
x_minus_one = x - copysign(1., x)
real = math.cos(y) * math.cosh(x_minus_one) * math.e
imag = math.sin(y) * math.sinh(x_minus_one) * math.e
else:
real = math.cos(y) * math.cosh(x)
imag = math.sin(y) * math.sinh(x)
if isinf(real) or isinf(imag):
raise OverflowError("math range error")
return real, imag
示例8: __eq__
def __eq__(self, other):
if (type(self) is SomeFloat and type(other) is SomeFloat and
self.is_constant() and other.is_constant()):
from pypy.rlib.rfloat import isnan, copysign
# NaN unpleasantness.
if isnan(self.const) and isnan(other.const):
return True
# 0.0 vs -0.0 unpleasantness.
if not self.const and not other.const:
return copysign(1., self.const) == copysign(1., other.const)
#
return super(SomeFloat, self).__eq__(other)
示例9: c_sqrt
def c_sqrt(x, y):
# Method: use symmetries to reduce to the case when x = z.real and y
# = z.imag are nonnegative. Then the real part of the result is
# given by
#
# s = sqrt((x + hypot(x, y))/2)
#
# and the imaginary part is
#
# d = (y/2)/s
#
# If either x or y is very large then there's a risk of overflow in
# computation of the expression x + hypot(x, y). We can avoid this
# by rewriting the formula for s as:
#
# s = 2*sqrt(x/8 + hypot(x/8, y/8))
#
# This costs us two extra multiplications/divisions, but avoids the
# overhead of checking for x and y large.
#
# If both x and y are subnormal then hypot(x, y) may also be
# subnormal, so will lack full precision. We solve this by rescaling
# x and y by a sufficiently large power of 2 to ensure that x and y
# are normal.
if not isfinite(x) or not isfinite(y):
return sqrt_special_values[special_type(x)][special_type(y)]
if x == 0. and y == 0.:
return (0., y)
ax = fabs(x)
ay = fabs(y)
if ax < DBL_MIN and ay < DBL_MIN and (ax > 0. or ay > 0.):
# here we catch cases where hypot(ax, ay) is subnormal
ax = math.ldexp(ax, CM_SCALE_UP)
ay1= math.ldexp(ay, CM_SCALE_UP)
s = math.ldexp(math.sqrt(ax + math.hypot(ax, ay1)),
CM_SCALE_DOWN)
else:
ax /= 8.
s = 2.*math.sqrt(ax + math.hypot(ax, ay/8.))
d = ay/(2.*s)
if x >= 0.:
return (s, copysign(d, y))
else:
return (d, copysign(s, y))
示例10: rAssertAlmostEqual
def rAssertAlmostEqual(a, b, rel_err = 2e-15, abs_err = 5e-323, msg=''):
"""Fail if the two floating-point numbers are not almost equal.
Determine whether floating-point values a and b are equal to within
a (small) rounding error. The default values for rel_err and
abs_err are chosen to be suitable for platforms where a float is
represented by an IEEE 754 double. They allow an error of between
9 and 19 ulps.
"""
# special values testing
if isnan(a):
if isnan(b):
return
raise AssertionError(msg + '%r should be nan' % (b,))
if isinf(a):
if a == b:
return
raise AssertionError(msg + 'finite result where infinity expected: '
'expected %r, got %r' % (a, b))
# if both a and b are zero, check whether they have the same sign
# (in theory there are examples where it would be legitimate for a
# and b to have opposite signs; in practice these hardly ever
# occur).
if not a and not b:
# only check it if we are running on top of CPython >= 2.6
if sys.version_info >= (2, 6) and copysign(1., a) != copysign(1., b):
raise AssertionError(msg + 'zero has wrong sign: expected %r, '
'got %r' % (a, b))
# if a-b overflows, or b is infinite, return False. Again, in
# theory there are examples where a is within a few ulps of the
# max representable float, and then b could legitimately be
# infinite. In practice these examples are rare.
try:
absolute_error = abs(b-a)
except OverflowError:
pass
else:
# test passes if either the absolute error or the relative
# error is sufficiently small. The defaults amount to an
# error of between 9 ulps and 19 ulps on an IEEE-754 compliant
# machine.
if absolute_error <= max(abs_err, rel_err * abs(a)):
return
raise AssertionError(msg + '%r and %r are not sufficiently close' % (a, b))
示例11: float_hex__Float
def float_hex__Float(space, w_float):
value = w_float.floatval
if not isfinite(value):
return str__Float(space, w_float)
if value == 0.0:
if copysign(1., value) == -1.:
return space.wrap("-0x0.0p+0")
else:
return space.wrap("0x0.0p+0")
mant, exp = math.frexp(value)
shift = 1 - max(rfloat.DBL_MIN_EXP - exp, 0)
mant = math.ldexp(mant, shift)
mant = abs(mant)
exp -= shift
result = ['\0'] * ((TOHEX_NBITS - 1) // 4 + 2)
result[0] = _char_from_hex(int(mant))
mant -= int(mant)
result[1] = "."
for i in range((TOHEX_NBITS - 1) // 4):
mant *= 16.0
result[i + 2] = _char_from_hex(int(mant))
mant -= int(mant)
if exp < 0:
sign = "-"
else:
sign = "+"
exp = abs(exp)
s = ''.join(result)
if value < 0.0:
return space.wrap("-0x%sp%s%d" % (s, sign, exp))
else:
return space.wrap("0x%sp%s%d" % (s, sign, exp))
示例12: test_special_values
def test_special_values():
from pypy.module.cmath.special_value import sqrt_special_values
assert len(sqrt_special_values) == 7
assert len(sqrt_special_values[4]) == 7
assert isinstance(sqrt_special_values[5][1], tuple)
assert sqrt_special_values[5][1][0] == 1e200 * 1e200
assert sqrt_special_values[5][1][1] == -0.
assert copysign(1., sqrt_special_values[5][1][1]) == -1.
示例13: div
def div(self, v1, v2):
# XXX this won't work after translation, probably requires ovfcheck
try:
return v1 / v2
except ZeroDivisionError:
if v1 == v2 == 0.0:
return rfloat.NAN
return rfloat.copysign(rfloat.INFINITY, v1 * v2)
示例14: c_asinh
def c_asinh(x, y):
if not isfinite(x) or not isfinite(y):
return asinh_special_values[special_type(x)][special_type(y)]
if fabs(x) > CM_LARGE_DOUBLE or fabs(y) > CM_LARGE_DOUBLE:
if y >= 0.:
real = copysign(math.log(math.hypot(x/2., y/2.)) +
M_LN2*2., x)
else:
real = -copysign(math.log(math.hypot(x/2., y/2.)) +
M_LN2*2., -x)
imag = math.atan2(y, fabs(x))
else:
s1x, s1y = c_sqrt(1.+y, -x)
s2x, s2y = c_sqrt(1.-y, x)
real = asinh(s1x*s2y - s2x*s1y)
imag = math.atan2(y, s1x*s2x - s1y*s2y)
return (real, imag)
示例15: pow
def pow(self, v1, v2):
try:
return math.pow(v1, v2)
except ValueError:
return rfloat.NAN
except OverflowError:
if math.modf(v2)[0] == 0 and math.modf(v2 / 2)[0] != 0:
# Odd integer powers result in the same sign as the base
return rfloat.copysign(rfloat.INFINITY, v1)
return rfloat.INFINITY