• 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

declaration float question!??

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
14 which three are valid declaraction of a float?
A. float foo=-1;
B. float foo=1.0;
C. float foo=42e1;
D. float foo=2.02f;
E. float foo=3.03d;
F. float foo=0x0123;
why A F is right!??? especially why A is rihgt??? B C is wrong???
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, integral values are interpreted as ints and decimal values are interpreted as doubles.
Therefore, these two are considered int values:

And ints can be automatically cast (through a widening conversion) to a float.
However, these are interpreted as doubles:

Since a double can't be explicitly cast to a float (due to loss of precision), this will cause a compiler error.
Corey
Of course, if you would have simply compiled these, the compiler would have given you the same answer. (and much faster)
[ June 24, 2002: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B C E are invalid
A D F are valid
A is valid because -1 is an int which is 32-bit size that is same size as float, so automatic cast is allowed.
B is invalid because 1.0 is considered as double? So, if I am to declare a float string, I must make sure the number fits into the range and I must add F at the end? Otherwise I'll get a double instead of float?
C is invalid because it is double? Why is it double? Does Java interpret all decimal number literals as double unless specified by appending F?
D is valid since it has a F appended. So if the F is dropped, would it be invalid as well because it is interpreted as double?
E is invalid since it has a D appened.
F is valid for same reason as A. However, I thought int and float both has same bit size, therefor the cast is a simple conversion not a widening conversion. Am I wrong?
Thanks a bunch for the question and and answers.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing to remember is that all integral types can be implicitly converted to float. The following would compile and run.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chung Huang:
...However, I thought int and float both has same bit size, therefor the cast is a simple conversion not a widening conversion...


True, they are the same size, but it is still considered a widening conversion to go from an int to a float. Therefore, this is legal:

While this is not:

The reason for this is that a float has a much wider range than an int (due to exponential notation). Therefore, any value that can fit in an int can fit in a float, but not vice versa. Take a look at this example:

Obviously, the resulting value fits in a float, but won't fit in the int.
Of course, with this, you may lose precision. Take a look at this thread for more details about loss of precision.
I hope that helps,
Corey
 
huanyu zhao
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float foo=-1;
yes it`s a widening conversion,but should it
float foo=-1f; why there is no suffix f?
and then
another question i have quite puzzle form a lot of books:
byte-[short]
---int---long---float----double
[char]
is it a widen order? why?
long is 64 bits and float is only 32 bits? why is widening conversion???
[ June 24, 2002: Message edited by: huanyu zhao ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by huanyu zhao:
float foo=-1;
yes it`s a widening conversion,but should it
float foo=-1f; why there is no suffix f?


Huh? Why would you want to put a 'f' on the end of an integral value. It wouldn't serve much purpose. The use of the trailing 'f' is to tell the compiler to refer to a decimal value as a float, not a double.


byte-[short]
---int---long---float----double
[char]
is it a widen order? why?
long is 64 bits and float is only 32 bits? why is widening conversion???


It's all about the range of the data types. The range of a float is much wider than that of a long, even though a long is twice as large. The reason for this is the fact that a float uses exponential notation. As there is no value that can be contained in a long that doesn't fall within the range of a float, it is considered a widening conversion to go from a long to a float.
Again, take a look at the thread I referred to earlier. That thread goes into pretty great detail about this matter.
Corey
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Huanyu,
float f1 = -1; // no error here
float f2 = -1.1; // error!
In this example, -1 is an int, while -1.1 is a double. Since a double is wider than a float, there's a compiler error. To solve this,
float f2 = -1.1F; // no more error!
Note that appending the 'F' on anything less than a double will be redundant.
float f3 = -1F; // not needed
That's because the int value -1 is automatically promoted to float in the assignment.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make thing clear, it depends on the range instead of the number of bits. So
float f = 100000000L; // float <-- long, OK.
A float's range can cover the entire range of a long, though with some lost of precision.
 
reply
    Bookmark Topic Watch Topic
  • New Topic