Python内建类型之数值类型 - 整数、浮点数和复数
Manual
There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision. Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in sys.float_info. Complex numbers have a real and imaginary part, which are each a floating point number. To extract these parts from a complex number z, use z.real and z.imag. (The standard library includes additional numeric types, fractions that hold rationals, and decimal that hold floating-point numbers with user-definable precision.)
Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including hex, octal and binary numbers) yield integers. Numeric literals containing a decimal point or an exponent sign yield floating point numbers. Appending 'j' or 'J' to a numeric literal yields an imaginary number (a complex number with a zero real part) which you can add to an integer or float to get a complex number with real and imaginary parts.
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule. The constructors int(), float(), and complex() can be used to produce numbers of a specific type.
直译
共有三种不同的数值类型:整数、浮点数和复数。此外,布尔值为整数的子类型。整数具有无限精度。浮点数通常利用C语言中的double实现,关于你的电脑运行程序的浮点数的精度和内置表征信息,可通过sys.float_info来获取。复数含有实部和虚部,其各为浮点数。从复数‘z'中提取这些部分时,可以使用z.real和z.imag(标准库中包含额外数值类型,分数(fractions)支持有理数,小数(decimal)支持自定义精度浮点数)。Note
拓展阅读
- sys.float_info
- fractions
- decimal
- int()
- float()
- complex()