I am new to java -could you please tell me why output appearing like this?????
Below is the program i wrote.
class Value
{
public static void main(String [] args)
{
float v=2.234567f;
System.out.println("the valu of v="+v);
}
}
1) When i give the valu of v=2.234567f provide output as "the valu of v=2.234567"
2)When i give the value of v=2.2345678f it provides output as"the valu of v=2.2345679" [ i thought the ouput will 2.2345678]
3)When i give the value of v=2.2345679f it provides output as "the valu of v=2.2345679"[my doubt is for the value of v in step 2 and the value of v in step 3 gives the same output why??].
Thanks in advance for clarifying and looking into my doubt.
Regards,
purni.
Re: I am new to java -could you please tell me why output appearing like this?????
Google: "what every computer scientist should know about floating point arithmetic".
The gist: computers have trouble representing exact decimals in binary. You're seeing the expected behavior.