• 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

Float & Double Conversion

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code prints:
true
false
9.223372036854776E18 9.223372036854776E18
2.147483647E9 2.147483648E9
System.out.println( (double) Long.MAX_VALUE == (float) Long.MAX_VALUE ); // 1
System.out.println( (double) Integer.MAX_VALUE == (float) Integer.MAX_VALUE); // 2

System.out.println( (double) Long.MAX_VALUE +" " + (double)(float) Long.MAX_VALUE );
System.out.println( (double) Integer.MAX_VALUE +" "+ (double)(float) Integer.MAX_VALUE);

What's happening on line 2?
SerdaR
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It has to do with the precision double and float types use to store the same number.
Remember double type uses 64 bits and float type uses 32 bits
 
Serdar Ozturk
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, long is 64-bit wide and there is no loss of precision when you convert it to float first and then to double... on line 1
On line 2, we convert 32-bit integer to float and then to double and we loss some precision.
Why dont we loss any precision on 64-bit long variable but on 32-bit integer?
Serdar
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value,



Why dont we loss any precision on 64-bit long variable but on 32-bit integer?


Because Long.MAX_VALUE is much bigger than Integer.MAX_VALUE and both double and float won't have any difference in the precision used to represent it. As for Interger.MAX_VALUE, converting to double will preserve more signaficant number than converting it to float and this is again due to the number of bits of each type
Hope this is clearer
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But,

is not displaying the same value, then why it displays true while comparing ?
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by smita raval:
But,

is not displaying the same value, then why it displays true while comparing ?



That's true but the difference btw the two numbers is small enough to be considered as 0.0
 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic