Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

assign float to int ??

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I guess this is a really stupid question, but here it goes..
Consider these:
float f = 345; (1)
int i = f; (2) //loss of precision
int and float are both 32 bits. They why is (1) valid, and (2) causes loss of precision?
Thanks,
Cathy.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since an int does not have a decimal portion, moving a float to an int causes a loss of precision. Also a float can hold a much larger number than an int even though they both have 32 bits. See the JavaRanch newsletter for an article on why a float can store a larger number than an int.
http://www.javaranch.com/newsletter/July2003/newsletterjuly2003.jsp#a4
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The set of float values is different from the set of int values.
Here is the format of a 32-bit float value:

A float can hold whole numbers (345) or fractions (0.12345, 345.12345).
The largest float is about 3 * 10^38, where as the largest int is only about 2 billion (2 * 10^9).
If you try to convert a float value to an int value, you might loose a fraction (precision) or the number might be too large (magnitude).
----
Although the range of positive float values is so much larger (10^-45 to 10^38) than an int (1 to 10^9), a float cannot hold every 32-bit integer. (A double can.)
A float cannot hold the integer 16777217, because the significand is 24 bits.
16777217 ==
binary 1000000000000000000000001 ==
binary 1.000000000000000000000001 * 2^24
So, you see, it is not a stupid question, after all.
[ September 08, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It�s my private opinion (i.e. don�t believe me) that the compiler error text �loss of precision� is a bad choice of words.
int i = 1;
byte b = i; // loss of precision? high-order bits are discarded
short s = 1;
char c = s; // loss of precision? no negative values
float f = 1f;
int i = f; // loss of precision? or loss magnitude
Here is a graphic description of the widening primitive conversions.
byte -> short -> int -> long -> float -> double
char -> int
The narrowing conversions go in the other direction. All the narrowing conversions without cast result in a compiler error. They are not necessarily loss of precision.
Instead, some values of one set cannot be represented in the other set.
[ September 08, 2003: Message edited by: Marlene Miller ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene and Thomas.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marlene Miller:
It�s my private opinion (i.e. don�t believe me) that the compiler error text �loss of precision� is a bad choice of words.


I think loss of "significance" would be a better choice. When we move large ints to a float we will have loss of precision but no loss of significance since the assumption is that low order digits are not significant in floats.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS uses "loss of information" without defining or clarifying what they mean. I once saw a post asking what "lost of information" means on the Sun Java Advanced Features forum. The opinions and debate went on for pages and pages and days and days.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think loss of information is even worse than loss of precision. "Significance" makes more sense. When we lose some digits in moving from an int to a float, the assumtion is that the loss is not significant since we know that floats do not accurately hold numbers.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. loss of information is almost useless. loss of significance is the best. It describes what is happening. I've never heard that one before.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think loss of "significance" would be a better choice. When we move large ints to a float we will have loss of precision but no loss of significance since the assumption is that low order digits are not significant in floats.
Not a bad term there. The only problem is that it's not widely used already. "Loss of precision" may not be great terminology, but it's in common usage in the numeric computing community. I also disagree a bit later - in casting int to float there's often no "loss of significance", but there certainly may be; we don't know how many digits really are significant.
Or putting it another way - to me, "significance" refers to what the value means. If a variable x represents a number of apples, and then someone says it "lost its significance", to me that implies that it no longer refers to a number of apples. (Perhaps because someone inadvisedly added the number of oranges to it, for example.) While Tom's new usage of "significance" may be clever, it doesn't match up well with existing usage, IMO.
Perhaps we could say:
int -> float : "Possible loss of least significant bits."
float -> int : "Possible loss of most significant bits."
I don't see what's so bad about "loss of information". It's perfectly accurate, and it at least draws attention to the problem. You just have to decide for yourself what it means in a given context. But that's true for most error messages after all. :roll:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic