本文整理匯總了Python中numpy.longdouble方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.longdouble方法的具體用法?Python numpy.longdouble怎麽用?Python numpy.longdouble使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.longdouble方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: best_float
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def best_float():
""" Floating point type with best precision
This is nearly always np.longdouble, except on Windows, where np.longdouble
is Intel80 storage, but with float64 precision for calculations. In that
case we return float64 on the basis it's the fastest and smallest at the
highest precision.
Returns
-------
best_type : numpy type
floating point type with highest precision
"""
if (type_info(np.longdouble)['nmant'] > type_info(np.float64)['nmant'] and
machine() != 'sparc64'): # sparc has crazy-slow float128
return np.longdouble
return np.float64
示例2: test_as_int
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_as_int():
# Integer representation of number
assert_equal(as_int(2.0), 2)
assert_equal(as_int(-2.0), -2)
assert_raises(FloatingError, as_int, 2.1)
assert_raises(FloatingError, as_int, -2.1)
assert_equal(as_int(2.1, False), 2)
assert_equal(as_int(-2.1, False), -2)
v = np.longdouble(2**64)
assert_equal(as_int(v), 2**64)
# Have all long doubles got 63+1 binary bits of precision? Windows 32-bit
# longdouble appears to have 52 bit precision, but we avoid that by checking
# for known precisions that are less than that required
try:
nmant = type_info(np.longdouble)['nmant']
except FloatingError:
nmant = 63 # Unknown precision, let's hope it's at least 63
v = np.longdouble(2) ** (nmant + 1) - 1
assert_equal(as_int(v), 2**(nmant + 1) -1)
# Check for predictable overflow
nexp64 = floor_log2(type_info(np.float64)['max'])
val = np.longdouble(2**nexp64) * 2 # outside float64 range
assert_raises(OverflowError, as_int, val)
assert_raises(OverflowError, as_int, -val)
示例3: test_float_types
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_float_types(tp):
""" Check formatting.
This is only for the str function, and only for simple types.
The precision of np.float32 and np.longdouble aren't the same as the
python float precision.
"""
for x in [0, 1, -1, 1e20]:
assert_equal(str(tp(x)), str(float(x)),
err_msg='Failed str formatting for type %s' % tp)
if tp(1e16).itemsize > 4:
assert_equal(str(tp(1e16)), str(float('1e16')),
err_msg='Failed str formatting for type %s' % tp)
else:
ref = '1e+16'
assert_equal(str(tp(1e16)), ref,
err_msg='Failed str formatting for type %s' % tp)
示例4: test_str
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_str(self):
svals = [0.0, -0.0, 1, -1, np.inf, -np.inf, np.nan]
styps = [np.float16, np.float32, np.float64, np.longdouble]
wanted = [
['0.0', '0.0', '0.0', '0.0' ],
['-0.0', '-0.0', '-0.0', '-0.0'],
['1.0', '1.0', '1.0', '1.0' ],
['-1.0', '-1.0', '-1.0', '-1.0'],
['inf', 'inf', 'inf', 'inf' ],
['-inf', '-inf', '-inf', '-inf'],
['nan', 'nan', 'nan', 'nan']]
for wants, val in zip(wanted, svals):
for want, styp in zip(wants, styps):
msg = 'for str({}({}))'.format(np.dtype(styp).name, repr(val))
assert_equal(str(styp(val)), want, err_msg=msg)
示例5: test_floating_overflow
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_floating_overflow(self):
""" Strings containing an unrepresentable float overflow """
fhalf = np.half('1e10000')
assert_equal(fhalf, np.inf)
fsingle = np.single('1e10000')
assert_equal(fsingle, np.inf)
fdouble = np.double('1e10000')
assert_equal(fdouble, np.inf)
flongdouble = assert_warns(RuntimeWarning, np.longdouble, '1e10000')
assert_equal(flongdouble, np.inf)
fhalf = np.half('-1e10000')
assert_equal(fhalf, -np.inf)
fsingle = np.single('-1e10000')
assert_equal(fsingle, -np.inf)
fdouble = np.double('-1e10000')
assert_equal(fdouble, -np.inf)
flongdouble = assert_warns(RuntimeWarning, np.longdouble, '-1e10000')
assert_equal(flongdouble, -np.inf)
示例6: test_known_types
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_known_types():
# Test we are correctly compiling parameters for known types
for ftype, ma_like in ((np.float16, _float_ma[16]),
(np.float32, _float_ma[32]),
(np.float64, _float_ma[64])):
assert_ma_equal(_discovered_machar(ftype), ma_like)
# Suppress warning for broken discovery of double double on PPC
with np.errstate(all='ignore'):
ld_ma = _discovered_machar(np.longdouble)
bytes = np.dtype(np.longdouble).itemsize
if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
# 80-bit extended precision
assert_ma_equal(ld_ma, _float_ma[80])
elif (ld_ma.it, ld_ma.maxexp) == (112, 16384) and bytes == 16:
# IEE 754 128-bit
assert_ma_equal(ld_ma, _float_ma[128])
示例7: test_sum
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_sum(self):
for dt in (np.int, np.float16, np.float32, np.float64, np.longdouble):
for v in (0, 1, 2, 7, 8, 9, 15, 16, 19, 127,
128, 1024, 1235):
tgt = dt(v * (v + 1) / 2)
d = np.arange(1, v + 1, dtype=dt)
assert_almost_equal(np.sum(d), tgt)
assert_almost_equal(np.sum(d[::-1]), tgt)
d = np.ones(500, dtype=dt)
assert_almost_equal(np.sum(d[::2]), 250.)
assert_almost_equal(np.sum(d[1::2]), 250.)
assert_almost_equal(np.sum(d[::3]), 167.)
assert_almost_equal(np.sum(d[1::3]), 167.)
assert_almost_equal(np.sum(d[::-2]), 250.)
assert_almost_equal(np.sum(d[-1::-2]), 250.)
assert_almost_equal(np.sum(d[::-3]), 167.)
assert_almost_equal(np.sum(d[-1::-3]), 167.)
# sum with first reduction entry != 0
d = np.ones((1,), dtype=dt)
d += d
assert_almost_equal(d, 2.)
示例8: int_to_float
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def int_to_float(val, flt_type):
""" Convert integer `val` to floating point type `flt_type`
Why is this so complicated?
At least in numpy <= 1.6.1, numpy longdoubles do not correctly convert to
ints, and ints do not correctly convert to longdoubles. Specifically, in
both cases, the values seem to go through float64 conversion on the way, so
to convert better, we need to split into float64s and sum up the result.
Parameters
----------
val : int
Integer value
flt_type : object
numpy floating point type
Returns
-------
f : numpy scalar
of type `flt_type`
"""
if not flt_type is np.longdouble:
return flt_type(val)
faval = np.longdouble(0)
while val != 0:
f64 = np.float64(val)
faval += f64
val -= int(f64)
return faval
示例9: have_binary128
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def have_binary128():
""" True if we have a binary128 IEEE longdouble
"""
ti = type_info(np.longdouble)
return (ti['nmant'], ti['maxexp']) == (112, 16384)
示例10: test_best_float
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_best_float():
# Finds the most capable floating point type
# The only time this isn't np.longdouble is when np.longdouble has float64
# precision.
best = best_float()
end_of_ints = np.float64(2**53)
# float64 has continuous integers up to 2**53
assert_equal(end_of_ints, end_of_ints + 1)
# longdouble may have more, but not on 32 bit windows, at least
end_of_ints = np.longdouble(2**53)
if (end_of_ints == (end_of_ints + 1) or # off continuous integers
machine() == 'sparc64'): # crippling slow longdouble on sparc
assert_equal(best, np.float64)
else:
assert_equal(best, np.longdouble)
示例11: test_nmant
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_nmant():
for t in IEEE_floats:
assert_equal(type_info(t)['nmant'], np.finfo(t).nmant)
if (LD_INFO['nmant'], LD_INFO['nexp']) == (63, 15):
assert_equal(type_info(np.longdouble)['nmant'], 63)
示例12: test_usable_binary128
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_usable_binary128():
# Check for usable binary128
yes = have_binary128()
exp_test = np.longdouble(2) ** 16383
assert_equal(yes,
exp_test.dtype.itemsize == 16 and
np.isfinite(exp_test) and
_check_nmant(np.longdouble, 112))
示例13: test_best_write_scale_ftype
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_best_write_scale_ftype():
# Test best write scaling type
# Types return better of (default, array type) unless scale overflows.
# Return float type cannot be less capable than the input array type
for dtt in IUINT_TYPES + FLOAT_TYPES:
arr = np.arange(10, dtype=dtt)
assert_equal(best_write_scale_ftype(arr, 1, 0),
better_float_of(dtt, np.float32))
assert_equal(best_write_scale_ftype(arr, 1, 0, np.float64),
better_float_of(dtt, np.float64))
assert_equal(best_write_scale_ftype(arr, np.float32(2), 0),
better_float_of(dtt, np.float32))
assert_equal(best_write_scale_ftype(arr, 1, np.float32(1)),
better_float_of(dtt, np.float32))
# Overflowing ints with scaling results in upcast
best_vals = ((np.float32, np.float64),)
if np.longdouble in OK_FLOATS:
best_vals += ((np.float64, np.longdouble),)
for lower_t, higher_t in best_vals:
# Information on this float
L_info = type_info(lower_t)
t_max = L_info['max']
nmant = L_info['nmant'] # number of significand digits
big_delta = lower_t(2**(floor_log2(t_max) - nmant)) # delta below max
# Even large values that don't overflow don't change output
arr = np.array([0, t_max], dtype=lower_t)
assert_equal(best_write_scale_ftype(arr, 1, 0), lower_t)
# Scaling > 1 reduces output values, so no upcast needed
assert_equal(best_write_scale_ftype(arr, lower_t(1.01), 0), lower_t)
# Scaling < 1 increases values, so upcast may be needed (and is here)
assert_equal(best_write_scale_ftype(arr, lower_t(0.99), 0), higher_t)
# Large minus offset on large array can cause upcast
assert_equal(best_write_scale_ftype(arr, 1, -big_delta/2.01), lower_t)
assert_equal(best_write_scale_ftype(arr, 1, -big_delta/2.0), higher_t)
# With infs already in input, default type returns
arr[0] = np.inf
assert_equal(best_write_scale_ftype(arr, lower_t(0.5), 0), lower_t)
arr[0] = -np.inf
assert_equal(best_write_scale_ftype(arr, lower_t(0.5), 0), lower_t)
示例14: test_float128
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def test_float128(self):
hdr = self.header_class()
if have_binary128():
hdr.set_data_dtype(np.longdouble)
assert_equal(hdr.get_data_dtype().type, np.longdouble)
else:
assert_raises(HeaderDataError, hdr.set_data_dtype, np.longdouble)
示例15: _ftype4scaled_finite
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import longdouble [as 別名]
def _ftype4scaled_finite(tst_arr, slope, inter, direction='read',
default=np.float32):
""" Smallest float type for scaling of `tst_arr` that does not overflow
"""
assert direction in ('read', 'write')
if not default in OK_FLOATS and default is np.longdouble:
# Omitted longdouble
return default
def_ind = OK_FLOATS.index(default)
# promote to arrays to avoid numpy scalar casting rules
tst_arr = np.atleast_1d(tst_arr)
slope = np.atleast_1d(slope)
inter = np.atleast_1d(inter)
warnings.filterwarnings('ignore', '.*overflow.*', RuntimeWarning)
try:
for ftype in OK_FLOATS[def_ind:]:
tst_trans = tst_arr.copy()
slope = slope.astype(ftype)
inter = inter.astype(ftype)
if direction == 'read': # as in reading of image from disk
if slope != 1.0:
tst_trans = tst_trans * slope
if inter != 0.0:
tst_trans = tst_trans + inter
elif direction == 'write':
if inter != 0.0:
tst_trans = tst_trans - inter
if slope != 1.0:
tst_trans = tst_trans / slope
if np.all(np.isfinite(tst_trans)):
return ftype
finally:
warnings.filters.pop(0)
raise ValueError('Overflow using highest floating point type')