简体   繁体   English

在Java中将double转换为String时的奇怪行为

[英]Strange behavior when converting a double to String in Java

I have a crash in my Android application coming from a strange behavior in a SQL query string construction. 我的Android应用程序崩溃来自SQL查询字符串构造中的奇怪行为。 Here is the error: 这是错误:

Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: , while
compiling: SELECT [...], (0.6773931613745379*coslat*(coslng*0.9999276712889913
+sinlng*0.012027143*)+0.7356211694364222*sinlat) [...]

The errors comes precisely from here: sinlng*0.012027143*) <== *) at the end 这些错误恰恰来自于: sinlng*0.012027143*) <== *)

This string is builded by the following function: 该字符串由以下函数构建:

public static String buildDistanceQuery(double latitude, double longitude) {
    final double coslat = Math.cos(MathUtils.deg2rad(latitude));
    final double sinlat = Math.sin(MathUtils.deg2rad(latitude));
    final double coslng = Math.cos(MathUtils.deg2rad(longitude));
    final double sinlng = Math.sin(MathUtils.deg2rad(longitude));
    return "(" + coslat + "*" + LocationColumns.COSLAT
            + "*(" + LocationColumns.COSLNG + "*" + coslng
            + "+" + LocationColumns.SINLNG + "*" + sinlng
            + ")+" + sinlat + "*" + LocationColumns.SINLAT 
            + ")";
}

As you can see, LocationColumns.SINLNG + "*" + sinlng + ")" becomes sinlng*0.012027143*) and I really don't see how this could be possible as sinlng is a double... 正如你所看到的, LocationColumns.SINLNG + "*" + sinlng + ")"变成了sinlng*0.012027143*)我真的不知道这是怎么可能的,因为sinlng是双重...

I cannot reproduce the problem, the crash comes from Android Market console and I do not have all the context. 我无法重现问题,崩溃来自Android Market控制台,我没有所有上下文。 It is not a unique crash, I got multiple occurences. 这不是一个独特的崩溃,我有多次出现。

I can try to use a StringBuilder() but I'm not sure it will correct the issue. 我可以尝试使用StringBuilder(),但我不确定它是否会纠正这个问题。

Do someone has a clue of how a double can generate a "*" when converted to String? 有人知道double在转换为String时如何生成“*”吗?

I'm not sure why, but there is a missing "(" just before the coslng , can you check? 我不知道为什么,但是有一个缺失的“(”就在coslng之前,你能查一下吗?

EDIT: Thanks for fixing it, there is a way to just print or inspect the String made, to check if the * already appears there, or just after the Query is built? 编辑:感谢您修复它,有一种方法可以打印或检查生成的字符串,检查*是否已经出现在那里,或者只是在构建查询之后? and maybe inspecting the double sinlng to se if it matches 0.012027143. 也许检查双sinlng瑟是否匹配0.012027143。

Not sure how this will help, but its a start :) 不知道这会有什么帮助,但它是一个开始:)

After having planted some reporting inside my APK, I found the origin of the problem. 在我的APK中设置了一些报告之后,我找到了问题的根源。

On the ZTE StarAddict, the result of: 在中兴StarAddict上,结果如下:

final double sinlat = Math.sin(MathUtils.deg2rad(47.3572329));

is

0.0121*

I know this doesn't make sense, but this is the way it is. 我知道这没有意义,但这就是它的方式。 I guess this device is really flawed because other strings are being "translated": 我猜这个设备真的有缺陷,因为其他字符串被“翻译”:

final static String QUERY = "distance > ?";

becomes

"distance &gt; ?"

when used into the data provider. 用于数据提供者时。

I made some research on Google, but couldn't find anything specific. 我在谷歌上做了一些研究,但找不到具体的东西。

For information, my application is active on 100K+ devices and only the ZTE StarAddict behaves like that. 有关信息,我的应用程序在100K +设备上处于活动状态,只有ZTE StarAddict的行为类似。 We can therefore exclude a mistake in my code. 因此,我们可以排除代码中的错误。

If anyone has had the same problem than me, I'd be happy to hear for some clues. 如果有人遇到与我相同的问题,我会很高兴听到一些线索。

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

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