Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unexpected Java Float Precision Changes

DZone's Guide to

Unexpected Java Float Precision Changes

· Performance Zone
Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

I ran across a little gotcha today where a float value being inserted into another object container (JSONObject) was not holding the precision of the original value. The JSONObject actually takes a double not a float, and I overlooked the up-casting initially until I started unit testing. (have to love unit tests!)

here is a print of the values I started with:

07:46:15.108 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - ===== abv:: calculateAbv =====
07:46:15.117 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - volume: 16
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - abv: 3.2
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - price: 5.99
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - value_r: 0.117
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - score_r: 12.0

Here is how I was creating my JSONObject:

JSONObject json = new JSONObject();
json.put("abv_r", valueObject.getAbv());
json.put("volume_r", valueObject.getVolume());
json.put("price_r", valueObject.getPrice());
json.put("value_r", valueObject.getValue());
json.put("score", valueObject.getScore());
result = json.toString();

result:
{“abv_r”:3.200000047683716,”volume_r”:16,”price_r”:5.989999771118164,”value_r”:0.11699999868869781,”score”:12}

The JSONObject.put method does not take a float, but takes a double:

/**
* Put a key/double pair in the JSONObject.
*
* @param key A key string.
* @param value A double which is the value.
* @return this.
* @throws JSONException If the key is null or if the number is invalid.
*/
public JSONObject put(String key, double value)
throws JSONException {
    put(key, new Double(value));
    return this;
}

@see org.codehaus.jettison.json.JSONObject

Now when I cast the values to String’s such as

“”+valueObject.getAbv()

The precision of the values are not cast to doubles:

JSONObject json = new JSONObject();
json.put("abv_r", ""+valueObject.getAbv());
json.put("volume_r", ""+valueObject.getVolume());
json.put("price_r", ""+valueObject.getPrice());
json.put("value_r", ""+valueObject.getValue());
json.put("score", ""+valueObject.getScore());
result = json.toString();

result:

{“abv_r”:”3.2“,”volume_r”:”16″,”price_r”:”5.99“,”value_r”:”0.117“,”score”:”12.0“}

Conclusion

So care needs to be taken to remember other Objects can cast your objects to have undesirable affects.

Another solution is to ensure the valueObject uses double’s then no casting is performed when putting the values into the JSONObject.

 

 

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Mick Knutson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}