Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我想生成一个包含所有Integers和.5值以及Int Range的数组. 结果想要: 最小值= 0 最大值= 5 生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5] 我知道如何只得到整数值: 阵列(0 … 5) 但不是这个浮动值… 我觉得很清楚,有人有想法吗? 使用带有步幅的Array()(from:through:by :): let arr = Array(st 我想生成一个包含所有Integers和.5值以及Int Range的数组.
结果想要:

最小值= 0

最大值= 5

生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]

我知道如何只得到整数值:

阵列(0 … 5)

但不是这个浮动值…
我觉得很清楚,有人有想法吗?

解决方法 使用带有步幅的Array()(from:through:by :):

let arr = Array(strIDe(from: 0,through: 5,by: 0.5))print(arr)

06001

如果您的最小值和最大值确实是Int,那么您需要将它们转换为Double:

let min = 0let max = 5let step = 0.5let arr = Array(strIDe(from: Double(min),through: Double(max),by: step))

警告:

由于nature of floating point math,如果步骤或端点不能精确表示为二进制浮点数,则可能会导致意外结果.

@MartinR举了一个例子:

let arr = Array(strIDe(from: 0,through: 0.7,by: 0.1))print(arr)

06004

排除端点0.7,因为该值稍微超出了它.

您可能还想考虑使用map生成数组:

// create array of 0 to 0.7 with step 0.1let arr2 = (0...7).map { Double(
// create an array from 0 to 5 with step 0.5let arr = (0...10).map { Double([+++]) / 2 }
) / 10 }

这将保证您捕获端点0.7(好吧,它的近似值).

所以,对于你原来的例子:

[+++] 总结

以上是内存溢出为你收集整理的arrays – 使用Int范围生成特定的浮点数组全部内容,希望文章能够帮你解决arrays – 使用Int范围生成特定的浮点数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我想生成一个包含所有Integers和.5值以及Int Range的数组. 结果想要: 最小值= 0 最大值= 5 生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5] 我知道如何只得到整数值: 阵列(0 … 5) 但不是这个浮动值… 我觉得很清楚,有人有想法吗? 使用带有步幅的Array()(from:through:by :): let arr = Array(st 我想生成一个包含所有Integers和.5值以及Int Range的数组.
结果想要:

最小值= 0

最大值= 5

生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]

我知道如何只得到整数值:

阵列(0 … 5)

但不是这个浮动值…
我觉得很清楚,有人有想法吗?

解决方法 使用带有步幅的Array()(from:through:by :):

let arr = Array(strIDe(from: 0,through: 5,by: 0.5))print(arr)

06001

如果您的最小值和最大值确实是Int,那么您需要将它们转换为Double:

let min = 0let max = 5let step = 0.5let arr = Array(strIDe(from: Double(min),through: Double(max),by: step))

警告:

由于nature of floating point math,如果步骤或端点不能精确表示为二进制浮点数,则可能会导致意外结果.

@MartinR举了一个例子:

let arr = Array(strIDe(from: 0,through: 0.7,by: 0.1))print(arr)

06004

排除端点0.7,因为该值稍微超出了它.

您可能还想考虑使用map生成数组:

// create array of 0 to 0.7 with step 0.1let arr2 = (0...7).map { Double(
// create an array from 0 to 5 with step 0.5let arr = (0...10).map { Double() / 2 }
) / 10 }

这将保证您捕获端点0.7(好吧,它的近似值).

所以,对于你原来的例子:

[+++] 总结

以上是内存溢出为你收集整理的arrays – 使用Int范围生成特定的浮点数组全部内容,希望文章能够帮你解决arrays – 使用Int范围生成特定的浮点数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
arrays – 使用Int范围生成特定的浮点数组_app_内存溢出

arrays – 使用Int范围生成特定的浮点数组

arrays – 使用Int范围生成特定的浮点数组,第1张

概述我想生成一个包含所有Integers和.5值以及Int Range的数组. 结果想要: 最小值= 0 最大值= 5 生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5] 我知道如何只得到整数值: 阵列(0 … 5) 但不是这个浮动值… 我觉得很清楚,有人有想法吗? 使用带有步幅的Array()(from:through:by :): let arr = Array(st 我想生成一个包含所有Integers和.5值以及Int Range的数组.
结果想要:

最小值= 0

最大值= 5

生成的数组= [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]

我知道如何只得到整数值:

阵列(0 … 5)

但不是这个浮动值…
我觉得很清楚,有人有想法吗?

解决方法 使用带有步幅的Array()(from:through:by :):

let arr = Array(strIDe(from: 0,through: 5,by: 0.5))print(arr)

06001

如果您的最小值和最大值确实是Int,那么您需要将它们转换为Double:

let min = 0let max = 5let step = 0.5let arr = Array(strIDe(from: Double(min),through: Double(max),by: step))

警告:

由于nature of floating point math,如果步骤或端点不能精确表示为二进制浮点数,则可能会导致意外结果.

@MartinR举了一个例子:

let arr = Array(strIDe(from: 0,through: 0.7,by: 0.1))print(arr)

06004

排除端点0.7,因为该值稍微超出了它.

您可能还想考虑使用map生成数组:

// create array of 0 to 0.7 with step 0.1let arr2 = (0...7).map { Double(
// create an array from 0 to 5 with step 0.5let arr = (0...10).map { Double() / 2 }
) / 10 }

这将保证您捕获端点0.7(好吧,它的近似值).

所以,对于你原来的例子:

总结

以上是内存溢出为你收集整理的arrays – 使用Int范围生成特定的浮点数组全部内容,希望文章能够帮你解决arrays – 使用Int范围生成特定的浮点数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1010571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存