Saturday, 31 August 2013

float, double in JSon fail test

float, double in JSon fail test

In test file:
float accelerationX = -10.01f;
float accelerationY = -20.02f;
float accelerationZ = -30.03f;
in setUp I am creating a myObject and I am passing those values
in test1function: -this are passed ( retrieved as float)
assertEquals(-10.01f, myObject.getAccelerationX());
assertEquals(-20.02f, myObject.getAccelerationY());
assertEquals(-30.03f, myObject.getAccelerationZ());
this will fail:
public void testJSON(){
JSONObject json = myObject.getJSON();
try {
assertEquals(-10.01, json.getDouble("x"));
assertEquals(-20.02, json.getDouble("y"));
assertEquals(-30.03, json.getDouble("z"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
fail();
}
}
The message is:
junit.framework.AssertionFailedError: expected:<-10.01> but
was:<-10.010000228881836>
targeted function part:
public JSONObject getJSON() {
JSONObject r = new JSONObject();
try {
...
// acceleration data:
r.put("x", (double)this.accelerationX);
r.put("y", (double)this.accelerationY);
r.put("z", (double)this.accelerationZ);
First time it was without casting to double, same error.
What is the problem here? why is converted the -10.01 float to
-10.010000228881836 even after casting to double? Must use a number
formatter?

No comments:

Post a Comment