When I add two parentheses in this float ,the output becomes zero.
int main()
{
float a=12.00*(20/100);
cout <<a<<endl;
}
If I remove the parentheses, the output will be 2.4 but if I kept it, the output will be zero . Why ???
When I add two parentheses in this float ,the output becomes zero.
int main()
{
float a=12.00*(20/100);
cout <<a<<endl;
}
If I remove the parentheses, the output will be 2.4 but if I kept it, the output will be zero . Why ???
12.00*20/100
is parsed as (12.00*20)/100
and is evaluated:
12.00
contains a decimal point; it is a double
with value 12.20
is an int
with value 20.12.00*20
, the int
20 is converted to double
, yielding a double
with value 20.double
values 12 and 20 are multiplied, producing a double
value 240.(12.00*20)/100
is dividing a double
240 by an int
100.int
100 is converted to double
100.double
240 is divided by the double
100, producing a double
value of approximately 2.4 (exactly 2.399999999999999911182158029987476766109466552734375 when IEEE-754 binary64 is used).In contrast, 12.00*(20/100)
is evaluated:
20
and 100
are both int
values, so 20/100
is performed with int
division. int
division produces an int
result with the fraction discarded. So the result is the int
value 0.12.00*(20/100)
is multiplying the double
value 12 by the int
value 0, which produces 0.In summary, two things are at play:
a*b/c
where the two operators *
and /
have otherwise equal precedence, they are structured to perform the left operation first. (This is the rule for multiplicative operators. Some operators, such as assignment, associate right-to-left.)int
operands are done with int
arithmetic, even if the overall expression contains double
operands somewhere else. In multiplications with mixed int
and double
operands, the int
operand is converted to double
.When you use
float a=12.00*(20/100);
the term (20/200)
is computed first before the result of that term is multiplied with 12.00
. That's because the parenthetical term has higher precedence than the multiplication operator. 20/100
is computed using integer division, which results in 0
.
When you use
float a=12.00*20/100;
The term 12.00*20/100
is evaluated as (12.00*20)/100
since the multiplication operator and division operator have the order of precedence and they have left to right associativity. Those operations are performed by promoting 20
and 100
to double
. Hence, you get the expected answer.
I think one important thing worth mentioning here which is highly recommended in mordern C++, Integer and Floating point Literals.
As already mentioned in most answers, by default in C++ a numeric with no literals is referred as integer types. But in mordern C++ you can be specific about the type using floating point literals "f".
Thus, in this way you know how you can explicitly let compiler know what type of numericals you are tying to use and get your expected result.
In below case, (20.0f/100.0f) is not refered as integer type any more, and here I'm explicitly specifying compiler to treat these numericals as floating point types.
(Remember even using 20.0 is by default treated as double types and not float types.)
Example, try this:
#include <iostream>
int main()
{
auto a = 12.0f * (20.0f/100.0f);
std::cout <<a<<std::endl;
}