gpt4 book ai didi

swift - 以 0.01 迭代数字在几次迭代后添加 .0000000000000001

转载 作者:行者123 更新时间:2023-11-28 06:45:33 24 4
gpt4 key购买 nike

<分区>

我正在尝试创建一对从 1.0 到 1.25 的数字数组。我使用以下代码:

heightDisplayArray: [String] = []
heightArray: [Double] = []

for i in 1.stride(to: 1.25, by: 0.01){

heightDisplayArray.append("\(i) meters")
heightArray.append(i)

}

print (heightDisplayArray)
print (heightArray)

出于某种原因,结果按预期附加到 heightDisplayArray,但向 heightArray 添加了一些意外数字。控制台显示如下:

["1.0 meters", "1.01 meters", "1.02 meters", "1.03 meters", "1.04 meters", "1.05 meters", "1.06 meters", "1.07 meters", "1.08 meters", "1.09 meters", "1.1 meters", "1.11 meters", "1.12 meters", "1.13 meters", "1.14 meters", "1.15 meters", "1.16 meters", "1.17 meters", "1.18 meters", "1.19 meters", "1.2 meters", "1.21 meters", "1.22 meters", "1.23 meters", "1.24 meters"]

[1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.0600000000000001, 1.0700000000000001, 1.0800000000000001, 1.0900000000000001, 1.1000000000000001, 1.1100000000000001, 1.1200000000000001, 1.1300000000000001, 1.1400000000000001, 1.1500000000000001, 1.1600000000000001, 1.1700000000000002, 1.1800000000000002, 1.1900000000000002, 1.2000000000000002, 1.2100000000000002, 1.2200000000000002, 1.2300000000000002, 1.2400000000000002]

我该如何纠正这种行为?我可以将 heightArray 更改为字符串数组而不是 double 组,但我更愿意找出实际导致这种情况发生的原因。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com