# Regarding Assignment

boopathy Gopalsamy
Greenhorn
Posts: 5
The following code compiles and prints "true"
float f=Long.MAX_VALUE;
double d=Long.MAX_VALUE;
System.out.println(f==d);

But this code compiles and prints "false"
float f=Integer.MAX_VALUE;
double d=Integer.MAX_VALUE;
System.out.println(f==d);

Explain me the difference and why?

vidya sagar
Ranch Hand
Posts: 580
Hi boopathy

float f=Integer.MAX_VALUE;
double d=Integer.MAX_VALUE;
System.out.println(f==d);

This is true because float and double container can accomdate the max value of integer as it is.

But in this case

float f=Long.MAX_VALUE;
double d=Long.MAX_VALUE;
System.out.println(f==d);

since Double is big container than float,Float cannot stores as Double stores

This is way the float stores 2.14748365E9
This is way the long stores 2.147483647E9

SO IT IS FALSE

I hope now ur clear

Mathangi Shankar
Ranch Hand
Posts: 56
Hi Sagar,

I think the explaination you have is viceversa. Please check. Correct me if I am wrong.

Mathangi.

vidya sagar
Ranch Hand
Posts: 580
Hi mathangi

where i am wrong..........?i cannot anything wrong in my above statement...

Joe Sondow
Ranch Hand
Posts: 195
vidya sagar, you are mistaken. Compile and run the code originally posted and you will see that boopathy Gopalsamy's claim is correct: the comparison involving Long.MAX_VALUE yields true, and the comparison involving Integer.MAX_VALUE yields false.

The reason one equality test returns true and the other returns false is that comparing floating-point numbers often has results that are difficult to predict. In this case, you're comparing a float and a double, so the compiler promotes the float to a double in order to do the comparison, because the compiler needs the two values to be the same type before comparing them. Here's a program that illustrates how this is happening:

The output is:

true
true
9.223372036854776E18
9.223372036854776E18
false
false
2.147483648E9
2.147483647E9

When f1 is cast to a double, it happens to have the same exact value as d1, so f1==d1 returns true. However, when f2 is cast to a double, it does not have exactly the same value as d2, so f2==d2 returns false. I don't think there is any way to predict the output without using a compiler, unless you happen to be an expert on floating-point bit patterns.

The moral: don't count on computer-based floating-point variables to be exactly equal to any values, even when pure mathematics suggests that they should be.

amit taneja
Ranch Hand
Posts: 813
Joe Sanowitz
pls tell the reason why floating value is chaning on casting to double even when it can hold the interger.max value... ?
and why its not changing in double max value ?

ok ...i have one more question above all ...that generally
as both float and integer are 32 bits then how integer can be implicitly be casted to floating point ..if all 32 bits are utilized and in that case where the value after decimal will be stored ?
i think interger don't have decimal value so float will always have .00 ( i.e zero values after decimal after decimal ?

is i m rt ?

Yingtao Jiang
Greenhorn
Posts: 13
Joe Sanowitz
pls tell the reason why floating value is chaning on casting to double even when it can hold the interger.max value... ?

Yes, float value can "hold" the integer.max value. Which only means the integer.max value fall into the "range" of the number a float can represents. BUT: It is impossible for float to PRECISELY represents all the numbers fall within its range. That is simply because there is infinite float numbers fall within a un-empty range and the bit-depth of float is limited.