简体   繁体   English

解析iOS-SDK-十进制数字加零

[英]Parse iOS-SDK - decimal number adding zeros

I have a list of Products in Parse, the format is like so; 我有一个Parse中的产品列表,格式如下:

{
    Environment = Both;
    "Ground_indoor" = Finished;
    "Ground_outdoor" = Finished;
    "Max_Working_Height" = "9.800000000000001";
    Name = "8m Battery Scissor Lift - Genie GS2632";
    "Non_Marking_Tyres" = 1;
    "Platform_Length" = "2.26";
    "Platform_Type" = "Scissor Lift";
    "Platform_Width" = "0.8100000000000001";
    "Tax_Class" = E;
    Weight = 1956;
}

However for some strange reason, anything with an "8" it's adding some random zeros in between. 但是,出于某些奇怪的原因,任何带有“ 8”的东西之间都会添加一些随机零。 For example Platform_Width is actually; 例如Platform_Width实际上是;

http://puu.sh/mlGLo/be9f200472.png http://puu.sh/mlGLo/be9f200472.png

Any clues? 有什么线索吗?

Yes this is due to floating point inaccuracy. 是的,这是因为浮点数不准确。 You may find very academic explanation about it here . 您可能会在这里找到关于它的非常学术性的解释。

Or if that is too much to read, let's simply put it this way - for example, let's take the value 0.81 as an example. 或者,如果要阅读的内容太多了,我们就这样简单地进行说明-例如,以0.81为例。 The number cannot be accurately represented by a float and is rounded up to 0.8100000000000001 because an int value of 81 is represented by the binary value 1010001. In order to make the value 0.81 it would be accurate if you could take 81 x 10^-2 (= 81 / 10^2.) But that's impossible because you must use the base 2 instead of 10. So the closest to 10^2 = 100 would be 128 = 2^7. 该数字不能精确地由浮点数表示,并且四舍五入为0.8100000000000001,因为int值81由二进制值1010001表示。为了使值成为0.81,如果可以采用81 x 10 ^ -2,则将是准确的。 (= 81/10 ^ 2。)但这是不可能的,因为必须使用基数2而不是10。因此最接近10 ^ 2 = 100的值将是128 = 2 ^ 7。 The total number of bits you need is 9 : 6 for the value 81 (1010001) + 3 bits for the value 7 (111). 对于值81(1010001),您需要的总位数为9:6;对于值7(111),则需要3位。 Then the value 81 x 2^-7 is not seriously inaccurate. 那么,值81 x 2 ^ -7并不是很不准确。 So to improve this inaccuracy, we could change the value 81 and 7 to something else. 因此,为了改善这种不准确性,我们可以将值81和7更改为其他值。 So the formula for your value would be X = A x 2^B where A and B are integer values positive or negative. 因此,您的值的公式为X = A x 2 ^ B,其中A和B是整数值,正或负。 The higher the numbers are the higher accuracy become. 数字越高,精度越高。 However as you know the number of bits to represent the values A and B are limited. 但是,您知道代表值A和B的位数是有限的。 For float you have a total number of 32. Double has 64 and Decimal has 128. 对于float,总数为32。Double的总数为64,Decimal的总数为128。

Hope this explains your question. 希望这能解释您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM