OSDN Git Service

fix [4026375] SensorManager quaternion functions can call sqrt with a negative number...
authorMathias Agopian <mathias@google.com>
Tue, 8 Mar 2011 01:49:54 +0000 (17:49 -0800)
committerMathias Agopian <mathias@google.com>
Tue, 8 Mar 2011 01:58:34 +0000 (17:58 -0800)
Just make sure to clamp the argument to zero.

Bug: 4026375
Change-Id: Idd3d4ef977e87c1b3f6b54371105c3152d7dc6b9

core/java/android/hardware/SensorManager.java

index f079e42..cc82170 100644 (file)
@@ -2026,8 +2026,8 @@ public class SensorManager
         if (rv.length == 4) {
             Q[0] = rv[3];
         } else {
-            //In this case, the w component of the quaternion is known to be a positive number
-            Q[0] = (float)Math.sqrt(1 - rv[0]*rv[0] - rv[1]*rv[1] - rv[2]*rv[2]);
+            Q[0] = 1 - rv[0]*rv[0] - rv[1]*rv[1] - rv[2]*rv[2];
+            Q[0] = (Q[0] > 0) ? (float)Math.sqrt(Q[0]) : 0;
         }
         Q[1] = rv[0];
         Q[2] = rv[1];