Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the sum of an int and a float an int?

Consider the following code:

float d  = 3.14f; int   i  = 1; auto sum = d + i; 

According to cppreference.com, i should be converted to float when it is added to d. However, when I actually run the code, I find that sum is 4. Why does this happen?

Interestingly, when I explicitly put my compiler into C11 mode, I found that sum was 4.14. What rules does the C11 standard change that affect the outcome?

What would happen if I compiled the same code using a C++ compiler?

like image 242
jfxu Avatar asked Jul 31 '17 12:07

jfxu


People also ask

What happens when you add an int and a float?

Yes, an integral value can be added to a float value. The basic math operations ( + , - , * , / ), when given an operand of type float and int , the int is converted to float first. So 15.0f + 2 will convert 2 to float (i.e. to 2.0f ) and the result is 17.0f .

Can we add int and float in Python?

Can mix integers and floats freely in operations. Integers and floating-point numbers can be mixed in arithmetic. Python 3 automatically converts integers to floats as needed.

Why use integers over floats and not just floats all the time?

Another reason to favour integers over floats is performance and efficiency. Integer arithmetic is faster. And for a given range integers consume less memory because integers don't need to represent non-integer values. Another reason is to show intent.

Does sum work on floats Python?

fsum() method to sum a list of float numbers in Python, e.g. total = math. fsum(list_of_floats) . The math. fsum() method returns an accurate sum of the floating-point values in the iterable.


2 Answers

In C (and C++), 3.14f + 1 is a float type due to type promotion of int to float.

But in C, up to and including C90, and such a standard may well possibly be your C compiler default, this is assigned to an int type, yielding 4, since int is the default type for a variable with automatic storage duration. From C99 onwards, compilation will fail as implicit int was withdrawn, although compilers might still permit it, with a warning.

(In C++11 and later, auto instructs the compiler to deduce the type. sum will be a float with value 3.14f + 1. Compiling as C++98 or C++03 may still work, but generate a warning about C++11 extensions. This is what clang does, for example. This re-defining of auto in C++11 represents another material divergence between C and C++.)

like image 198
Bathsheba Avatar answered Sep 19 '22 16:09

Bathsheba


It's rather simple really.

In old versions of C (before C99), you could write something like

auto n = 3; 

and n would be an int type with the value 3. You could also write

auto n = 3.14f; 

and n would still be an int type, with a value 3.

This was called implicit int and K & R made it quite famous.

So can you see that

auto sum = d + i; 

merely assigns the float type d + i to sum which is an implicit int.

Hence the answer 4.

In newer versions of C (C99 onwards), implicit int was dropped.

like image 32
P45 Imminent Avatar answered Sep 23 '22 16:09

P45 Imminent