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.