Exemple #1
0
format(x, 'b')
format(x, '0')
format(x, 'x')

int('4d2', 16)
int('010100101', 2)

import os
# os.chmod('script.py', 0755)
# os.chmod('script.py', 0o755)

a = complex(2, 4)
b = 3 - 5j
a.real
a.imag
a.conjugate()
a + b
a-b
abs(a)

import numpy as np
a = np.array([2+3j, 4+5j, 6-7j, 8+9j])
a + 2
np.sin(a)

import random
values = [1, 2, 3, 4, 5, 6]
random.choice(values)

# maybe show some randint/shuffle and stuff
Exemple #2
0
print(len(data))
print(int.from_bytes(data, 'little'))  # 将字节转换为整型
print(int.from_bytes(data, 'big'))

x = 94522842520747284487117727783387188
print(x.to_bytes(16, 'big'))
print(x.to_bytes(16, 'little'))

# 3.6 复数运算 (复数是实属的延伸。实数是复数的真子集。实数=有理数+无理数。有理数=整数,0或分数。无理数=无限不循环小数)
a = complex(2, 4)
b = 3 - 5j
print(a)
print(b)
print(a.real)  # 复数实部
print(b.imag)  # 复数虚部
print(b.conjugate())  # 复数共轭值
import cmath

print(cmath.sin(a))  # 复数求正弦
print(cmath.cos(b))  # 复数求余弦
print(cmath.exp(a))  # 复数求平方根

# 3.7 处理无穷大和NaN
from numpy import inf  # numpy模块中的inf,标识无穷数
import math

a = float(inf)
b = float(-inf)
c = float(inf * 0)
print(a is inf)
print(math.isinf(a))  # math模块中的isinf函数,判断是否是inf
#################
# complex numbers
#################

a = complex(1, 2)
b = 1 + 2j
print(a == b)

## represented internally as float type
print(a.real)
print(type(a.real))

print(a.imag)
print(type(a.imag))

print(a.conjugate())

## operations
## // doesn't work
## % doesn't work
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a**b)

## equality
a = 0.1j
print(format(a.imag, ".25f"))
print(a + a + a == 0.3j)  # false
Exemple #4
0
# 大整数转化为字节字符串
x = 94522842520747284487117727783387188
x.to_bytes(16,
           'little')  # 小端 b'4\x00#\x00\x01\xef\xcd\x00\xab\x90x\x00V4\x12\x00'
x.to_bytes(16,
           'big')  # 大端 b'\x00\x124V\x00x\x90\xab\x00\xcd\xef\x01\x00#\x004'

# 6.复数运算
""" 使用 complex(real, imag) 或者带 j 后缀的浮点数来指定一个复数
复数支持所有常见的数学运算,复杂些的可以使用 cmath 模块
"""
a = complex(2, 4)  # 2+4j
b = 3 - 5j  # 3-5j
a.real  # 实部 2.0
a.imag  # 虚部 4.0
a.conjugate()  # 共轭复数 2-4j
# 所有常见数学运算都支持
a + b  # 5-1j
a * b  # 26+2j
# 复杂运算使用 cmath
import cmath
cmath.sin(a)  # (24.83130584894638-11.356612711218174j)

# 7.无穷大和NAN
""" 你可以使用 float 来创建 无穷大和 NaN
ATTENTION NaN 之间的比较总是返回 False
 """
a = float('inf')
b = float('-inf')
c = float('nan')
# 通过 math.isinf() 和 math.isnan 进行测试
Exemple #5
0
    nbytes+=1
print("nbytes:",nbytes)
print(x.to_bytes(nbytes,'big'))

# 3.6 复数的数学运算
# 你写的最新的网络认证方案代码遇到了一个难题,并且你唯一的解决办法就是使
# 用复数空间。再或者是你仅仅需要使用复数来执行一些计算操作。

# 复数可以用使用函数 complex(real, imag) 或者是带有后缀 j 的浮点数来指定。
# 比如:

a = complex(2, 4)
b = 3 - 5j
print(a,"and",b)
print(a.real,'and imag',a.imag)
print("a={},and a conjugate={}".format(a,a.conjugate()))

# 如果要执行其他的复数函数比如正弦、余弦或平方根,使用 cmath 模块:

import cmath

print("a sin use cmath module={}".format(cmath.sin(a)))

print("a cos use cmath modul={}".format(cmath.cos(a)))

print("a exp use cmath module={}".format(cmath.exp(a)))

import numpy as np

a = np.array([2+3j, 4+5j, 6-7j, 8+9j])
print(a)
Exemple #6
0
print(hex(x))

print(format(x, 'b'))
print(format(x, 'o'))
print(format(x, 'x'))

print(int('4d2', 16))
print(int('10011010010', 2))

# 复数
a = complex(2, 4)
b = 3 - 5j
print(a)
print(b)

print(a.real, a.imag, a.conjugate())
print(a + b)

# 执行其他复数函数比如正弦、余弦或平方根,使用cmath模块
import cmath
print(cmath.sin(a))
print(cmath.cos(a))

a = float('inf')
print(a)

# fractions 模块可以被用来执行包含分数的数学运算
from fractions import Fraction
a = Fraction(5, 4)
b = Fraction(7, 16)
print(a + b)
except OverflowError as e:
    print("Error Occured: ", e)

print(x.bit_length())
nbytes, rem = divmod(x.bit_length(), 8)
if rem:
    nbytes += 1

print(x.to_bytes(nbytes, 'little'))

# 3.6 Performing Complex-Valued Math
# complex(real, imag)
a = complex(2, 4)  # 2 + 4j  : 복소수
b = 3 - 5j
print('a.real= ', a.real, 'a.imag= ', a.imag)
print('a.conjugate()= ', a.conjugate())

print('a+b= ', a + b)
print('a*b= ', a * b)
print('a/b= ', a / b)
print('abs(a)= ', abs(a))

import cmath

print(cmath.sin(a))
print(cmath.cos(a))
print(cmath.exp(a))

import numpy as np

a = np.array([2 + 3j, 4 + 5j, 6 - 7j, 8 + 9j])
from math import floor
print(
    Fraction(2, 3) + Fraction(4, 5),
    Fraction(2, 3) - Fraction(4, 5),
    Fraction(2, 3) * Fraction(4, 5),
    Fraction(2, 3) / Fraction(4, 5),
    Fraction(2, 3) // Fraction(4, 5),
    Fraction(2, 3) % Fraction(4, 5), floor(Fraction(2, 3) + Fraction(4, 5)))

print(2j, 3 + 4j, type(2 - 2j), complex(5, -2), complex('(-9+29j)'),
      complex('-9+29j'))
# not allowed - complex('32 + 23j') white space is not allowed

c = 1 + 234j

print(c.real, c.imag, c.conjugate())

import math
#error when called t = math.sqrt(-1)

import cmath
# no error
t = cmath.sqrt(-123)
pp(t)

t = cmath.sqrt(-1)
pp(t)

print(cmath.phase(complex('1+1j')), abs(complex('1+1j')))
modulus, phase = cmath.polar(complex('1+1j'))
print(modulus, phase)