416 views
1 votes
1 votes
If f is a float and n an int, then the expression

(n > 0) ? f : n

is of type float regardless of whether n is positive.

Why?

1 Answer

0 votes
0 votes
Information loss is a major concern. For an operator with operands of different types the operands are converted to a common type, generally from a narrower to a wider operand without losing information. In simple terms more number of bits to represent the information.

"If f is a float and n an int, then the expression
(n > 0) ? f : n
is of type float regardless of whether n is positive."

Here the n gets converted to the type float and regardless of what n is the result is of type float.
One can read rules of type conversion from the standard. ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf  , page 129)

Related questions

1 votes
1 votes
0 answers
2