1

Let's say I have variables named pi (float) & pi2 (double). I assigned the value of (float)3.14159 to pi and it is 3.14159 exactly.

    float pi;
    pi = (float)3.14159;
    System.out.println(pi);   //3.14159

I typecasted the pi (float) variable into a double and assign the value of pi2 (double) to it. However I get a value that's not exactly 3.14159. I get 3.141590118408203 and it has more significant digits than 3.14159 however it's less precise than 3.14159 because it is farther from 3.14159. On the other hand, if I directly assign pi2 = 3.14159, it is exactly 3.14159.

    double pi2 = (double)pi;
    System.out.println(pi2);   //3.141590118408203
    pi2 = 3.14159;
    System.out.println(pi2);   //3.14159

Why is it that when I change a double into a float and changed it back to double again I get unwanted imprecisions?

The answer to this question isn't clear cut to me: "When you convert a float into a double, there is no loss of information. Every float can be represented exactly as a double." Why is it that when we convert from a float to a double, there are extra non-zero significant digits which lessens the precision rather than adding to it? If what he said is true that no loss of information from float to double, I should be getting 3.141590000000000 and not 3.141590118408203.

Batibot323
  • 21
  • 3
  • 1
    If you want a `double` variable to have the value of PI, you should use [`Math.PI`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#PI) to get the more accurate value of `3.14159265358979323846`. – Andreas Aug 21 '17 at 05:23
  • This [link](https://stackoverflow.com/questions/916081/convert-float-to-double-without-losing-precision) – Bui Anh Tuan Aug 21 '17 at 05:29
  • I'm not messing with strings, I'm just converting from float to double and vice-versa. I assigned a float variable to be 3.14159 and it has enough bits to precisely show 3.14159. If anything, double is 'less precise' because it cannot show that it's 3.141590000000 instead it adds a bunch of seemingly random numbers. – Batibot323 Aug 21 '17 at 05:51
  • 2
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Hulk Aug 21 '17 at 05:54
  • 2
    You never had 3.14159 in the first place. It isn't represented accuratey by either a `float` or a `double`. – user207421 Aug 21 '17 at 05:57
  • That's the fundamental problem -- the standard Java floating point math code doesn't use decimal values. So when you store "3.14159" in a float or a double, you get an approximation of that decimal value in the specific binary representation that the math code uses. Unfortunately, that approximation is different in float and double, which is why even values that appear -- in decimal -- to have fewer significant digits that the floating point representation can store still cannot be converted from one form of representation to another. – Kevin Boone Aug 21 '17 at 12:10

0 Answers0