python基础-numeric types

数值类型(numeric types)

integers(整型)

什么是整数

数学上,整数是指正整数、零、负整数。python中也是如此。

整型可表示的范围

sys.maxsize
An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.

floating point numbers(浮点数)

科学计数法

科学记数法是一种记数的方法。把一个数表示成a与10的n次幂相乘的形式(1≤|a|<10,n为整数),这种记数法叫做科学记数法,在计算机中,10的幂通常用E或e来计数,例如,$ 31415926 = 3.1415926\ast10^7 = 3.1415926E7$

什么是浮点数

指一个数的小数点的位置不是固定的,是可以浮动的。浮点数在计算机中的存储规则是根据 IEEE 754标准。

浮点数是属于有理数中某特定子集的数的数字表示,在计算机中以近似表示任意某个实数。具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学记数法。

python中的浮点数

python中的浮点数是64位的双精度浮点数,可使用十进制或科学计数法表示。它的实现还是采用c语言的double float。

IEEE754 标准 64位双精度 计算机表示为 1S/11E/52M

1S (signed)表示符号位,用来表示正、负,占1个bit;

11E (exponent)表示阶码,可以理解为浮点数的 整数 或者说是指数部分,占11个bit;

52M (mantissa)表示尾数,可以理解为小数部分,或者说精度,占52个bit;

浮点数可表示的范围

使用sys.float_info 查看本机的浮点数的精度以及内部表征

attribute float.h macro explanation
epsilon DBL_EPSILON difference between 1.0 and the least value greater than 1.0 that is representable as a float
dig DBL_DIG maximum number of decimal digits that can be faithfully represented in a float; see below
mant_dig DBL_MANT_DIG float precision: the number of base-radix digits in the significand of a float
max DBL_MAX maximum representable positive finite float
max_exp DBL_MAX_EXP maximum integer e such that radix**(e-1) is a representable finite float
max_10_exp DBL_MAX_10_EXP maximum integer e such that 10**e is in the range of representable finite floats
min DBL_MIN minimum representable positive normalized float
min_exp DBL_MIN_EXP minimum integer e such that radix**(e-1) is a normalized float
min_10_exp DBL_MIN_10_EXP minimum integer e such that 10**e is a normalized float
radix FLT_RADIX radix of exponent representation
rounds FLT_ROUNDS integer constant representing the rounding mode used for arithmetic operations. This reflects the value of the system FLT_ROUNDS macro at interpreter startup time. See section 5.2.4.2.2 of the C99 standard for an explanation of the possible values and their meanings.

complex numbers(复数)

复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。

复数的特点:

  1. 复数由实数部分和虚数部分构成
  2. 虚部不能单独存在,它总是和一个值为 0.0 的实部一起构成一个复数。
  3. 表示虚部的语法: real + imag j
  4. 实部和虚部都是浮点数
  5. 虚部必须有后缀 j 或 J。

运算符

基本算数运算

Operation Result
x + y sum of x and y
x - y difference of x and y
x * y product of x and y
x / y quotient of x and y
x // y floored quotient of x and y
x % y remainder of x / y
-x x negated
+x x unchanged
abs(x) absolute value or magnitude of x
int(x) x converted to integer
float(x) x converted to floating point
complex(re, im) a complex number with real part re, imaginary part im. im defaults to zero.
c.conjugate() conjugate of the complex number c
divmod(x, y) the pair (x // y, x % y)
pow(x, y) x to the power y
x ** y x to the power y
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章