我不明白为什么我的模型中的浮点值比数据库中的值更不精确。
我们有一张桌子:
create_table "materials", force: :cascade do |t|
# ...
t.float "tiling_rotation"
end当我试图保存一个16小数的浮点数时,就会发生这样的情况:
m = Material.create!(tiling_rotation: 1.5707963267948966)
p m.tiling_rotation # 1.5707963267948966
m.reload
p m.tiling_rotation # 1.5707963267949
p m.id # 1234此时,我认为我的数据库将跟踪这个值,但是当我检查时,我得到了以下内容:
select tiling_rotation from materials where id = 1234
-- 1.5707963267948966我的数据库列的类型为double precision
这怎麽可能?如何将ruby的浮动精度设置为与数据库相同的精度?
发布于 2021-04-22 15:59:55
从对ActiveRecord的一点了解来看,这可能是由于数据库小数和Ruby之间的映射的精确性问题。
请尝试更改数据库模式,使"tiling_rotation“成为精度为16的十进制。
https://stackoverflow.com/questions/67215800
复制相似问题