float() - 浮点数转换函数
将值转换为浮点数类型
float() - 浮点数转换函数
📝 概述
float() 是 Python 中的内置函数,用于将数值或字符串转换为浮点数类型。它支持多种格式的数值转换,包括科学计数法、无穷大和非数值等特殊值的处理。
🎯 学习目标
- 掌握 float()函数的基本用法
- 理解不同数据类型到浮点数的转换规则
- 学会处理特殊浮点数值(inf、nan)
- 了解浮点数精度和表示范围
📋 前置知识
- Python 基本数据类型
- 数值系统基础
- 浮点数表示原理
🔍 详细内容
基本概念
float() 函数将输入值转换为浮点数。Python 使用 IEEE 754 双精度浮点数标准,提供约 15-17 位十进制精度。
语法格式
## 无参数调用,返回 0.0
float()
## 转换数值或字符串
float(x)
参数说明
| 参数名 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| x | number/string | 否 | 无 | 要转换的数值或字符串 |
返回值
- 类型: float
- 说明: 转换后的浮点数值
💡 实际应用
基础用法
## 无参数调用
result = float() # 返回 0.0
print(f"无参数调用: {result}") # 输出: 无参数调用: 0.0
print(f"类型: {type(result)}") # 输出: 类型: <class 'float'>
## 整数转换
int_num = 42
float_num = float(int_num)
print(f"整数转换: {float_num}") # 输出: 整数转换: 42.0
## 字符串转换
str_num = "3.14159"
converted = float(str_num)
print(f"字符串转换: {converted}") # 输出: 字符串转换: 3.14159
## 布尔值转换
print(f"True 转换: {float(True)}") # 输出: True 转换: 1.0
print(f"False 转换: {float(False)}") # 输出: False 转换: 0.0
科学计数法
## 科学计数法字符串
scientific_notation = [
"1.23e4", # 12300.0
"1.23E4", # 12300.0
"1.23e-4", # 0.000123
"1.23E-4", # 0.000123
"6.022e23", # 阿伏伽德罗常数
]
for notation in scientific_notation:
result = float(notation)
print(f"{notation} = {result}")
## 输出:
## 1.23e4 = 12300.0
## 1.23E4 = 12300.0
## 1.23e-4 = 0.000123
## 1.23E-4 = 0.000123
## 6.022e23 = 6.022e+23
特殊值处理
## 无穷大
positive_inf = float('inf') # 正无穷
negative_inf = float('-inf') # 负无穷
positive_inf2 = float('infinity') # 正无穷的另一种写法
print(f"正无穷: {positive_inf}")
print(f"负无穷: {negative_inf}")
print(f"无穷大检查: {positive_inf == float('inf')}")
## 非数值 (Not a Number)
nan_value = float('nan')
print(f"NaN 值: {nan_value}")
print(f"NaN 检查: {nan_value != nan_value}") # NaN 的特殊性质
## 使用 math 模块检查特殊值
import math
print(f"是否为无穷: {math.isinf(positive_inf)}")
print(f"是否为 NaN: {math.isnan(nan_value)}")
print(f"是否为有限数: {math.isfinite(3.14)}")
高级用法
## 处理不同格式的数值字符串
number_strings = [
" 3.14 ", # 带空格
"+3.14", # 带正号
"-3.14", # 带负号
"3.", # 省略小数部分
".14", # 省略整数部分
"0.0", # 零值
]
for num_str in number_strings:
try:
result = float(num_str)
print(f"'{num_str}' -> {result}")
except ValueError as e:
print(f"'{num_str}' -> 错误: {e}")
实际案例:数据清洗
def clean_numeric_data(data_list, default_value=0.0):
"""清洗数值数据,处理各种异常情况"""
cleaned_data = []
error_log = []
for i, item in enumerate(data_list):
try:
# # 处理 None 值
if item is None:
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: None 值已替换为默认值")
continue
# # 处理字符串
if isinstance(item, str):
# # 去除空格
item = item.strip()
# # 处理空字符串
if not item:
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: 空字符串已替换为默认值")
continue
# # 处理特殊字符串
if item.lower() in ['na', 'n/a', 'null', 'none']:
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: 缺失值标记已替换为默认值")
continue
# # 转换为浮点数
result = float(item)
# # 检查是否为有效数值
if math.isnan(result):
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: NaN 值已替换为默认值")
elif math.isinf(result):
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: 无穷值已替换为默认值")
else:
cleaned_data.append(result)
except (ValueError, TypeError) as e:
cleaned_data.append(default_value)
error_log.append(f"索引 {i}: 转换错误 '{item}' - {str(e)}")
return cleaned_data, error_log
## 测试数据
test_data = [
"3.14", "2.71", None, "", " 5.0 ",
"invalid", "inf", "nan", "N/A", 42
]
cleaned, errors = clean_numeric_data(test_data)
print(f"清洗后数据: {cleaned}")
print("\n 错误日志:")
for error in errors:
print(f" {error}")
精度和范围
import sys
## 浮点数信息
print(f"浮点数最大值: {sys.float_info.max}")
print(f"浮点数最小正值: {sys.float_info.min}")
print(f"浮点数精度: {sys.float_info.dig} 位")
print(f"浮点数机器精度: {sys.float_info.epsilon}")
## 精度演示
print("\n 精度演示:")
print(f"0.1 + 0.2 = {0.1 + 0.2}")
print(f"0.1 + 0.2 == 0.3: {0.1 + 0.2 == 0.3}")
## 精度比较的正确方法
def float_equal(a, b, tolerance=1e-9):
"""浮点数相等比较"""
return abs(a - b) < tolerance
print(f"使用容差比较: {float_equal(0.1 + 0.2, 0.3)}")
⚠️ 注意事项
精度问题
## 浮点数精度限制
print(f"大数精度: {float('9' * 20)}")
print(f"小数精度: {float('0.' + '0' * 15 + '1')}")
## 避免精度问题的方法
from decimal import Decimal
## 使用 Decimal 进行精确计算
decimal_result = Decimal('0.1') + Decimal('0.2')
print(f"Decimal 结果: {decimal_result}")
print(f"Decimal 等于 0.3: {decimal_result == Decimal('0.3')}")
异常处理
## 常见转换错误
invalid_inputs = [
"abc", # 无效字符串
"3.14.15", # 多个小数点
"3 + 4", # 表达式
"", # 空字符串
"3.14e", # 不完整的科学计数法
]
for invalid_input in invalid_inputs:
try:
result = float(invalid_input)
print(f"'{invalid_input}' -> {result}")
except ValueError as e:
print(f"'{invalid_input}' -> 错误: {e}")
性能考虑
import time
## 大量转换的性能测试
data = [str(i + 0.5) for i in range(100000)]
## 方法 1:列表推导式
start_time = time.time()
results1 = [float(x) for x in data]
time1 = time.time() - start_time
## 方法 2:map 函数
start_time = time.time()
results2 = list(map(float, data))
time2 = time.time() - start_time
print(f"列表推导式耗时: {time1:.4f}秒")
print(f"map 函数耗时: {time2:.4f}秒")
print(f"性能提升: {(time1/time2-1)*100:.1f}%")
🔗 相关内容
相关函数
- int() - 整数转换函数 - 转换为整数
- str() - 字符串转换函数 - 转换为字符串
- bool() - 布尔转换函数 - 转换为布尔值
- round() - 四舍五入函数 - 浮点数四舍五入
- abs() - 绝对值函数 - 计算绝对值
相关模块
- math 模块 - 数学函数
- decimal 模块 - 精确小数运算
- fractions 模块 - 分数运算
相关概念
- Python 数据类型 - 基本数据类型
- 数值精度 - 数值精度详解
- 异常处理 - 异常处理机制
📚 扩展阅读
🏷️ 标签
类型转换 浮点数 数值处理 精度 科学计数法
最后更新: 2024-01-15
作者: Python 文档工程师
版本: 1.0
讨论与反馈
欢迎在下方留言讨论,分享你的学习心得或提出问题。评论基于GitHub Issues,需要GitHub账号。