python 时间/高精度

1
>>> eval(repr(mpf(2.5)))

时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# coding: utf-8
#copyRight by heibanke

# 1. 从现在开始1000天后和1000天前是哪一天
import datetime

a=datetime.date.today()
b=datetime.datetime.now()
d1=datetime.timedelta(days=1000)
d2=datetime.timedelta(hours=1000)

(a-d1).isoformat()
(a+d1).strftime('%m/%d/%Y')
b.isoformat()
(b-d2)

# 2. 离你的重要纪念日还有多少天
important_day=datetime.datetime.strptime('2008-06-18','%Y-%m-%d')

important_day>b
d3=b-important_day
d3.days

t=datetime.time(12,11,30)

# 3. 两段程序哪个快些
############## time.time and time.clock ####################
import time

a=input("please input 0 or 1:")

start_time = time.time()
start_clock = time.clock()


if a:
sum_i=0
for i in range(100000):
sum_i+=i
else:
sum_i=sum(range(100000))

print sum_i

time.sleep(2)
end_time = time.time()
end_clock = time.clock()

print "time-delta:"
print start_time-end_time
print "clock-delta:"
print start_clock-end_clock

高精度

doc

  1. 另外下面的浮点换成区间,就用iv

The iv.mpf type represents a closed interval [a,b][a,b]; that is, the set {x:a≤x≤b}{x:a≤x≤b}, where aa and bb are arbitrary-precision floating-point values, possibly ±∞±∞. The iv.mpc type represents a rectangular complex interval [a,b]+[c,d]i[a,b]+[c,d]i; that is, the set {z=x+iy:a≤x≤b∧c≤y≤d}{z=x+iy:a≤x≤b∧c≤y≤d}.

  1. 低精度

    Fast low-precision arithmetic (fp)

普通精度

1
2
3
4
5
import decimal
decimal.getcontext().prec=200 #200数位精度
a=decimal.Decimal(3)**decimal.Decimal(123323)
b=decimal.Decimal('0.13')**decimal.Decimal('324325')
print('\t{}\n*\n\t{}\n=\n\t{}'.format(a,b,a*b))

高精度(mpmath)

对象类型

1
2
3
4
5
6
7
8
9
10
11
12
13
from mpmath import *
sin(1),cos(1)
mpf(1) == sin(1) ** 2 + cos(1) ** 2
mpf("inf") #浮点数
mpc(2,3) #复数

# 矩阵:
>>> mp.matrix([[1,0],[0,1]])
matrix(
[['1.0', '0.0'],
['0.0', '1.0']])
>>> _[0,0]
mpf('1.0')

精度控制

1
2
3
4
Mpmath settings:
mp.prec = 53 [default: 53] #二进制
mp.dps = 15 [default: 15] #十进制
mp.trap_complex = False [default: False]

不同规则

1
mp2 = mp.clone()

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Setting the mp.pretty option will use the str()-style output for repr() as well:

>>> mp.pretty = True
>>> mpf(0.6)
0.6
>>> mp.pretty = False
>>> mpf(0.6)
mpf('0.59999999999999998')

# The number of digits with which numbers are printed by default is determined by the working precision. To specify the number of digits to show without changing the working precision, use mpmath.nstr() and mpmath.nprint():

>>> a = mpf(1) / 6
>>> a
mpf('0.16666666666666666')
>>> nstr(a, 8)
'0.16666667'
>>> nprint(a, 8)
0.16666667
>>> nstr(a, 50)
'0.16666666666666665741480812812369549646973609924316'

基本功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
>>> mp.fsum([mpf('1.2'),1]) # 加法
mpf('2.200000000012')
>>> fprod([1, 2, 0.5, 7]) # 乘法
mpf('7.0')


>>> fadd(2, 1e-20, rounding='u') # 当然还有fsub
mpf('2.0000000000000004')
# Valid options are 'n' for nearest (default),
# 'f' for floor, 'c' for ceiling, 'd' for down, 'u' for up.

>>> x, y = mpf(2), mpf('1e1000')
>>> print(x - y + y) #把prec跳到5k也可以
0.0
>>> print(fsub(x, y, prec=inf) + y)
2.0
>>> print(fsub(x, y, exact=True) + y)
2.0

fneg,fmul,fdiv,fmod,fabs
sign

re, im # real\image part
arg polar # polar phase
>>> arg(-3j)
-1.5707963267949
>>> polar(-2)
(2.0, 3.14159265358979)
#旋转
>>> chop(rect(2, pi))
-2.0

# 点乘
>>> A = [2, 1.5, 3]
>>> B = [1, -1, 2]
>>> fdot(A, B)
mpf('6.5')
>>> list(zip(A, B))
[(2, 1), (1.5, -1), (3, 2)]
>>> fdot(_)
mpf('6.5')

高级eval

1
2
3
4
>>> mpmathify('3/4')
mpf('0.75')
>>> mpmathify('2+3j')
mpc(real='2.0', imag='3.0')
文章目录
  1. 1. 时间
  2. 2. 高精度
    1. 2.1. 普通精度
    2. 2.2. 高精度(mpmath)
      1. 2.2.1. 对象类型
      2. 2.2.2. 精度控制
      3. 2.2.3. 不同规则
      4. 2.2.4. 输出
      5. 2.2.5. 基本功能
      6. 2.2.6. 高级eval