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 查看本机的浮点数的精度以及内部表征

attributefloat.h macroexplanation
epsilonDBL_EPSILONdifference between 1.0 and the least value greater than 1.0 that is representable as a float
digDBL_DIGmaximum number of decimal digits that can be faithfully represented in a float; see below
mant_digDBL_MANT_DIGfloat precision: the number of base-radix digits in the significand of a float
maxDBL_MAXmaximum representable positive finite float
max_expDBL_MAX_EXPmaximum integer e such that radix**(e-1) is a representable finite float
max_10_expDBL_MAX_10_EXPmaximum integer e such that 10**e is in the range of representable finite floats
minDBL_MINminimum representable positive normalized float
min_expDBL_MIN_EXPminimum integer e such that radix**(e-1) is a normalized float
min_10_expDBL_MIN_10_EXPminimum integer e such that 10**e is a normalized float
radixFLT_RADIXradix of exponent representation
roundsFLT_ROUNDSinteger 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。

运算符

基本算数运算

OperationResult
x + ysum of x and y
x - ydifference of x and y
x * yproduct of x and y
x / yquotient of x and y
x // yfloored quotient of x and y
x % yremainder of x / y
-xx negated
+xx 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 ** yx to the power y
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sif_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值