Article CRE5 CodeSOD: We All Float Down Here…

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.

weallfloat.jpg

/** * 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"

inedo50.png[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! TheDailyWtf?d=yIl2AUoC8zAlk7BEP-8rcA
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments