## In C, are the arithmetic operators the same for integers and floating points?

arithmetic operators in c

adding an integer to a double c

logical operators in c

what is the result of a relational operator

relational operators in c

adding int and float in java

real arithmetic in c

**This question already has answers here**:

Since 'f' is a float, I expect the result of '3 / 2' to be 1.5 (as in the second example), but somehow it is the result of an integer division.

Yes, the result of the operation is an integer because the two operands (the literal values) are integers. The reference for C Arithmetic Operators explains:

The binary operator / divides the first operand by the second (after usual arithmetic conversions) following the usual arithmetics definitions, except that when the type after usual arithmetic conversions is an integer type, the result is the algebraic quotient (not a fraction), rounded in implementation-defined direction (until C99) truncated towards zero (since C99)

So you will get integer arithmetic unless one of the operands is a float, in which case type promotions come into effect. You can use a suffix to make a literal into a float, or use a cast to make a variable into a float.

I therefore conclude that there are two different types of division: A integer division of form [int] / [int] = [int] and a float division [float] / [float] = [float].

Yes, that is essentially correct: the types of the operands to the operators determine the resulting type of the expression. (It is not determined by the type of assignment.)

**Arithmetic Operators in C,** An operation between two floating point operands always yields a floating point result. Let's take two The following table shows the result of arithmetic operations performed on a and b . int main(). Floating-point types in C support most of the same arithmetic and relational operators as integer types; x > y, x / y, x + y all make sense when x and y are floats. If you mix two different floating-point types together, the less-precise one will be extended to match the precision of the more-precise one; this also works if you mix integer and floating point types as in 2 / 3.0 .

In C, there are constants and literals. These are numeric constants (not to be confused with `const`

variables), and they can have suffixes that specify the type of number it is. An `f`

as in `2.1f`

informs the compiler that this is a `float`

constant, meanwhile, plain `2.1`

is a `double`

constant.

The type of the operands determine the type of the result of an arithmetic expression. So, even if mathematically `3 / 2 == 1.5`

, to a C compiler it means `3 / 2 == 1`

since the operands are integers, and a truncation is performed.

Your conclusion is thereby correct.

**5.2 — Arithmetic operators,** As with all floating point arithmetic operations, rounding errors may occur. Prior to C++11, integer division with a negative operand could round up or down. a cake, each person recieves 0 pieces of a cake), same with the modules (%), you � The reason is that floating-point values and integers are handled differently inside the computer. An integer exists inside the computer as a true binary value. For example, the value 123 is stored in modern computers as a 32-bit value: A true binary value.

Is this the actual way how C "interprets" arithmetic operations?

It's more complex.

First, types narrower than `int/unsigned`

are promoted `int`

(or `unsigned`

if the value may exceed `INT_MAX`

) . Then prior to arithmetic operator evaluation, the arguments are promoted to a *common* type. The lower ranking type is promoted to the higher ranking type: `int, unsigned, long, unsigned long, long long, unsigned long long, float, double, long double`

** and onto `complex`

is needed.

Note that the type of the left of the `=`

has nothing to do with the math operation on the right.

Constants like `2.0`

are type `double`

and `2.0/3.0`

form a `double`

quotient. Since 2.0/3.0 is not exactly representable, the result is rounded to a nearby `double`

- before assignment. If the result is stored in a `float`

, another rounding may occur.

I therefore conclude that there are two different types of division:

Consider more than 2 different possible quotients who arguments only differ in type.

