Skip to content

Float

一句话解释

float (floating-点 数字) 是 数字 使用 decimal 点, 如 3.14 or -0.5.

用大白话说

A float is a number that can have a fractional part, represented with a decimal point. Python uses IEEE 754 double-precision (64-bit) floating-point, the same as most languages.

Floats are created by writing a literal with a decimal point (3.14), using scientific notation (1e-3), or converting with float(). Division with / always returns a float, even for integer operands.

Floats have limited precision — 0.1 + 0.2 equals 0.30000000000000004, not exactly 0.3. This is a fundamental property of floating-point arithmetic, not a Python bug. For exact decimal arithmetic, use the decimal module.

示例

python
pi = 3.14159
tiny = 1e-6
result = 7 / 2

print(pi)           # 3.14159
print(tiny)         # 1e-06
print(result)       # 3.5
print(0.1 + 0.2)    # 0.30000000000000004
print(type(pi))     # <class 'float'>

Floats 有 decimal 点, support scientific notation, and 有 precision limitations.

常见混淆

  • 容易混淆的对象: integer

    关键区别: float 有 fractional 部分 and 有限 precision. integer 是 整个 数字 使用 精确 precision. Use int() 到 truncate float 到 integer, or round() 到 round 到 nearest integer.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表