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 or double

Ranch Hand
Posts: 103
• Number of slices to send:
Optional 'thank-you' note:
This is a RHE question.
In the code fragment below, what are the legal data types for the variable "answer"?
byte b = 1;
char c = 2;
short s = 3;
int i = 4;
float f = 5f;
answer = b * c * s * i * f;

Correct selection is: FLOAT OR DOUBLE
The result of the multiple arithmetic is either int or the widest of the operand types, whichever is wider. Here the widest operand type is float, which is wider than int, so the result type is float. The variable answer may be float or any type that is wider than float. The only type that is wider than float is double.
Problem: well, I choose float only because float is wider. I don�t think double is ccorect
But they have given double.

Author and all-around good cowpoke
Posts: 13078
6
• Number of slices to send:
Optional 'thank-you' note:
I think they say float or double because double would also work. The result of the expression would be float but that can be cast to double.
Bill

Ranch Hand
Posts: 103
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by William Brogden:
I think they say float or double because double would also work. The result of the expression would be float but that can be cast to double.
Bill

___________________________________________________________
Thanks william, But i think the answer is very unclear.
Becuase , in case i get a problem where the answer is only short(well this is an eg) , then should my reply be short, int, long, float and double just because short can be casted to an int , long , float and double too???
Thanks once again for your attention

Greenhorn
Posts: 22
• Number of slices to send:
Optional 'thank-you' note:
hi,
i think you're right, though don't forget the automatic promotion rules when looking at expressions, it seems most smaller than int expressions will promote to size int, so usually answer will be int, long, float, double, unless you're dealing with a single term ( I may be corrected here )
e.g
b=25
I would answer b could be byte, char, short, int, float, double.
An interesting question would be
b=32768?
char, int, float, double.
just include all types bigger than the smallest type of the result.
cheers.

 Consider Paul's rocket mass heater.