Python Cookbook 2 number Date time (2)

Source: Internet
Author: User
Tags mathematical functions square root cmath

5. Conversion of bytes to large integers

#拥有128位长的16个元素的字节字符串. >>> data = B ' \x00\x124v\x00x\x90\xab\x00\xcd\xef\x01\x00#\x004 ' >>> len (data) 16 resolves the bytes to an integer, using the The Int.from_bytes () method # uses only the low-level arrangement with the python3# byte order rule (little or big) to specify only the byte when the integer is built. >>> int.from_bytes (data, ' little ') 69120565665751139577663547927094891008>>> int.from_bytes (data , ' big ') 94522842520747284487117727783387188>>> converts a large integer to a byte string, uses the Int.to_bytes () method, and specifies the number of bytes and byte order as follows: >>> x=94522842520747284487117727783387188>>> x.to_bytes (+, ' big ') B ' \x00\x124v\x00x\x90\xab\x00 \xcd\xef\x01\x00#\x004 ' >>> x.to_bytes (+, ' little ') B ' 4\x00#\x00\x01\xef\xcd\x00\xab\x90x\x00v4\x12\x00 ' >>> Use the Int.bit_length () method to determine how many byte bits are required to store this value >>> x=94522842520747284487117727783387188>>> X.bit_length (117)

6. Mathematical operations of complex numbers

Complex numbers can be specified by using Functions complex (real, imag) or floating-point numbers with suffix J

>>> a = complex (2, 4) >>> b= 3-5j>>> a (2+4j) > >> b (3-5j) corresponding to the real, imaginary and conjugate complex >>> a.real2.0>>> a.imag4.0>>>  A.conjugate () (2-4j) All common mathematical operations can work >>> a+b (5-1j) >>> a-b ( -1+9j) >>> a/b ( -0.4117647058823529+0.6470588235294118j) >>> abs (a) 4.47213595499958 performs other complex functions such as sine, cosine, or square root, using the Cmath module >>> import cmath>>> cmath.sin (a) (24.83130584894638-11.356612711218173J) > >> cmath.cos (a) ( -11.36423470640106-24.814651485634183j) >>> cmath.exp (a) (- 4.829809383269385-5.5920560936409816J) The standard mathematical functions of Python do not produce complex values in real cases >>> import math>>>  math.sqrt ( -1) traceback  (most recent call last):  file  "<stdin>" , line 1, in <module>valueerror: math domain error>>>  import cmath>>>&Nbsp;cmath.sqrt ( -1) 1j 

7. Infinity and Nan

Create or test floating-point numbers with positive infinity, negative infinity, or Nan (not a number)

>>> a = float (' inf ') >>> B = float ('-inf ') >>> c = float (' nan ') >>> ainf>>> b-i Nf>>> Cnan using Math.isinf () and Math.isnan () function detection >>> math.isinf (a) true>>> Math.isnan (c) True >>> Math.isinf (b) True infinite number propagates when performing mathematical calculations >>> a+45inf>>> a*10inf>>> 10/ a0.0 Some operations are undefined and return a nan result. >>> a/anan>>> A+bnannan values are propagated across all operations without an exception. >>> c+11nan>>> c/3nan>>> C*3nannan The value of a particular place when the comparison operation between them always returns false. >>> d=float (' nan ') >>> c==dfalse>>> C is dfalse for this reason, testing a nan is worth the only safe way is to use Math.isnan ()

8. Fractional operations

The fractions module can be used to perform mathematical operations that contain fractions.

>>> from fractions import fraction>>> a =fraction (5,4) >>> B = fraction (7, +) >>> AFra Ction (5, 4) >>> bfraction (7) >>> print a+b27/16>>> print a*b35/64 numerator denominator >>> c= a*b& Gt;>> c.numerator35>>> c.denominator64 Convert to decimal >>> float (c) 0.546875 limit denominator >>> c.limit_ Denominator (8) #分母不超过8的最接近的分数Fraction (4, 7) float converted to fractions >>> x=1.75>>> y = fraction (*x.as_integer_ Ratio ()) >>> yfraction (7, 4)


Python Cookbook 2 number Date time (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.