• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

int to float conversion !

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I know these are widening primitive conversions:
byte ->to short, int, long, float, or double
short ->to int, long, float, or double
char ->to int, long, float, or double
int ->to long, float, or double
long ->to float or double
float ->to double
I have tried :
class Testing {
public static void main(String[] args) {
int i = 1234567899;
float j = i;
System.out.println(i-(int)j);
}
}
Guess what : The output is -37
How this is happening ?
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is your code with a couple of modifications:
class Testing {
public static void main(String[] args) {
int i = 1234567899;
float j = i;
System.out.println(i-j);
System.out.println(i);
System.out.println((int)j);
}
}
Notice what happens when you NARROW the conversion from float back to int.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 1234567899;
float j = i; //float will convert the value 1234567940 0r 1.23456794E9
System.out.println(j);
// (int) j; this will convert the 1234567940 into 1234567930
// finally the result will be 12345678899 - 1234567930 = -31(approximately)
Basically the compiler will not convert the accurate value from float
to int because the this is explicit narrowing conversion evan though the
value is in the range.
that why you will get the unexpected result.


------------------
Iftikhar
 
M Iftikhar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The voice conversation with Impulse has ended.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider that
System.out.println((float)1234567899);
prints
1.23456794E9
Note how the integer looses the last digits (precision) . This is because an integer can store numbers within a 32 bits range. While a float has only 24 bits for the mantisa.
 
Salamina Daniel
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers.
From what I saw(by coding ) I conclude that is ok to say:
Widening conversion can result in loss of precision if an int or a long value is converted to float, or a long value to double !!
Anyone disagree ?
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic