java - Loss of precision when converting from float to double? -


let's have variables named pi (float) & pi2 (double). assigned value of (float)3.14159 pi , 3.14159 exactly.

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

i typecasted pi (float) variable double , assign value of pi2 (double) it. value that's not 3.14159. 3.141590118408203 , has more significant digits 3.14159 it's less precise 3.14159 because farther 3.14159. on other hand, if directly assign pi2 = 3.14159, 3.14159.

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

why when change double float , changed double again unwanted imprecisions?

the answer this question isn't clear cut me: "when convert float double, there no loss of information. every float can represented double." why when convert float double, there non-zero significant digits lessens precision rather adding it? if said true no loss of information float double, should getting 3.141590000000000 , not 3.141590118408203.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -