EricLarch
EricLarch

Reputation: 5773

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. 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

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...

I cannot reproduce the problem, the crash comes from Android Market console and I do not have all the context. 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.

Do someone has a clue of how a double can generate a "*" when converted to String?

Upvotes: 0

Views: 296

Answers (2)

EricLarch
EricLarch

Reputation: 5773

After having planted some reporting inside my APK, I found the origin of the problem.

On the ZTE StarAddict, the result of:

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. 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.

Upvotes: 0

Montolide
Montolide

Reputation: 792

I'm not sure why, but there is a missing "(" just before the coslng, can you check?

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.

Not sure how this will help, but its a start :)

Upvotes: 2

Related Questions