Pitfall 3: Don't mix floats and doubles when generating text or XML messages
While developing an order execution system for an online brokerage, I stumbled across a serious bug that incorrectly converted certain values from doubles to strings. Here is the scenario: The Website presents a stock-trade form to the user. A Java servlet processes the form and sends the trade information to the order execution server, a Java RMI server. The Java RMI server formats the message as either XML or another text format -- the common message switch (CMS) format, for example -- and passes it to one of several executing agents. One of the fields in the stock-trade message is the stock price, which is stored as a double. For certain double values, the Java platform incorrectly converts the price when formatting the order message, and the trade is rejected. Customers don't like that!
What if this was embedded software in a medical device, and the double value represented the amount of radiation administered to a patient? A low-level bug like this can be extremely dangerous.
Below is an applet that simulates the above scenario and generates two stock transaction messages. The first price formats correctly, while the second value -- 100.28 -- formats incorrectly.
Message 1...
Message 2...
I originally labeled this problem a bug, and I did find that Sun's JDC bug database reports it several times, in numbers 4030639, 4087318, 4068996, and 4169083. Unfortunately, these similar bugs are described inconsistently. Some are reported fixed, others are not even considered bugs, and one is labeled as a bug "in progress." Unfortunately for the many application developers who must generate XML messages that contain double values, this bug exists in JDK 1.1, 1.2, and 1.3. Thus I consider it a pitfall.
The following is a simple example of the bug. The problem lies in converting a float to a double, prior to converting to a String. This occurred in the brokerage software when one developer primarily used floats for decimals, but another implemented doubles. That caused the bug to surface; this crashed the executing agent's stock trading system, which received our incorrectly formatted trade message. Notice that the method genTradeMessage() uses a float to represent the price. The purpose of genTradeMessage() is to generate a simple text XML order message. In turn, genTradeMessage() calls formatStockPrice(), which takes a double as the price parameter. Here is the invocation of genTradeMessage() that fails:
String msg2 = genTradeMessage("SUNW", "BUY", 1000, 100.28f);
Notice that a float of 100.28 is passed into genTradeMessage(). Here is the code for genTradeMessage() and formatStockPrice():
public String genTradeMessage(String symbol,
String action,
int shares,
float price)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("");
pw.println("\t" + action + " ");
pw.println("\t" + symbol + " ");
pw.println("\t" + shares + " ");
pw.println("\t" + formatStockPrice(price));
pw.println("");
return sw.toString();
}
public String formatStockPrice(double d)
{
return "" + d + " ";
}
There are two workarounds to this problem. One solution is to implement only doubles or only floats in your APIs. That would mean rewriting formatStockPrice() to use a float. Here is that code:
public String formatStockPrice(float f)
{
return "" + f + " ";
}
Below is the GoodTradeMessageApplet that features the revised formatStockPrice().
Message 1...
Message 2...
Another potential workaround is to calculate using doubles, but cast to float when converting to a String. Of course, this solution is only viable if you are not losing precision on the cast.
Here is that version of formatStockPrice():
public String formatStockPrice(double d)
{
float f = (float) d;
return "" + f + " ";
}
This pitfall can also trip you up when you use JDBC. In the aforementioned order execution system, trades were stored in an SQL server database. When retrieving the day's trades to format a report, the getString() method would incorrectly format the price value -- as an 8-byte SQL float type -- from the database. Here is the JDBC call that would return the erroneous value:
String sprice = rslt.getString("price");
A run of the program produced the following results:
- Getting a connection to jdbc:odbc:CustomerTrades...
- Symbol: SUNW
- Action: BUY
- Price as String: 91.090000000000003
The solution is to use the getFloat() method in the ResultSet class to retrieve the value as a float, which then properly converts to a String. Here is the replaced code:
float price = rslt.getFloat("price");
A run of TestGetPrice2.java produces:
- Getting a connection to jdbc:odbc:CustomerTrades...
- Symbol: SUNW
- Action: BUY
- Price as Float: 91.09
No comments:
Post a Comment