i have a problem with double values i need to store in an android homed sqlite database. since these double values represent gps values (lat & lng), i really NEED
an absolute precision down to the 9th number after the comma.
我有一个问题,我需要在android homed sqlite数据库中存储双重值。由于这些双值表示gps值(lat & lng),我确实需要一个绝对精度,直到逗号后的第9个数字。
now i have a table like this:
现在我有这样一张桌子:
CREATE TABLE x REAL lng;
and insert sth (hardcoded) like:
插入某物(硬编码)如:
INSERT INTO x lng = '1.0';
and when reading lng from this table into some (java) double variable, i get a value like "0.999956837" - this renders the values pretty useless to me.
当从这个表中读取lng到某个(java)双变量时,我得到一个值,比如“0.999956837”——这使得这些值对我来说非常无用。
is there a way to enforce the precision i need other than storing the values as "text" fields (what would make expensive casts neccessary) or storing them as integers (meaning i need to multiply/divide at each write/read-op)?
除了将值存储为“文本”字段(将会使昂贵的转换为必需的)或将它们存储为整数(这意味着我需要在每个写/读操作上增加/分配)之外,是否还有一种方法来执行我所需要的精度呢?
2 个解决方案
#1
13
SQLite is typeless, that means all representation is written as text, probably the wrapper api does some converts you don't know of, that you get those results.
SQLite是无类型的,这意味着所有的表示都是作为文本编写的,可能包装器api会做一些您不知道的转换,您会得到这些结果。
If you need to store the data as string do it.
如果需要以字符串形式存储数据,请这样做。
Just when you read out the double make sure you saved in the right format, you can use getDouble
on the column.
当您读取double时,请确保以正确的格式保存,您可以在列上使用getDouble。
#2
4
double
has about 17 decimal digits of precision, so if 9 digits is what you need, there should be no problem (assuming that you don't do any complex calculations on those values). Just make sure you never end up using float
, because that has only about 7 digits of precision.
double的精度大约是17位小数,所以如果需要9位数字,应该没有问题(假设您没有对这些值进行任何复杂的计算)。只要确保你永远不会使用浮点数,因为它只有7位精度。
You should also make sure you understand how binary floating-point works, and that it will always result in seemingly "round" values becoming slightly off - which simply does not matter for most applications (including yours) as long as it happes somewhere in the 17th decimal digit. See that link also for alternatives for applications where it does matter.
您还应该确保您理解二进制浮点数的工作原理,并且它总是会导致看起来“圆”的值稍微偏离—这对大多数应用程序(包括您的应用程序)来说根本不重要,只要它位于第17位小数。还可以查看该链接,以找到对其有影响的应用程序的替代方案。
#1
13
SQLite is typeless, that means all representation is written as text, probably the wrapper api does some converts you don't know of, that you get those results.
SQLite是无类型的,这意味着所有的表示都是作为文本编写的,可能包装器api会做一些您不知道的转换,您会得到这些结果。
If you need to store the data as string do it.
如果需要以字符串形式存储数据,请这样做。
Just when you read out the double make sure you saved in the right format, you can use getDouble
on the column.
当您读取double时,请确保以正确的格式保存,您可以在列上使用getDouble。
#2
4
double
has about 17 decimal digits of precision, so if 9 digits is what you need, there should be no problem (assuming that you don't do any complex calculations on those values). Just make sure you never end up using float
, because that has only about 7 digits of precision.
double的精度大约是17位小数,所以如果需要9位数字,应该没有问题(假设您没有对这些值进行任何复杂的计算)。只要确保你永远不会使用浮点数,因为它只有7位精度。
You should also make sure you understand how binary floating-point works, and that it will always result in seemingly "round" values becoming slightly off - which simply does not matter for most applications (including yours) as long as it happes somewhere in the 17th decimal digit. See that link also for alternatives for applications where it does matter.
您还应该确保您理解二进制浮点数的工作原理,并且它总是会导致看起来“圆”的值稍微偏离—这对大多数应用程序(包括您的应用程序)来说根本不重要,只要它位于第17位小数。还可以查看该链接,以找到对其有影响的应用程序的替代方案。