Numpy Arange 生成小数点不一致的值

Sae*_*aed 5 python numpy python-3.x data-science

有人可以解释一下这里发生了什么吗?

为什么 0.3 和 0.7 的值有更多的小数点?我只想要 1 位小数点值。

threshold_range = np.arange(0.1,1,0.1)
threshold_range.tolist()
Run Code Online (Sandbox Code Playgroud)
[Output]: [0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7000000000000001, 0.8, 0.9]
Run Code Online (Sandbox Code Playgroud)

bha*_*atk 5

使用np.round

前任。

import numpy as np

threshold_range = np.arange(0.1,1,0.1)
print(threshold_range.tolist())
print(np.round(threshold_range, 2).tolist())
Run Code Online (Sandbox Code Playgroud)

输出:

[0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7000000000000001, 0.8, 0.9]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
Run Code Online (Sandbox Code Playgroud)