• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

float or double

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks in advance
Padmini
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Padmini.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic