2
#include <iostream>
 using namespace std ;
  int main ()
  {

     int x,y ;
     x= 7 , y=3 ;
      cout<<float(x) /y << endl ; 

  }

What is the difference between float(x)/y and x/float(y)?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Gzat Shrit
  • 91
  • 1
  • 5

3 Answers3

2

There is no practical difference in your question.

The result of dividing two integers is an int (this is called integer division and what you get if you omit the float conversion entirely); if you then cast to a float you still have the int result, albeit as a float. The result of dividing a float and an int is a float; so you aren't promoting the int result to a float.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

There was a similar question involving uint8_t and bools in an arithmetic expression.

The answers to that question apply here also, with the following additional information relevant to floats.

5 Expressions

9 Many binary operators that expect operands of arithmetic or enumera- tion type cause conversions ...

--Otherwise, if either operand is float, the other shall be converted to float.

Both the operands are converted to float before the division and the resultant is of type float.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Very little. With float(x)/y the value x will first be converted to float and then to double while y will be converted directly to double. With x/float(y), x will be converted directly to double while y will be converted to float and then double. In either case, the division will be carried out as double and the double result will be printed.

So for your example, there is no difference, but if x or y was larger (greater than 224), then the value would be rounded slightly to fit in a float if it was first converted to float. This would then give a (very) slightly different result.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    Where did `double` dcome from? Expressions with `float` operands are evaluated within `float` type. `float` is not promoted to `double` unless one of the operands is `double` already. – AnT stands with Russia Sep 18 '14 at 22:24
  • They may be (and generally are for all except for some embedded compilers) converted to double first. – Chris Dodd Sep 18 '14 at 22:25
  • 1
    @ChrisDodd, The C++ standard says otherwise (as part of the usual arithmetic conversions performed on both operands). *If either operand is of type long double, the other shall be converted to long double. — Otherwise, if either operand is double, the other shall be converted to double. — Otherwise, if either operand is float, the other shall be converted to float.* – chris Sep 18 '14 at 22:25
  • 1
    @Chris Dodd: The language does not "convert them to `double`". If you are talking about some hardware-specific behavior, then it will be some hardware-specific internal representation (like 10-byte FP type on x86 platforms), not `double`. If some implementation uses `double` as an intermediate type, it is OK, but that would be implementation-specific behavior. – AnT stands with Russia Sep 18 '14 at 22:26
  • thank you chris dodd but can you give me the source of this information – Gzat Shrit Sep 18 '14 at 22:28
  • I guess this comes under the heading of "compilers may use any level of precision they please for float/double/long double" as otherwise gcc fails to follow the standard. In particular, depending on optimization level and other things it sometimes converts directly to double (ignoring the explicit `float`) and other times does not... – Chris Dodd Sep 18 '14 at 22:42