3

For example:

int number1 = 1, number2= 2;
float variable = (float)number1/(float)number2;

Instead of this, Why can't we use "float" only once? For example:

int number1 = 1, number2= 2;
float variable = (float)(number1/number2);
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Hakan
  • 97
  • 1
  • 10
  • 1
    Because integer division truncates. Use `printf` to display the results from both calculations, and you'll find that the first result is `0.5` and the second result is `0.0`, since `1/2` truncates to `0`. – user3386109 Dec 28 '14 at 06:55
  • 1
    Well, `number1/number2` is integer division. In fact, the `(float)` in the second example makes no difference. – juanchopanza Dec 28 '14 at 06:55
  • 3
    Because the divison operation would have truncated the decimal places before the conversion to float. – ABCD Dec 28 '14 at 06:55
  • The evaluation of a subexpression generally is not affected by the context in which it appears. Since `number1` and `number2` are both of type `int`, `(number1/number2)` is evaluated using `int`-by-`int` division. Casting the result to `float` doesn't change that. – Keith Thompson Dec 28 '14 at 07:19
  • Very related: [c - Why did my float get truncated? - Stack Overflow](https://stackoverflow.com/questions/35097935/why-did-my-float-get-truncated) – user202729 Feb 12 '21 at 03:16

4 Answers4

11

The objective is to avoid the truncation that comes with integer division. This requires that at least one of the operands of the division be a floating point number. Thus you only need one cast to float, but in the right place. For example,

float variable = number1/(float)number2; // denominator is float

or

float variable = ((float)number1)/number2; // numerator is float

Note that in the second example, one extra set of parentheses has been added for clarity, but due to precedence rules, it is the same as

float variable = (float)number1/number2; // numerator is float, same as above

Also note that in your second example,

float variable = (float)(number1/number2);

the cast to float is applied after the integer division, so this does not avoid truncation. Since the result of the expression is assigned to a float anyway, it is the exact of

float variable = number1/number2;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

You can write either expression, but you get different results.

With float variable = (float)(number1 / number2); the value in variable is 0, because the division is done as integer division, and 1/2 is 0, and the result is converted.

With float variable = (float)number1 / (float)number2;, the value in variable is 0.5, because the division is done as floating point division.

Either one of the casts in float variable = (float)number1 / (float)number2; can be omitted and the result is the same; the other operand is converted from int to float before the division occurs.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Since number1 and number2 are ints, the division performed will be integral division. Thus, number1/number2 will evaluate to the int 0. To do floating point arithmetic instead, you need to cast them. Note that simply casting one will suffice, since the other one will be implicitly promoted. So, you can just say ((float)number1)/number2.

Pradhan
  • 16,391
  • 3
  • 44
  • 59
0

In the First case,

1/2 results 0

In the second case, you can use float once, but it has to be applied with one of the numbers before division

1.0/2.0 or 1.0/2 or 1/2.0 results 0.5

sagar pant
  • 377
  • 1
  • 2
  • 12