# 使用pandas read_csv读写CSV时浮点数精度丢失

- **ID:** `data/csv-float-precision-loss`
- **领域:** data
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

pandas read_csv默认使用float64，会截断超过15-17位有效数字的浮点数，导致科学测量或金融交易等高精度数据静默丢失精度。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| pandas 1.5.3 | active | — | — |
| pandas 2.0.0 | active | — | — |
| pandas 2.1.0 | active | — | — |

## 解决方案

1. ```
   Use pandas.read_csv with dtype=Decimal for critical columns: `import decimal; df = pd.read_csv('data.csv', dtype={'amount': decimal.Decimal})`
   ```
2. ```
   Read CSV as string and convert to Decimal after: `df = pd.read_csv('data.csv', dtype=str); from decimal import Decimal; df['amount'] = df['amount'].apply(Decimal)`
   ```
3. ```
   Use numpy.float128 if available: `df = pd.read_csv('data.csv', dtype={'amount': np.float128})`
   ```

## 无效尝试

- **** — This disables all numeric processing and may break downstream operations expecting float types; also increases memory usage significantly. (60% 失败率)
- **** — Rounding cannot recover lost precision; the original value is already truncated during CSV parsing. (90% 失败率)
- **** — float64 is the default and still truncates; need higher precision type like float128 or decimal. (80% 失败率)
