• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there any reason to ever use float over double?

 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only practical reason I can think of is when you might be creating very large amounts of floating points that don't need to be very precise. Is there anything I'm unaware of or should I just keep making everything a double?

-Zach
 
Ranch Hand
Posts: 91
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

Memory can still be a precious resource depending on the device where your code runs,
for example in the case of embedded software...
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Mano hinted at, it's a tradeoff between precision and memory. If memory is not a concern, then you don't use float.

I almost never use double either, but that's another discussion.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some old API's requiring floats, and some time ago Graphic cards often demanded floats or FloatBuffers. Whether the latter is still the case with modern GPU's I don't know.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Floats have a precision of "6 1/2" decimal digits. The "1/2" part comes because binary fractions and decimal fractions don't correspond precisely. To understand, consider the decimal value of 1 divided by 3.

Doubles have something like 14 1/2 digit precision - I don't remember the exact value, and having done most of my worrying about such things on old IBM mainframes where the floating-point format is slightly different means that I'm probably remembering wrong, but you get the picture. What it basically boils down to is 4 bytes versus 8 bytes, which means that double takes (ahem) double the memory for each value stored in RAM or persistent storage. And, typically, a double would require more time to load into the CPU or FPU registers and to operate on, but that depends on the width of the data buses and the internal structure of the floating point processor unit. So you can generally save memory and possibly time by using floats.

It's true that floats are less precise, but consider that many, many daily calculations don't even need 6 digits of precision. The old-time mechanical slide rules ran something like 3 1/2 digits* and we built roads, bridges, rockets and war machines with them for decades. Many data collection devices are less than 6 digits. I'm presently working with temperature sensors that are only 4 digits at most.

---
* Obviously slide rules aren't subject to the constraints of binary fractions. But they do use logarithmic scales, so the lower numbers have lots more space between them and thus can be read with more precision.
 
Zach Rode
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Everyone,

Thanks for the answers. There's some very useful information here. So my take away is 99% of the time memory is the biggest difference between the two. I was thinking that memory wasn't a huge issue in this day and age but the point on mobile apps definitely changes that. Tim, does the 1/2 point of precision effectively become useless in calculations? Your post was very informative! Take an imaginary cow because I don't have any real ones to give.

Again,Much appreciated everyone,
-Zach
 
Tim Holloway
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:Tim, does the 1/2 point of precision effectively become useless in calculations?



It's not so much "useless" as it is a warning. Floating-point numbers are fuzzy. Let me return to my decimal example.

If you divide 1 by 3, you get 0.333333333.....

Technically, this is "useless", because it will never precisely represent 1/3 this side of infinity.

The same applies to 1/10 in binary. 1/2 or 1/4 can be represented precisely in binary, but 1/10 cannot. However, just as "1.333" can be "close enough for Government work"*, the binary equivalent of 1/10 is likewise. It isn't exact, which is why bookkeeping systems should never use floating point (this is where the infamous "penny-shaving" fraud comes from), but it's perfectly fine for higher-level computations like sales projections.

If memory serves, the potential error has the technical name "epsilon". For best accuracy, the epsilon value should be as small as possible relative to the accuracy actually needed. For example, if your numbers are only accurate to 3-1/2 places, then trying to work with 4 digits is going be more perception than precision. Ultimately, the whole thing falls into the basic Calculus concepts of Range and Domain and computing how precise a value is going to be after all the necessary computing is done.


----
* Literally, in some cases.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:Take an imaginary cow because I don't have any real ones to give.



I gave one to Tim on your behalf. I gave you one too.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic