CodeSOD: We All Float Down Here…
by Dan J. from The Daily WTF on (#CRE5)
When Maarten first saw the utility function below, he couldn't imagine why converting a number from float to double required a string intermediate. Fortunately, whoever wrote this code followed best practices: their comment not only explained what the code is doing, it also explained why.
/** * This method converts a float to a double. * Because the model uses doubles in calculations while the matrices holds floats. * @param number * The float value to convert to a double * @param accurate * Flag to specify whether to use an accurate conversion from float to double * or to use a less accurate conversion. Accurate means strings will be used * what may result in a bit slower performance. Not accurate means typecasting * will be used, this result in a fault since a double has a higher precision * then a float. */ double floatToDouble(float number, boolean accurate) { if (accurate) { return Double.parseDouble(Float.toString(number)); } return number; }
While Maarten appreciates the author's effort, he's not entirely convinced that comments alone can a sensible method make"
[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too!