• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Integer.MAX_VALUE

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a code

class Int
{
public static void main(String[] args)
{
a(Integer.MAX_VALUE , Integer.MAX_VALUE);
}
staticvoid a(double a , float b)
{
if( a == b)
System.out.println("equal");
else
System.out.println("not equal");
}
}

This is gives "not equal"

This is only for Integer.
But if you use Byte,Short, Long, Float it gives "equal"
Let me know what is the behind reason
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am just giving you clue not real reasons

Integer.MAX_VALUE is 2147483647
Float can hold a value as big as 3.4028235E38
Double can hold a value as big as 1.7976931348623157E308
Integer.MAX_VALUE can be held in double type as 2.147483647E9
Integer.MAX_VALUE can be held in float type as 2.14748365E9
2.147483647E9 is not equal 2.14748365E9

In the float representation why is the value rounded?
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent question, I will try to explain this one, I hope this can be done simple.

The problem relates to your widening conversion from the primitive type Integer.MAX_VALUE to your float primitive type.

In Java there are 2 different representations of floating point numbers, these are floats and doubles. floats have a single precision and contain 32 bits, doubles are considered double precision dloating point numbers and contain 64 bits.

However the internal representation of floats and doubles is different then primitives like int, short, byte, etc.

A float contains as the most significant bit the sign, followed by 8 bits which represent the exponent and 23 bits for the value or called the mantissa.
A double contains 11 bits for the exponent and 52 bits for the mantissa.

As such there is only 23 bits available to convert your int primitive type to the float, the remainder will be part of the exponenent and as such they are not equal.

This is an example from the JLS, and clearly demonstrates that even with a widening you can have a loss of precision.


If both arguments were floats, you would not have noticed this, however you were then comparing different values after the widening.

The reason Long.MAX_VALUE is equals is that going from a long to a float is considered as widening conversion, we can represent with less bits (32) a bigger number in a float. Converting from a float to a double we don't have any loss in data as we have 52 bits available.

Hopefully this is not confusing you more.
[ December 22, 2005: Message edited by: Thomas De Vos ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic