def __trunc__(self): return self.clone(math.trunc(self._value))
python类trunc()的实例源码
def __trunc__(self): return self.clone(math.trunc(float(self)))
def julian_datetime(julianday, milisecond=0):
"""Return datetime from days since 1/1/4713 BC and ms since midnight.
Convert Julian dates according to MetaMorph.
>>> julian_datetime(2451576, 54362783)
datetime.datetime(2000, 2, 2, 15, 6, 2, 783)
"""
if julianday <= 1721423:
# no datetime before year 1
return None
a = julianday + 1
if a > 2299160:
alpha = math.trunc((a - 1867216.25) / 36524.25)
a += 1 + alpha - alpha // 4
b = a + (1524 if a > 1721423 else 1158)
c = math.trunc((b - 122.1) / 365.25)
d = math.trunc(365.25 * c)
e = math.trunc((b - d) / 30.6001)
day = b - d - math.trunc(30.6001 * e)
month = e - (1 if e < 13.5 else 13)
year = c - (4716 if month > 2.5 else 4715)
hour, milisecond = divmod(milisecond, 1000 * 60 * 60)
minute, milisecond = divmod(milisecond, 1000 * 60)
second, milisecond = divmod(milisecond, 1000)
return datetime.datetime(year, month, day,
hour, minute, second, milisecond)
def trunc(number: Real) -> int:
"""
Truncates x to the nearest Integral toward 0.
"""
return _math.trunc(number)
# Operations on collections of numbers
def __trunc__(self): return self.clone(math.trunc(self._value))
def __trunc__(self): return self.clone(math.trunc(float(self)))
def trunc_example():
# additive
series = []
i = 0
for v in range(round(math.pi * 4.5 * 10)):
series.append((i, math.trunc(i / 10)))
i += 2
print_real(series)
def __trunc__(self):
return self.clone(math.trunc(self._value))
def __trunc__(self):
return self.clone(math.trunc(float(self)))
def create_colors(self, hue, num_colors=5, saturation=None, lightness=None):
if saturation is None:
saturation = 0.9
if lightness is None:
lightness = 40
else:
lightness *= 100
import math
saturation -= math.trunc(saturation)
print hue, saturation
import colorsys
''' Create n related colours '''
colors = []
for i in xrange(num_colors):
ix = i * (1.0/num_colors)
_lightness = (lightness + (ix * 40))/100.
if _lightness > 1.0:
_lightness = 1.0
color = colorsys.hls_to_rgb(hue, _lightness, saturation)
hex_color = '#'
for part in color:
hex_color += '%02x' % int(part * 255)
# check and remove any bad values
if not re.match('^\#[0-9a-f]{6}$', hex_color):
hex_color = '#FFFFFF'
colors.append(hex_color)
return colors
def testBigFloatComparisons(self):
# Because 10**23 can't be represented exactly as a float:
self.assertFalse(F(10**23) == float(10**23))
# The first test demonstrates why these are important.
self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1)))
self.assertTrue(1e23 < F(math.trunc(1e23) + 1))
self.assertFalse(1e23 <= F(math.trunc(1e23) - 1))
self.assertTrue(1e23 > F(math.trunc(1e23) - 1))
self.assertFalse(1e23 >= F(math.trunc(1e23) + 1))
def test_trunc(self):
for x in range(-250, 250):
s = '%0.2f' % (x / 100.0)
# should work the same as for floats
self.assertEqual(int(Decimal(s)), int(float(s)))
# should work the same as to_integral in the ROUND_DOWN mode
d = Decimal(s)
r = d.to_integral(ROUND_DOWN)
self.assertEqual(Decimal(math.trunc(d)), r)
def test_complex(self):
self.assertFalse(issubclass(complex, Real))
self.assertTrue(issubclass(complex, Complex))
c1, c2 = complex(3, 2), complex(4,1)
# XXX: This is not ideal, but see the comment in math_trunc().
self.assertRaises(TypeError, math.trunc, c1)
self.assertRaises(TypeError, operator.mod, c1, c2)
self.assertRaises(TypeError, divmod, c1, c2)
self.assertRaises(TypeError, operator.floordiv, c1, c2)
self.assertRaises(TypeError, float, c1)
self.assertRaises(TypeError, int, c1)
def __trunc__(self): return self.clone(math.trunc(self._value))
def __trunc__(self): return self.clone(math.trunc(float(self)))
def __trunc__(self): return self.clone(math.trunc(self._value))
def __trunc__(self): return self.clone(math.trunc(float(self)))
def get_balance_string(amount):
"""Gets the formatted bank balance string for a given amount"""
return 'Balance: {0}'.format(math.trunc(amount))
def deposit(self):
""" Deposit money to the bank.
"""
# Don't deposit more money than the user has
gross_amount = min(
math.trunc(self.__user_balance - self.__bank_traits['min_user_balance']),
self._config.bank_deposit)
fee = math.trunc(self.calculate_fee(gross_amount))
nett_amount = max(0, gross_amount - fee)
# update the bank balance
self.update_bank_balance(self.__bank_balance + nett_amount)
# subtract from user balance
if not self.dry_run:
if self._config.bank_type == 'mana':
self._hs.set_mp(max(0, self.__user_balance - gross_amount))
elif self._config.bank_type == 'health':
self._hs.set_hp(max(0, self.__user_balance - gross_amount))
else:
self._hs.set_gp(max(0, self.__user_balance - gross_amount))
message = '{2} Deposit: {0}, Fee: {1}'.format(
nett_amount,
fee,
self.__bank_traits['icon'])
self.notify(message)
def withdraw(self):
""" Withdraw money from the bank.
"""
# Don't withdraw more money than the bank has
gross_amount = min(self.__bank_balance, self._config.bank_withdraw)
# If the traits supports a max user balance, don't withdraw more
# than that amount
if self.__bank_traits['max_user_balance']:
gross_amount = min(
max(0, self.__bank_traits['max_user_balance'] - self.__user_balance),
gross_amount)
print('capping withdrawal to ', gross_amount)
fee = math.trunc(self.calculate_fee(gross_amount))
nett_amount = max(0, gross_amount - fee)
# update the bank balance
new_balance = max(0, self.__bank_balance - gross_amount)
self.update_bank_balance(new_balance)
# add to user balance
if not self.dry_run:
if self._config.bank_type == 'mana':
self._hs.set_mp(self.__user_balance + nett_amount)
elif self._config.bank_type == 'health':
self._hs.set_hp(self.__user_balance + nett_amount)
else:
self._hs.set_gp(self.__user_balance + nett_amount)
message = '{2} Withdrew: {0}, Fee: {1}'.format(
nett_amount,
fee,
self.__bank_traits['icon'])
self.notify(message)