本文整理匯總了Python中math.frexp方法的典型用法代碼示例。如果您正苦於以下問題:Python math.frexp方法的具體用法?Python math.frexp怎麽用?Python math.frexp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類math
的用法示例。
在下文中一共展示了math.frexp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ldexp
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def ldexp(cls, fraction, exponent):
"""Make an IBMFloat from fraction and exponent.
The is the inverse function of IBMFloat.frexp()
Args:
fraction: A Real in the range -1.0 to 1.0.
exponent: An integer in the range -256 to 255 inclusive.
"""
if not (-1.0 <= fraction <= 1.0):
raise ValueError("ldexp fraction {!r} out of range -1.0 to +1.0")
if not (-256 <= exponent < 256):
raise ValueError("ldexp exponent {!r} out of range -256 to 256")
ieee = fraction * 2**exponent
return IBMFloat.from_float(ieee)
示例2: testFrexp
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def testFrexp(self):
self.assertRaises(TypeError, math.frexp)
def testfrexp(name, result, expected):
(mant, exp), (emant, eexp) = result, expected
if abs(mant-emant) > eps or exp != eexp:
self.fail('%s returned %r, expected %r'%\
(name, (mant, exp), (emant,eexp)))
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
testfrexp('frexp(0)', math.frexp(0), (0, 0))
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
self.assertEqual(math.frexp(INF)[0], INF)
self.assertEqual(math.frexp(NINF)[0], NINF)
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
示例3: get_min_level
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def get_min_level(self, value):
"""Minimum cell level for given value.
Return the minimum level such that the metric is at most the given
value, or ``s2sphere.CellId.MAX_LEVEL`` if there is no such level.
For example, ``s2sphere.MAX_DIAG.get_min_level(0.1)`` returns the
minimum level such that all cell diagonal lengths are 0.1 or smaller.
The return value is always a valid level.
:param value:
Depending on whether this is used in one or two dimensions, this is
an angle in radians or a solid angle in steradians.
"""
if value <= 0:
return CellId.MAX_LEVEL
m, x = math.frexp(value / self.deriv())
level = max(0, min(CellId.MAX_LEVEL, -((x - 1) >> (self.__dim - 1))))
assert level == CellId.MAX_LEVEL or self.get_value(level) <= value
assert level == 0 or self.get_value(level - 1) > value
return level
示例4: get_max_level
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def get_max_level(self, value):
"""Maximum cell level for given value.
Return the maximum level such that the metric is at least the given
value, or zero if there is no such level. For example,
``s2sphere.MIN_WIDTH.get_max_level(0.1)`` returns the maximum level
such that all cells have a minimum width of 0.1 or larger.
The return value is always a valid level.
:param value:
Depending on whether this is used in one or two dimensions, this is
an angle in radians or a solid angle in steradians.
"""
if value <= 0:
return CellId.MAX_LEVEL
m, x = math.frexp(self.deriv() / value)
level = max(0, min(CellId.MAX_LEVEL, (x - 1) >> (self.__dim - 1)))
assert level == 0 or self.get_value(level) >= value
assert level == CellId.MAX_LEVEL or self.get_value(level + 1) < value
return level
示例5: testFrexp
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def testFrexp(self):
self.assertRaises(TypeError, math.frexp)
def testfrexp(name, result, expected):
(mant, exp), (emant, eexp) = result, expected
if abs(mant-emant) > eps or exp != eexp:
self.fail('%s returned %r, expected %r'%\
(name, result, expected))
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
testfrexp('frexp(0)', math.frexp(0), (0, 0))
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
self.assertEqual(math.frexp(INF)[0], INF)
self.assertEqual(math.frexp(NINF)[0], NINF)
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
示例6: __init__
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def __init__(self, r, g, b):
d = max(r, max(g, b))
if (d < EPS):
self.r = 0
self.g = 0
self.b = 0
self.e = 0
return
m, ie = math.frexp(d)
d = m * 256.0 / d
self.r = int(clamp(r * d, (0, 255)))
self.g = int(clamp(g * d, (0, 255)))
self.b = int(clamp(b * d, (0, 255)))
self.e = int(clamp(ie + 128, (0, 255)))
示例7: from_float
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def from_float(x, prec=53, rnd=round_fast):
"""Create a raw mpf from a Python float, rounding if necessary.
If prec >= 53, the result is guaranteed to represent exactly the
same number as the input. If prec is not specified, use prec=53."""
# frexp only raises an exception for nan on some platforms
if x != x:
return fnan
# in Python2.5 math.frexp gives an exception for float infinity
# in Python2.6 it returns (float infinity, 0)
try:
m, e = math.frexp(x)
except:
if x == math_float_inf: return finf
if x == -math_float_inf: return fninf
return fnan
if x == math_float_inf: return finf
if x == -math_float_inf: return fninf
return from_man_exp(int(m*(1<<53)), e-53, prec, rnd)
示例8: bytes_for_humans
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def bytes_for_humans(byte_count: int):
# Get power of two directly from floating point exponent bits (mantissa)
power_of_2 = frexp(byte_count)[1] - 1
binary_multiple = power_of_2 // 10
# If too big, represent in largest form
if binary_multiple >= len(byte_names):
binary_multiple = len(byte_names) - 1
# Gets the magnitude of the most significant multiple of 1024
impercise_magnitude = byte_count // (1 << (binary_multiple * 10))
# If less than 1024B, just return number of bytes
if binary_multiple == 0:
return str(impercise_magnitude) + ' B'
return str(impercise_magnitude) + ' ' \
+ byte_names[binary_multiple - 1] + 'B'
示例9: get_max_level
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def get_max_level(self, value):
if value <= 0:
return CellId.MAX_LEVEL
m, x = math.frexp(self.deriv() / value)
level = max(0, min(CellId.MAX_LEVEL, (x - 1) >> (self.__dim - 1)))
assert level == 0 or self.get_value(level) >= value
assert level == CellId.MAX_LEVEL or self.get_value(level + 1) < value
return level
示例10: encode_real
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def encode_real(data):
if data == float('inf'):
data = b'\x40'
elif data == float('-inf'):
data = b'\x41'
elif math.isnan(data):
data = b'\x42'
elif data == 0.0:
data = b''
else:
if data >= 0:
negative_bit = 0
else:
negative_bit = 0x40
data *= -1
mantissa, exponent = math.frexp(abs(data))
mantissa = int(mantissa * 2 ** 53)
lowest_set_bit = compiler.lowest_set_bit(mantissa)
mantissa >>= lowest_set_bit
mantissa |= (0x80 << (8 * ((mantissa.bit_length() // 8) + 1)))
mantissa = binascii.unhexlify(hex(mantissa)[4:].rstrip('L'))
exponent = (52 - lowest_set_bit - exponent)
if -129 < exponent < 128:
exponent = [0x80 | negative_bit, ((0xff - exponent) & 0xff)]
elif -32769 < exponent < 32768:
exponent = ((0xffff - exponent) & 0xffff)
exponent = [0x81 | negative_bit, (exponent >> 8), exponent & 0xff]
else:
raise NotImplementedError(
'REAL exponent {} out of range.'.format(exponent))
data = bytearray(exponent) + mantissa
return data
示例11: frexp
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def frexp(self):
"""Obtain the fraction and exponent.
Returns:
A pair where the first item is the fraction in the range -1.0 and +1.0 and the
exponent is an integer such that f = fraction * 2**exponent
"""
sign = -1 if self.signbit else 1
mantissa = sign * self.int_mantissa / _F24
exp_2 = self.exp16 * 4
return mantissa, exp_2
示例12: _write_float
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def _write_float(f, x):
import math
if x < 0:
sign = 0x8000
x = x * -1
else:
sign = 0
if x == 0:
expon = 0
himant = 0
lomant = 0
else:
fmant, expon = math.frexp(x)
if expon > 16384 or fmant >= 1: # Infinity or NaN
expon = sign|0x7FFF
himant = 0
lomant = 0
else: # Finite
expon = expon + 16382
if expon < 0: # denormalized
fmant = math.ldexp(fmant, expon)
expon = 0
expon = expon | sign
fmant = math.ldexp(fmant, 32)
fsmant = math.floor(fmant)
himant = long(fsmant)
fmant = math.ldexp(fmant - fsmant, 32)
fsmant = math.floor(fmant)
lomant = long(fsmant)
_write_short(f, expon)
_write_long(f, himant)
_write_long(f, lomant)
示例13: _write_float
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def _write_float(f, x):
import math
if x < 0:
sign = 0x8000
x = x * -1
else:
sign = 0
if x == 0:
expon = 0
himant = 0
lomant = 0
else:
fmant, expon = math.frexp(x)
if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN
expon = sign|0x7FFF
himant = 0
lomant = 0
else: # Finite
expon = expon + 16382
if expon < 0: # denormalized
fmant = math.ldexp(fmant, expon)
expon = 0
expon = expon | sign
fmant = math.ldexp(fmant, 32)
fsmant = math.floor(fmant)
himant = long(fsmant)
fmant = math.ldexp(fmant - fsmant, 32)
fsmant = math.floor(fmant)
lomant = long(fsmant)
_write_ushort(f, expon)
_write_ulong(f, himant)
_write_ulong(f, lomant)
示例14: __init__
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def __init__(self, size, sample_rate=16000, band_number=12, window=[50, 8000]):
self.size = 1 << math.frexp(size - 1)[1]
self.sample_rate = float(sample_rate)
self.resolution = self.sample_rate / self.size # (sample_rate/2) / (band/2)
self.set_band(band_number, window)
self.fft = FFT(self.size)
示例15: encode
# 需要導入模塊: import math [as 別名]
# 或者: from math import frexp [as 別名]
def encode(cls, scalar, n=None, max_int=None, precision=None, max_exponent=None):
"""return an encoding of an int or float.
"""
# Calculate the maximum exponent for desired precision
exponent = None
# Too low value preprocess;
# avoid "OverflowError: int too large to convert to float"
if np.abs(scalar) < 1e-200:
scalar = 0
if n is None:
n = cls.Q
max_int = cls.Q // 3 - 1
if precision is None:
if isinstance(scalar, int) or isinstance(scalar, np.int16) or \
isinstance(scalar, np.int32) or isinstance(scalar, np.int64):
exponent = 0
elif isinstance(scalar, float) or isinstance(scalar, np.float16) \
or isinstance(scalar, np.float32) or isinstance(scalar, np.float64):
flt_exponent = math.frexp(scalar)[1]
lsb_exponent = cls.FLOAT_MANTISSA_BITS - flt_exponent
exponent = math.floor(lsb_exponent / cls.LOG2_BASE)
else:
raise TypeError("Don't know the precision of type %s."
% type(scalar))
else:
exponent = math.floor(math.log(precision, cls.BASE))
if max_exponent is not None:
exponent = max(max_exponent, exponent)
int_fixpoint = int(round(scalar * pow(cls.BASE, exponent)))
if abs(int_fixpoint) > max_int:
raise ValueError('Integer needs to be within +/- %d but got %d'
% (max_int, int_fixpoint))
return cls(int_fixpoint % n, exponent, n, max_int)