# Python numpy.longdouble() Examples

The following are 30 code examples for showing how to use numpy.longdouble(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module , or try the search function .

Example 1
```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 ```
Example 2
```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) ```
Example 3
```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) ```
Example 4
```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) ```
Example 5
```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) ```
Example 6
```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]) ```
Example 7
```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.) ```
Example 8
```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 ```
Example 9
```def have_binary128():
""" True if we have a binary128 IEEE longdouble
"""
ti = type_info(np.longdouble)
return (ti['nmant'], ti['maxexp']) == (112, 16384) ```
Example 10
```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) ```
Example 11
```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) ```
Example 12
```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)) ```
Example 13
```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) ```
Example 14
```def test_float128(self):
if have_binary128():
hdr.set_data_dtype(np.longdouble)
assert_equal(hdr.get_data_dtype().type, np.longdouble)
else:
Example 15
```def _ftype4scaled_finite(tst_arr, slope, inter, direction='read',
default=np.float32):
""" Smallest float type for scaling of `tst_arr` that does not overflow
"""
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') ```
Example 16
```def test_clean_part_all_scalar_types(self):
"""Test single scalar value for all valid data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = dtype(1)
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) ```
Example 17
```def test_clean_part_all_np_array_types(self):
"""Test scalar value in numpy array of ndim=0 for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = np.array(1, dtype=dtype)
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) ```
Example 18
```def test_clean_part_all_np_array_types2(self):
"""Test numpy array for all types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = np.array([1, 2], dtype=dtype)
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) ```
Example 19
```def test_clean_part_list_all_types(self):
"""Test list of a single value for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = [dtype(1)]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) ```
Example 20
```def test_clean_part_list_all_types2(self):
"""List of list of numbers of all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = [dtype(1), dtype(2)]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) ```
Example 21
```def test_clean_part_tuple_all_types2(self):
"""Test tuple of a single value for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]:
num = (dtype(1), dtype(2))
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1, 2], dtype=float)) ```
Example 22
```def test_clean_part_list_list_array(self):
"""List of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble:
num = [[array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)]]
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) ```
Example 23
```def test_clean_part_tuple_list_array(self):
"""Tuple of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble:
num = ([array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)],)
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) ```
Example 24
```def test_clean_part_list_tuple_array(self):
"""List of tuple of numpy array for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble:
num = [(array([1, 1], dtype=dtype), array([2, 2], dtype=dtype))]
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) ```
Example 25
```def test_clean_part_tuple_tuples_arrays(self):
"""Tuple of tuples of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble:
num = ((array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)),
(array([3, 4], dtype=dtype), array([4, 4], dtype=dtype)))
num_ = _clean_part(num)

assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) ```
Example 26
```def test_clean_part_list_list_arrays(self):
"""List of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble:
num = [[array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)],
[array([3, 3], dtype=dtype), array([4, 4], dtype=dtype)]]
num_ = _clean_part(num)

assert len(num_) == 2
assert np.all([isinstance(part, list) for part in num_])
assert np.all([len(part) == 2 for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
np.testing.assert_array_equal(num_[1][0], array([3.0, 3.0], dtype=float))
np.testing.assert_array_equal(num_[1][1], array([4.0, 4.0], dtype=float)) ```
Example 27
```def test_precision(self):
# not looping results in a useful stack trace upon failure
self.do_precision(np.half, np.single)
self.do_precision(np.half, np.double)
self.do_precision(np.half, np.longdouble)
self.do_precision(np.single, np.double)
self.do_precision(np.single, np.longdouble)
self.do_precision(np.double, np.longdouble) ```
Example 28
```def _getconv(dtype):
""" Find the correct dtype converter. Adapted from matplotlib """

def floatconv(x):
x.lower()
if '0x' in x:
return float.fromhex(x)
return float(x)

typ = dtype.type
if issubclass(typ, np.bool_):
return lambda x: bool(int(x))
if issubclass(typ, np.uint64):
return np.uint64
if issubclass(typ, np.int64):
return np.int64
if issubclass(typ, np.integer):
return lambda x: int(float(x))
elif issubclass(typ, np.longdouble):
return np.longdouble
elif issubclass(typ, np.floating):
return floatconv
elif issubclass(typ, complex):
return lambda x: complex(asstr(x).replace('+-', '-'))
elif issubclass(typ, np.bytes_):
return asbytes
elif issubclass(typ, np.unicode_):
return asunicode
else:
return asstr

# amount of lines loadtxt reads in one chunk, can be overridden for testing ```
Example 29
```def test_longdouble_norm(self):
# Non-regression test: p-norm of longdouble would previously raise
# UnboundLocalError.
x = np.arange(10, dtype=np.longdouble)
old_assert_almost_equal(norm(x, ord=3), 12.65, decimal=2) ```
Example 30
```def test_nan_inf_float(tp):