3

When I try to parse the following string into a float and into a double :

String abc = "8.40";
System.out.println("Double Value: " + Double.parseDouble(abc) * 100);
System.out.println("Float Value: " + Float.parseFloat(abc) * 100);

I get two different results.

Double Value: 840.0
Float Value: 839.99994

But when I try the same code with multiplying the float and double by 10 or 1000 I get the similar results for both of them.

String abc = "8.40";
System.out.println("Double Value: " + Double.parseDouble(abc) * 10);
System.out.println("Float Value: " + Float.parseFloat(abc) * 10);

I get two similar results.

Double Value: 84.0
Float Value: 84.0

And when I try this :

String abc = "8.40";
System.out.println("Double Value: " + Double.parseDouble(abc) * 1000);
System.out.println("Float Value: " + Float.parseFloat(abc) * 1000);

I get two similar results.

Double Value: 8400.0
Float Value: 8400.0
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Vijay
  • 558
  • 6
  • 22

2 Answers2

4

This will work fine:

System.out.println("Float Value: "+Math.round((float)Float.parseFloat(abc)*100));

So, this happens because of different representation of double and float, or more precise, about IEEE-754 rounding for float. Read about it here.

float has a smaller range and precision, so double would be better when you have memory (which you do today). But, they are both evil! There is a better option in Java called BigDecimal and you should use it, since it doesn't have problem with size and today we have strong computers so we will not have problems with memory and speed when dealing with a large number of decimal numbers needing max precision. For example, if you work on software that deals with a lot of money transactions, its a must to use BigDecimal.

Adnan Isajbegovic
  • 2,227
  • 17
  • 27
  • BigDecimal, float, and double all have good uses. I've seen situations in which each is a much better choice than either of the other two. Always using BigDecimal is just as serious a mistake as always using one of the other two. – Patricia Shanahan May 19 '16 at 14:59
  • @PatriciaShanahan Only usage today I can think of is some micro processors with some fast sensors, so BigDecimal processing would take too much time because of non parallel processing and slow CPU..most SE and EE based projects would handle large amount of BigDecimals in less then 1ms, so should not make problems with speed..Even memory should be OK, since for large EE based projects, you can afford enough RAM and Java 8 handles it great (worked with one such SE based project, handled perfect cache size of 30+GB and I always used Integer, Long, Double, BigDecimal, etc. - Used Long even as Ids) – Adnan Isajbegovic May 19 '16 at 16:12
  • You seem to be thinking in terms of taking problems sizes that could be solved on older computers, and assuming that is all that current computers need to run. I'm just going to take one field. Many years ago I saw a presentation by a computational chemist. He had a chart with logarithmic scales of floating point operations per second on one axis, memory size on the other, filled in with problems he would work on given those resources. The chart extended orders of magnitude beyond current performance and memory. – Patricia Shanahan May 19 '16 at 16:26
  • Do you know a good trig etc. library for BigDecimal? That would extend its usefulness quite a bit. – Patricia Shanahan May 19 '16 at 16:27
  • apache common-langs should have some things for it... google guava has MathUtils so it should also deal with it... But, its always better to create your own stuff - better control and you can avoid having to much checkups and abstractions... – Adnan Isajbegovic May 19 '16 at 16:33
  • I tried looking up guava mathutils, but could not find out how to use it to calculate e.g. the sine or arctangent of a BigDecimal. Could you give a more specific pointer? I have been involved in writing trig library code for a vector processor. I am surprised you would suggest roll-your-own for something like that. I happen to have the mathematical background needed, but I don't think that is the case for most readers here. – Patricia Shanahan May 19 '16 at 16:45
  • Well, those are the libraries that i know that have some mathematical functions, but never used them as such, just some structures from them. Well, if you don't need to much precision, java Math class is fine.. But for super high precision, I couldn't find any library (at least not in google first few sugestions), but what I would recommend here, as mathematician and as java backend enteprise developer, is to use Wolfram Alpha Java Binding library so you can use full potential of Wolfram software, which is the best option out there. – Adnan Isajbegovic May 19 '16 at 17:00
2

It is true that double has more precision than float, but both of them suffer from the same problem: their value may not be exact, and they both have some (small) rounding error in their Least Significant Bit (LSB). This is clear in the first result you got: float value is not accurate. But when you multiply by 10 or 1000, the LSB is discarded from the result, and so you get the right answer for both float and double.

AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35