def log2(x):
return math.frexp(x)[1] - 1
python类frexp()的实例源码
def frexp(node): return merge([node], math.frexp)
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)
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 = int(fsmant)
fmant = math.ldexp(fmant - fsmant, 32)
fsmant = math.floor(fmant)
lomant = int(fsmant)
_write_ushort(f, expon)
_write_ulong(f, himant)
_write_ulong(f, lomant)
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)
def mag(ctx, z):
if z:
return ctx.frexp(abs(z))[1]
return ctx.ninf
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)
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 = int(fsmant)
fmant = math.ldexp(fmant - fsmant, 32)
fsmant = math.floor(fmant)
lomant = int(fsmant)
_write_ushort(f, expon)
_write_ulong(f, himant)
_write_ulong(f, lomant)
def deg2dms (dgs) :
f, d = math.modf (dgs)
f = abs (f)
m = 60.0 * f
f, m = math.modf (m)
#print math.frexp (f), math.frexp (m)
s = 60.0 * f
dms = "%dd%02d'%09.6f\"" % (d, m, s)
#print dms
return dms
#
###
#
# Convert from polar to rectangular coordinates
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'
def mag(ctx, z):
if z:
return ctx.frexp(abs(z))[1]
return ctx.ninf
def SetFog(self,fog):
projection = (fog.function >> 3) & 1
if projection:
if fog.z_far == fog.z_near or fog.z_end == fog.z_start:
A = 0
C = 0
else:
A = (fog.z_far - fog.z_near)/(fog.z_end - fog.z_start)
C = (fog.z_start - fog.z_near)/(fog.z_end - fog.z_start)
b_shift = 0
b_magnitude = 0
else:
if fog.z_far == fog.z_near or fog.z_end == fog.z_start:
A = 0
B = 0.5
C = 0
else:
A = fog.z_far*fog.z_near/((fog.z_far - fog.z_near)*(fog.z_end - fog.z_start))
B = fog.z_far/(fog.z_far - fog.z_near)
C = fog.z_start/(fog.z_end - fog.z_start)
if B > 1:
b_shift = 1 + int(ceil(log(B,2)))
elif 0 < B < 0.5:
b_shift = 0
else:
b_shift = 1
A /= 2**b_shift
b_magnitude = int(2*(B/2**b_shift)*8388638)
a_mantissa,a_exponent = frexp(A)
self.fog_param0[0:11] = int(abs(a_mantissa)*2**12) & 0x7FF
self.fog_param0[11:19] = a_exponent + 126 if A != 0 else 0
self.fog_param0[19] = a_mantissa < 0
self.fog_param1[0:24] = b_magnitude
self.fog_param2[0:5] = b_shift
c_mantissa,c_exponent = frexp(C)
self.fog_param3[0:11] = int(abs(c_mantissa)*2**12) & 0x7FF
self.fog_param3[11:19] = c_exponent + 126 if C != 0 else 0
self.fog_param3[19] = c_mantissa < 0
self.fog_param3[20:21] = projection
self.fog_param3[21:24] = fog.function
self.fog_color[0:8] = fog.color.b
self.fog_color[8:16] = fog.color.g
self.fog_color[16:24] = fog.color.r
def float_pack(x, size):
"""Convert a Python float x into a 64-bit unsigned integer
with the same byte representation."""
if size == 8:
MIN_EXP = -1021 # = sys.float_info.min_exp
MAX_EXP = 1024 # = sys.float_info.max_exp
MANT_DIG = 53 # = sys.float_info.mant_dig
BITS = 64
elif size == 4:
MIN_EXP = -125 # C's FLT_MIN_EXP
MAX_EXP = 128 # FLT_MAX_EXP
MANT_DIG = 24 # FLT_MANT_DIG
BITS = 32
else:
raise ValueError("invalid size value")
sign = math.copysign(1.0, x) < 0.0
if math.isinf(x):
mant = 0
exp = MAX_EXP - MIN_EXP + 2
elif math.isnan(x):
mant = 1 << (MANT_DIG-2) # other values possible
exp = MAX_EXP - MIN_EXP + 2
elif x == 0.0:
mant = 0
exp = 0
else:
m, e = math.frexp(abs(x)) # abs(x) == m * 2**e
exp = e - (MIN_EXP - 1)
if exp > 0:
# Normal case.
mant = round_to_nearest(m * (1 << MANT_DIG))
mant -= 1 << MANT_DIG - 1
else:
# Subnormal case.
if exp + MANT_DIG - 1 >= 0:
mant = round_to_nearest(m * (1 << exp + MANT_DIG - 1))
else:
mant = 0
exp = 0
# Special case: rounding produced a MANT_DIG-bit mantissa.
assert 0 <= mant <= 1 << MANT_DIG - 1
if mant == 1 << MANT_DIG - 1:
mant = 0
exp += 1
# Raise on overflow (in some circumstances, may want to return
# infinity instead).
if exp >= MAX_EXP - MIN_EXP + 2:
raise OverflowError("float too large to pack in this format")
# check constraints
assert 0 <= mant < 1 << MANT_DIG - 1
assert 0 <= exp <= MAX_EXP - MIN_EXP + 2
assert 0 <= sign <= 1
return ((sign << BITS - 1) | (exp << MANT_DIG - 1)) | mant