Field的float精度问题

import taichi as ti
ti.init(arch = ti.cpu)

x= ti.Vector.field(1,ti.f64,8)
x.fill(0.3)
print(x)

以上运行后

[Taichi] version 1.0.1, llvm 10.0.0, commit 1c3619d9, win, python 3.10.4
[Taichi] Starting on arch=x64
[[0.30000001]
 [0.30000001]
 [0.30000001]
 [0.30000001]
 [0.30000001]
 [0.30000001]
 [0.30000001]
 [0.30000001]]

上述结果可以看到一个明显的误差,而且以下判断式为false

x[0].x==0.3
False

我的脚本跑出来是相等的。你再跑一下试试?

import taichi as ti
#ti.init(arch = ti.cpu, default_fp=ti.f64)
ti.init(arch=ti.cpu)

x= ti.Vector.field(1,ti.f64,8)

@ti.kernel
def test():
	for i in x:
	    print(x[i], " ", x[i] == 0.3)


x.fill(0.3)
test()

Hey @Jiao_luhuai thanks for the report! It’s indeed something we’re interested in fixing. I created Float precision issue · Issue #5059 · taichi-dev/taichi · GitHub to track the progress of fixing the issue.
@YuPeng Yours is fine since the comparison is done in taichi scope but it’ll fail if you do it in python scope.

1 个赞

@ailing @YuPeng Thank you