You are hereBlogs / David Grant's blog / Java float vs. double and micro-optimization

Java float vs. double and micro-optimization

By David Grant - Posted on 15 December 2006

Some people at work implemented a few classes all with floats and I am sick and tired of casting everything into float. There is absolutely no reason why floats should have ever been used. The original reason was because of memory and/or network and/or diskspace issues which is all now totally irrelevant because the project that these classes were mostly used for has now been scrapped. I recently ripped out a few classes from that project, however, because they are useful. I am finding them extremely annoying to use, however. Not only do they use floats but they are totally micro-optimized in other ways as well. I'm probably rewrite the class soon anyways so that it is readable. My opinion is that there is no reason to ever use anything other than ints, doubles, and bytes in Java. At leas in 2 years I have never come across any reason. Part of this is Java's fault for having two floating point types in the first place. I would have been much happier had this all been developed in Python.


Though quite old now, it was a pretty useless rant at the time and it still is now, if you ask me.

It comes down to "Don't use floats because I don't like them", which can hardly be considered a reasonable argument. Instead, if you want to try to confince people, mention what the difference between floats and doubles is, why floats where actually used in the past, why this is different now, why using floats is now a problem and why doubles would now be more appropiate. No concrete examples where given, just that some project used to have 'memory and/or network and/or diskspace issues' when not using floats. We don't know anything about that project.

I haven't heard of any performance hit for using double, and if it's there, I'm guessing that it's so small as to verge on micro-optimization. I would stick with double unless you know with hard evidence that float would work better.
vincere il Jackpot

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <s> <img> <h2> <h3>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.