创建一个带有默认值的数组,但默认值被改变 [英] Creating an Array with a Default Value,But the default value is changed

查看:128
本文介绍了创建一个带有默认值的数组,但默认值被改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个数组,设置默认值是0.3,根据0.29999999999999999,为什么会这样?的

I create an array, set the default value is 0.3, according to 0.29999999999999999, why is this so?

推荐答案

这是因为浮点值的二进制重新presentation的。一些浮点值不能重新在给定的存储空间恰好psented $ P $,因此它们被舍入到一个适合的值。检查这个职位有关这个。如果你想避免这种大多数编程语言有一个使用基地10算术和没有这个限制的数据类型。对于SWIFT是 NSDecimalNumber ,但请注意,通常这数据类型是要慢得多。

This is because of the binary representation of the floating point values. Some floating point values can not be represented exactly in the given memory space, so they are rounded to a value that fits. Check this post for more details about this. If you want to avoid this most programming languages have a data type that uses base 10 arithmetic and does not have this limitation. For swift that is NSDecimalNumber, but please note that usually this data type is much slower.

这篇关于创建一个带有默认值的数组,但默认值被改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