0

I know float type is A IEEE floating point, and it's not accuracy in calculation, for example, if I'd like to sum two floats 8.4 and 2.4, what I get is 10.7999999 rather than 10.8. I also know BigDecimal can solve this problem, but BigDecimal is much slower than float type.

In most real productions we'd like an accuracy value like above 10.8 not a 10.7999.. so my question is shall I prevent to use float as much as I can in programming? if not is there any use cases? I mean in a real production.

Saorikido
  • 2,173
  • 2
  • 26
  • 37
  • Possible duplicate of [Double vs. BigDecimal?](http://stackoverflow.com/questions/3413448/double-vs-bigdecimal) – Jerry Chin Dec 23 '16 at 07:48
  • You should not prevent to use float number because of memory limit in programming expect you can use floor and ceil to get your correct answer and if you use BigDecimal it will slow your program if you get you correct answer. – Sahadev Dec 23 '16 at 07:48
  • Round up your answer to 1dp or 2dp if you want accuracy – denvercoder9 Dec 23 '16 at 07:50
  • 1
    Premature optimization is evil. If Yoy made computation from external data, IO has bigger cost than simple computation (adding???) – Jacek Cz Dec 23 '16 at 08:10

4 Answers4

3

If you're handling monetary amounts, then numbers like 8.4 and 2.4 are exact values, and you'll want to use BigDecimal for those. However, if you're doing a physics calculation where you're dealing with measurements, the values 8.4 and 2.4 aren't going to be exact anyway, since measurements aren't exact. That's a use case where using double is better. Also, a scientific calculation could involve things like square roots, trigonometric functions, logarithms, etc., and those can be done only using IEEE floats. Calculations involving money don't normally involve those kinds of functions.

By the way, there's very little reason to ever use the float type; stick with double.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • I'm not handling monetary amounts but goods quantity in KG, which is as important as monetary stuffs. so in my case do I have to change to BigDecimal or stick with your suggested `double` then rounded up 2 decimal places at the end? – Saorikido Dec 23 '16 at 08:02
  • 1
    Rounding floating point values is BAD decision. Hides problem but not resolves. – Jacek Cz Dec 23 '16 at 08:03
  • 2
    "Important" isn't the issue; "precise" is the issue. If you're measuring quantity in Kg, your values won't be infinitely precise. So even if you use `BigDecimal`, you have to realize that your results are going to be accurate only to a certain number of decimal places, based on how precise the measurements are. The result you get with `double` may appear to be off in the 15th decimal place, but an error of 10^(-15) is still smaller than the possible error you're going to get due to the limitations of measurements. – ajb Dec 23 '16 at 08:10
  • @JacekCz That's not correct, unless you're dealing with exact amounts like money. Anyone who's studied science knows that the numbers they work with are all accurate only to a certain number of decimal places, and when you show a result, you should show it rounded to that number of decimal places--any further decimal places you display past that accuracy are meaningless garbage. – ajb Dec 23 '16 at 08:12
  • I understand now what is "KG", guess acronym from english world bookkeeping etc like GL, HR , Weight we measure with kG or kg. "k" is always small letter – Jacek Cz Dec 23 '16 at 08:12
  • @abj add long list of doubles (few thousand) , rounding after etc ... Final result will be VERY interresant – Jacek Cz Dec 23 '16 at 08:14
  • @JacekCz I'm not sure what you're saying, but if you have a few thousand `BigDecimals` that are accurate to, say, only 3 decimal places, then when you add them, the potential error will be a lot more than it will be just for one number. I'm not familiar with the math behind this, and I'm sure it's _very_ complicated. But I seriously doubt that you will do any better with `BigDecimal` than with `double`. We probably need a math Ph.D. to figure this out for us. – ajb Dec 23 '16 at 08:21
  • @ajb Did You observe f.e chief of warehouse? Long list of positions in kg / value must arithmetically match (never can be guaranteed with floating point). Totally un-important is Ph.D opinion that elementary position is in-acurate – Jacek Cz Dec 23 '16 at 08:58
1

You use float when the percision is enough. It is generally faster to do calculations with float and requires less memory. Sometimes you just need the performance.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
1

What you describe is caused by the fact that binary floating point numbers cannot exactly represent many numbers that can be exactly represented by decimal floating point numbers, like 8.4 or 2.4.

This affects not only the float type in Java but also double.

In many cases you can do calculations with integers and then rescale to get the deciamls correctly. But if you require numbers with equal relative accurracies, no matter how large they are, floating point is far superior.

So yes, if you can, you should prefer integers over floats, but there are many applications where floating point is required. This includes many scientific and mathematical algorithms.

You should also consider that 10.7999999 instead of 10.8 looks weird when displayed but actually the difference is really small. So it's not so much an accurracy issue but more related to number formatting. In most cases this problem is resolved by rounding the number appropriately when converting it to a string for output, for example:

String price = String.format("%.2f", floatPrice);
Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • Then what's suggestion with my case, I'm handling some quantity (KG) calculation, it's very sensitive of it's quantity coz the price is expensive. Use BigDecimal instead? – Saorikido Dec 23 '16 at 08:05
  • @Neeson.Z: No, you probably won't need BigDecimal here. Do the calculation with `double` and then round the result appropriately, for example: `String price = String.format("%.2f", floatPrice);` – Frank Puffer Dec 23 '16 at 08:09
0

BigDecimals are very precise (you can determine their precision -- it is mainly limited by memory) but pretty slow and memory intensive. You use them when you need exact results, i.e. in financial applications, or when you otherwise need very precise results and when speed is not too critical.

Floating point types (double and float) are not nearly as precise, but much faster and they only take up limited memory. Typically, a float takes up 4 bytes and a double takes up 8 bytes. You use them with measurements that can't be very exact anyway, but also if you need the speed or the memory. I use them for (real time) graphics and real time music. Or when otherwise precision of the result is not so important, e.g. when measuring time or percentages when downloading or some such.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94