• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

strange conversion??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried running the program and I got the output : -46.
Why such a strange answer??
Sonir
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sonir shah:

I tried running the program and I got the output : -46.
Why such a strange answer??
Sonir


Do you understand what this program is showing? It's showing that there is a slight loss of precision in the automatic widening conversion from an int to a float. When you convert this int number to a float, and convert it back to an int, the converted value is 46 less than it was previously...this is the magnitude of the precision loss.
Rob
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: just to clarify. it should be loss during narrowing conversion. right ?
(coz you stated during widening conversion).....

Originally posted by Rob Ross:

I tried running the program and I got the output : -46.
Why such a strange answer??
Sonir<hr></blockquote>
Do you understand what this program is showing? It's showing that there is a slight loss of precision in the automatic widening conversion from an int to a float. When you convert this int number to a float, and convert it back to an int, the converted value is 46 less than it was previously...this is the magnitude of the precision loss.
Rob[/QB]

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe, I can understand your confusion Mark, but I meant what I said
public class Conversion{
public static void main(String[] args){
int i = 1234567890;
float f = i;
System.out.println(i - (int)f);
}
}
the float f = i is an implicit widening conversion, even though it causes a loss of precision!
The idea behind it is that it is ok to loose a little precision as long as the overall magnitude of the number is not lost.
Rob
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Rob,
From where did this 46 come from?? Why only 46??
How can we come to know?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is just the difference in precision between the representation of the integer number 1234567890 as an int vs a float value. This is a fact of life in dealing with binary representation of real numbers.
There's really no other way to explain this...anyone else care to try??
Rob
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case:
5.1.2 Widening Primitive Conversion


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, using IEEE 754 round-to-nearest mode (�4.2.4).
A widening conversion of a signed integer value to an integral type T simply sign-extends the two's-complement representation of the integer value to fill the wider format. A widening
conversion of a character to an integral type T zero-extends the representation of the character value to fill the wider format.
Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a run-time exception (�11).


HIH
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know what you mean. but there is more than what meets the eye.
let's simplify the code to make sense
int ss = 10000890;
float f = ss;
System.out.println((int)f);
fine. now it really makes sense of what we are talking about. "PRECISION"
here as above it is, there is no loss. BUT make the number 100000890 (added another zero). yes NOW THERE IS A LOSS. output is ...888 loss of 2
so after some quantity or magnitude the loss appears.
Rob, but your point of precision loss is well taken, the only thing is that after certain magnitude the loss sets in.

Originally posted by Rob Ross:
Hehe, I can understand your confusion Mark, but I meant what I said
public class Conversion{
public static void main(String[] args){
int i = 1234567890;
float f = i;
System.out.println(i - (int)f);
}
}
the float f = i is an implicit widening conversion, even though it causes a loss of precision!
The idea behind it is that it is ok to loose a little precision as long as the overall magnitude of the number is not lost.
Rob

 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an int one bit represents the sign, leaving 31 bits for the value of the int.
In IEEE754 32 bit format, again one bit represents the sign, but there are also 8 bits used to represent the size (scale) of the number. This leaves 23 bits for the value.
Therefore if the original value is larger than 23 bits there will be a few bits truncated from the end
 
You didn't tell me he was so big. Unlike this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic