本文介绍了什么是单precision的浮点数的近似分辨率时其在零附近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储许多经度和纬度为双打,我想知道如果我能与他们存储为脱身花车

I am storing many longitudes and latitudes as doubles, I am wondering if I can get away with storing them as floats.

要回答这个问题,我需要了解

To answer this question, I need to know the approximate resolution of a single precision floating point number when the stored values are longitudes / latitudes (-180 to +180).

推荐答案

您的问题可能有几间pretations。

Your question may have several interpretations.

如果它只是一个角度和一个磁盘或设备,我会建议你使用的是完全不同的技术来存储你的价值观的存储:存储32位整数

If it is just for angles and for storage on a disk or on a device i would suggest you to store your values using a totally different technique: store as 32 bit integer.

int encodedAngle = (int)(value * (0x7FFFFFFF / 180.0));

要恢复它,做相反的。

double angle = (encodedAngle / (0x7FFFFFFF / 180.0));

在这种方式,你有180度和1位符号全31位分辨率。

In this way you have full 31 bit resolution for 180 degrees and 1 bit for the sign.

您可以用这种方式也让你的价值观在RAM中,这相比的丁文的成本较高,直接与双打的工作,但如果你想保持你的内存低,但分辨率高这可以工作得很好。
成本不高,只要转换到从整数/从/加倍和一个乘法,现代处理器会做的时间极少量,由于被访问的存储器少,如果列表中包含了大量的值,你的code将与处理器的高速缓存更加友好。

You can use this way also to keep your values in ram, the cost of this coversion is higher compared to work directly with doubles, but if you want to keep your memory low but resolution high this can work quite well.The cost is not so high, just a conversion to/from integer from/to double and a multiplication, modern processors will do it in a very little amount of time, and since the accessed memory is less, if the list contains a lot of values, your code will be more friendly with processor cache.

您的分辨率将会是 180 /((2 ^ 31) - 1)= 8.38190318×10 ^ -8 度,不坏:)

Your resolution will be 180 / ((2^31) - 1) = 8.38190318 × 10^-8 degrees, not bad :)

这篇关于什么是单precision的浮点数的近似分辨率时其在零附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:52