本文整理汇总了Python中pypy.rlib.rfloat.isnan函数的典型用法代码示例。如果您正苦于以下问题:Python isnan函数的具体用法?Python isnan怎么用?Python isnan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isnan函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_math_sqrt
def test_math_sqrt(self):
def f(x):
try:
return math.sqrt(x)
except ValueError:
return -INFINITY
res = self.interp_operations(f, [0.0])
assert res == 0.0
self.check_operations_history(call_pure=1)
#
res = self.interp_operations(f, [25.0])
assert res == 5.0
self.check_operations_history(call_pure=1)
#
res = self.interp_operations(f, [-0.0])
assert str(res) == '-0.0'
self.check_operations_history(call_pure=1)
#
res = self.interp_operations(f, [1000000.0])
assert res == 1000.0
self.check_operations_history(call_pure=1)
#
res = self.interp_operations(f, [-1.0])
assert res == -INFINITY
self.check_operations_history(call_pure=0)
#
res = self.interp_operations(f, [INFINITY])
assert isinf(res) and not isnan(res) and res > 0.0
self.check_operations_history(call_pure=0)
#
res = self.interp_operations(f, [NAN])
assert isnan(res) and not isinf(res)
self.check_operations_history(call_pure=0)
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:34,代码来源:test_math.py
示例2: AbstractEC
def AbstractEC(ctx, x, y):
"""
Implements the Abstract Equality Comparison x == y
trying to be fully to the spec
"""
if isinstance(x, W_IntNumber) and isinstance(y, W_IntNumber):
return x.intval == y.intval
if isinstance(x, W_FloatNumber) and isinstance(y, W_FloatNumber):
if isnan(x.floatval) or isnan(y.floatval):
return False
return x.floatval == y.floatval
type1 = x.type()
type2 = y.type()
if type1 == type2:
if type1 == "undefined" or type1 == "null":
return True
if type1 == "number":
n1 = x.ToNumber(ctx)
n2 = y.ToNumber(ctx)
if isnan(n1) or isnan(n2):
return False
if n1 == n2:
return True
return False
elif type1 == "string":
return x.ToString(ctx) == y.ToString(ctx)
elif type1 == "boolean":
return x.ToBoolean() == x.ToBoolean()
# XXX rethink it here
return x.ToString(ctx) == y.ToString(ctx)
else:
# step 14
if (type1 == "undefined" and type2 == "null") or (type1 == "null" and type2 == "undefined"):
return True
if type1 == "number" and type2 == "string":
return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber(ctx)))
if type1 == "string" and type2 == "number":
return AbstractEC(ctx, W_FloatNumber(x.ToNumber(ctx)), y)
if type1 == "boolean":
return AbstractEC(ctx, W_FloatNumber(x.ToNumber(ctx)), y)
if type2 == "boolean":
return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber(ctx)))
if (type1 == "string" or type1 == "number") and type2 == "object":
return AbstractEC(ctx, x, y.ToPrimitive(ctx))
if (type2 == "string" or type2 == "number") and type1 == "object":
return AbstractEC(ctx, x.ToPrimitive(ctx), y)
return False
objtype = x.GetValue().type()
if objtype == y.GetValue().type():
if objtype == "undefined" or objtype == "null":
return True
if isinstance(x, W_String) and isinstance(y, W_String):
r = x.ToString(ctx) == y.ToString(ctx)
else:
r = x.ToNumber(ctx) == y.ToNumber(ctx)
return r
开发者ID:rafaelcaricio,项目名称:lang-js,代码行数:58,代码来源:baseop.py
示例3: check_roundtrip
def check_roundtrip(x):
s = c_pack(x)
assert s == pack(x)
if not isnan(x):
assert unpack(s) == x
assert c_unpack(s) == x
else:
assert isnan(unpack(s))
assert isnan(c_unpack(s))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:9,代码来源:test_rstruct.py
示例4: __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)
开发者ID:gorakhargosh,项目名称:pypy,代码行数:12,代码来源:model.py
示例5: 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))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:48,代码来源:test_cmath.py
示例6: 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)
开发者ID:ieure,项目名称:pypy,代码行数:34,代码来源:test_genc.py
示例7: 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
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:30,代码来源:interp_cmath.py
示例8: push_primitive_constant
def push_primitive_constant(self, TYPE, value):
ilasm = self.ilasm
if TYPE is ootype.Void:
pass
elif TYPE is ootype.Bool:
ilasm.opcode("ldc.i4", str(int(value)))
elif TYPE is ootype.Char or TYPE is ootype.UniChar:
ilasm.opcode("ldc.i4", ord(value))
elif TYPE is ootype.Float:
if isinf(value):
if value < 0.0:
ilasm.opcode("ldc.r8", "(00 00 00 00 00 00 f0 ff)")
else:
ilasm.opcode("ldc.r8", "(00 00 00 00 00 00 f0 7f)")
elif isnan(value):
ilasm.opcode("ldc.r8", "(00 00 00 00 00 00 f8 ff)")
else:
ilasm.opcode("ldc.r8", repr(value))
elif isinstance(value, CDefinedIntSymbolic):
ilasm.opcode("ldc.i4", DEFINED_INT_SYMBOLICS[value.expr])
elif TYPE in (ootype.Signed, ootype.Unsigned, rffi.SHORT):
ilasm.opcode("ldc.i4", str(value))
elif TYPE in (ootype.SignedLongLong, ootype.UnsignedLongLong):
ilasm.opcode("ldc.i8", str(value))
elif TYPE in (ootype.String, ootype.Unicode):
if value._str is None:
ilasm.opcode("ldnull")
else:
ilasm.opcode("ldstr", string_literal(value._str))
else:
assert False, "Unexpected constant type"
开发者ID:junion,项目名称:butlerbot-unstable,代码行数:31,代码来源:ilgenerator.py
示例9: round
def round(space, number, w_ndigits=0):
"""round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative."""
# Algorithm copied directly from CPython
# interpret 2nd argument as a Py_ssize_t; clip on overflow
ndigits = space.getindex_w(w_ndigits, None)
# nans, infinities and zeros round to themselves
if number == 0 or isinf(number) or isnan(number):
return space.wrap(number)
# Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
# always rounds to itself. For ndigits < NDIGITS_MIN, x always
# rounds to +-0.0.
if ndigits > NDIGITS_MAX:
return space.wrap(number)
elif ndigits < NDIGITS_MIN:
# return 0.0, but with sign of x
return space.wrap(0.0 * number)
# finite x, and ndigits is not unreasonably large
z = round_double(number, ndigits)
if isinf(z):
raise OperationError(space.w_OverflowError, space.wrap("rounded value too large to represent"))
return space.wrap(z)
开发者ID:junion,项目名称:butlerbot-unstable,代码行数:28,代码来源:operation.py
示例10: interp_operations
def interp_operations(self, f, args, **kwds):
# get the JitCodes for the function f
_get_jitcodes(self, self.CPUClass, f, args, self.type_system, **kwds)
# try to run it with blackhole.py
result1 = _run_with_blackhole(self, args)
# try to run it with pyjitpl.py
result2 = _run_with_pyjitpl(self, args)
assert result1 == result2 or isnan(result1) and isnan(result2)
# try to run it by running the code compiled just before
result3 = _run_with_machine_code(self, args)
assert result1 == result3 or result3 == NotImplemented or isnan(result1) and isnan(result3)
#
if (longlong.supports_longlong and
isinstance(result1, longlong.r_float_storage)):
result1 = longlong.getrealfloat(result1)
return result1
开发者ID:gorakhargosh,项目名称:pypy,代码行数:16,代码来源:support.py
示例11: 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
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:25,代码来源:interp_cmath.py
示例12: 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)')
开发者ID:craigkerstiens,项目名称:pypy,代码行数:7,代码来源:complexobject.py
示例13: 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
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:31,代码来源:interp_cmath.py
示例14: format_float
def format_float(self, w_value, char):
space = self.space
x = space.float_w(maybe_float(space, w_value))
if isnan(x):
if char in 'EFG':
r = 'NAN'
else:
r = 'nan'
elif isinf(x):
if x < 0:
if char in 'EFG':
r = '-INF'
else:
r = '-inf'
else:
if char in 'EFG':
r = 'INF'
else:
r = 'inf'
else:
prec = self.prec
if prec < 0:
prec = 6
if char in 'fF' and x/1e25 > 1e25:
char = chr(ord(char) + 1) # 'f' => 'g'
flags = 0
if self.f_alt:
flags |= DTSF_ALT
r = formatd(x, char, prec, flags)
self.std_wp_number(r)
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:30,代码来源:formatting.py
示例15: ll_math_fmod
def ll_math_fmod(x, y):
if isinf(y):
if isinf(x):
raise ValueError("math domain error")
return x # fmod(x, +/-Inf) returns x for finite x (or if x is a NaN).
_error_reset()
r = math_fmod(x, y)
errno = rposix.get_errno()
if isnan(r):
if isnan(x) or isnan(y):
errno = 0
else:
errno = EDOM
if errno:
_likely_raise(errno, r)
return r
开发者ID:ieure,项目名称:pypy,代码行数:17,代码来源:ll_math.py
示例16: isfinitejs
def isfinitejs(ctx, args, this):
if len(args) < 1:
return newbool(True)
n = args[0].ToNumber(ctx)
if isinf(n) or isnan(n):
return newbool(False)
else:
return newbool(True)
开发者ID:rafaelcaricio,项目名称:lang-js,代码行数:8,代码来源:interpreter.py
示例17: _erfc
def _erfc(x):
if rfloat.isnan(x):
return x
absx = abs(x)
if absx < ERF_SERIES_CUTOFF:
return 1. - _erf_series(x)
else:
cf = _erfc_contfrac(absx)
return cf if x > 0. else 2. - cf
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:9,代码来源:interp_math.py
示例18: compare
def compare(ctx, x, y):
if isinstance(x, W_IntNumber) and isinstance(y, W_IntNumber):
return x.intval > y.intval
if isinstance(x, W_FloatNumber) and isinstance(y, W_FloatNumber):
if isnan(x.floatval) or isnan(y.floatval):
return -1
return x.floatval > y.floatval
s1 = x.ToPrimitive(ctx, "Number")
s2 = y.ToPrimitive(ctx, "Number")
if not (isinstance(s1, W_String) and isinstance(s2, W_String)):
s4 = s1.ToNumber(ctx)
s5 = s2.ToNumber(ctx)
if isnan(s4) or isnan(s5):
return False
return s4 > s5
else:
s4 = s1.ToString(ctx)
s5 = s2.ToString(ctx)
return s4 > s5
开发者ID:rafaelcaricio,项目名称:lang-js,代码行数:19,代码来源:baseop.py
示例19: long__Float
def long__Float(space, w_floatobj):
try:
return W_LongObject.fromfloat(space, w_floatobj.floatval)
except OverflowError:
if isnan(w_floatobj.floatval):
raise OperationError(
space.w_ValueError,
space.wrap("cannot convert float NaN to integer"))
raise OperationError(space.w_OverflowError,
space.wrap("cannot convert float infinity to long"))
开发者ID:craigkerstiens,项目名称:pypy,代码行数:10,代码来源:floatobject.py
示例20: floorjs
def floorjs(ctx, args, this):
if len(args) < 1:
return W_FloatNumber(NAN)
val = args[0].ToNumber(ctx)
pos = math.floor(val)
if isnan(val):
pos = INFINITY
return W_FloatNumber(pos)
开发者ID:rafaelcaricio,项目名称:lang-js,代码行数:11,代码来源:interpreter.py
注:本文中的pypy.rlib.rfloat.isnan函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论