I'm quite new to c++,and I'm wondering whether you can add different number types together,like this:
int num1=1;
float num2=1.0;
double num3=1.0;
can you add these variables together?If you can,what type would
num1+num2+num3
be?
I'm quite new to c++,and I'm wondering whether you can add different number types together,like this:
int num1=1;
float num2=1.0;
double num3=1.0;
can you add these variables together?If you can,what type would
num1+num2+num3
be?
The answer is double. if you want to test it, you can try auto ret = num1+num2+num3
ans see that type ret
has.
As already said, the answer will be double
.
What the compiler will do for this (without optimization) is
You need to take some care with these conversions. Whilst you can convert float
(and int
) to double
without any loss of precision, you can't always do the same with int
to float
.
float
has 24 bits of precision, which means that it can exactly represent all integers up to about 16.8 million, while a signed int
can go up to about 2 billion. See here for details.
[I'm assuming the LP64 model]
Yes of course... the result will give a float 1+1.0+1.0=3.0 since double