int main() { printf("%.17f\n", (double) (2/3)); printf("%.17f\n", (double) (2/3.0f)); printf("%.17f\n", (double) (2/3.0)); // long double may have yet another quotient. }

Output

0.00000000000000000 0.66666668653488159 0.66666666666666663

Note: Floating point may evaluate to a higher rank than either operand depending on `FLT_EVAL_METHOD`

.

** This is a slight over simplification.

**Mixing integer and floating point numbers in an arithmetic operation,** A computer can only operate on data of the same data type. Mixing integer and floating point numbers in an arithmetic operation. Java's automatic conversion in a� Floating-Point Numbers. Floating point numbers have a decimal point that is not fixed. In fact, it can be located anywhere in the number sequence such as with 3.199993, 319.9993, and 3199.993.

try this

f = 3 / 2; printf("%f\n",f); //1.000000 f = (float)(3 / 2); printf("%f\n",f); //1.000000 f = (float)3 / 2; printf("%f\n",f); //1.500000 f = 3 / (float)2; printf("%f\n",f); //1.500000 f = 3 / 2.0; printf("%f\n",f); //1.500000 f = 3.0 / 2; printf("%f\n",f); //1.500000 f = 3.0 / 2.0; printf("%f\n",f); //1.500000

Parse this like a compiler would. Yes f is declared as a float, but the number 3 without a decimal point is an integer. When there is a decimal point then C dictates that is a double. In order to perform an operation the operands are promoted to the highest/largest/most precise-ish, whatever, float is above int, double is above float.

f = 3 / 2; printf("%f\n",f); //1.000000

3 and 2 are integers the division happens first as an integer division result of 1 then the equals is done second the 1 needs to be promoted to a float.

f = (float)(3 / 2); printf("%f\n",f); //1.000000

same as above the division comes first it is isolated within the parenthesis so integer division, then it is promoted to float, the equals is next and the result is already promoted to float.

f = (float)3 / 2; printf("%f\n",f); //1.500000

3 is promoted to float first so in order to perform the division 2 has to be promoted to float, the division is done as a float and then the storage to f it is already float.

f = 3 / (float)2; printf("%f\n",f); //1.500000

same as above 2 is promoted from an int to float then the division has to be float so 3 is promoted.

f = 3 / 2.0; printf("%f\n",f); //1.500000

2.0 is a double as written like this in C so 3 is promoted to a double, the division is double. that result has to be converted from double to float then stored to f as a float.

f = 3.0 / 2; printf("%f\n",f); //1.500000

same as above 3.0 is a double, 2 is promoted, division is double, then converted to float.

f = 3.0 / 2.0; printf("%f\n",f); //1.500000

both are double.

Now I just added this one:

f = 3.0F / 2; printf("%f\n",f); //1.500000

adding the F makes 3 a single float, 2 is promoted to float, division is float then the answer is stored in f.

Unless you do no optimization, most of these "conversions" are optimized out where possible, if you compiled the above or your program with optimizations on the compiler is going to simply generate a list of printfs of 1.0f or 1.5F. The comments describe how to read/interpret the code, how the compiler is going to read that code and functionally implement it.

**C/FloatingPoint,** 3. Operators. Floating-point types in C support most of the same arithmetic and relational operators as integer types; x > y, x / y, x + y all make sense when x and y are floats. In above program, variable c has float data type and program prints c = 0.555556, excepted output. Reason: in this expression c = 5.0 / 9, the / is the arithmetic operator, 5.0 is floating-point operand and 9 is integer operand. As per the 2nd Rule before the operation is done the integer operand is converted into floating-point operand.

**2.5 Arithmetic Operators,** The basic operators for performing arithmetic are the same in many computer the division operator yields a floating-point result, with a potentially nonzero The modulus operator % gives you the remainder when two integers are Some languages have an exponentiation operator (typically ^ or **), but C doesn't. By default, arithmetic operations occur in an unchecked context. Floating-point arithmetic overflow. Arithmetic operations with the float and double types never throw an exception. The result of arithmetic operations with those types can be one of special values that represent infinity and not-a-number:

**[PDF] Chapter 7 Arithmetic,** The arithmetic operators in C++ are as follows. + (addition) integer and floating point values in the same expression this is called mixed mode arithmetic. C++ Arithmetic Operators. Perhaps you have warm memories of doing arithmetic drills in grade school. You can give that same pleasure to your computer. C++ uses operators to do arithmetic. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus.

**3. Variables, Data Types, and Arithmetic Expressions,** C has a rich variety of math operators that you can use to manipulate your data. The double type is the same as type float , only with roughly twice the precision. Every value, whether it's a character, integer, or floating-point number, has a� In these cases C++ casts the result to an int, that screws up the whole calculation. No, in these cases since both operands are integers you explicitly perform integer division; no casting takes place at all. The correct answer would be to perform floating-point division: 1.0/2 It isn't that much extra effort to write the decimal point, is it?

##### Comments

- While seeking knowledge is a good thing, you are supposed to do some research before posting a question. A search for "c integer division" yields thousands of relevant links. Please read How to Ask And yes, your assumptions are correct.
- Read en.cppreference.com/w/c/language/conversion, specifically Usual arithmetic conversions. It applies to division.
`3.0`

and`2.0`

are`double`

, not`float`

.- You may be overthinking things. In C, the default numeric type is
`int`

, so absent something designating a number as something other than`int`

(like a cast or suffix), the number will be treated as an`int`

. - Only 2 types?
`2.0 / 3.0`

may differ from`2.0f / 3.0f`

. - True, but the distinction being made was integer division versus floating point. The
`3.0`

vs`3.0f`

is a matter of precision, not truncation. - @chux You're right, they're literals in C++, not in C. Just checked the reference docs and edited the post.