Why doesn't separating two ints give the correct value when assigning a double?

How it happens in the next fragment

int a = 7; int b = 3; double c = 0; c = a / b; 

c ends up with a value of 2, not 2.3333, as you would expect. If a and b are doubles, the answer goes to 2.333. But of course, because c already double, should it work with integers?

So why int/int=double does not work?

+77
c ++ variables double integer-division
Sep 27 '11 at 15:02
source share
8 answers

This is because you are using the integer division version of operator/ , which takes 2 int and returns int . To use the double version that returns double , at least one of int must be explicitly lebaned into double .

 c = a/(double)b; 
+122
Sep 27 '11 at 15:05
source share

There he is:

a) Separating two int always performs integer division. So the result of a/b in your case can only be int .

If you want to save a and b as int s, but separate them completely, you must make at least one of them double: (double)a/b or a/(double)b or (double)a/(double)b .

b) c is double , so it can take the value int when assigned: int automatically converted to double and assigned c .

c) Remember that when assigning, the expression to the right of = calculated first (in accordance with rule (a) above and without taking into account the variable to the left of = ), and then the variable to the left of = (according to (b) above) is assigned. I believe this completes the picture.

+11
Sep 27 '11 at 3:15 a.m.
source share

With very few exceptions (I can only think of one thing), C ++ defines the whole meaning of an expression (or subexpression) from the expression itself. What you do with the results of the expression does not matter. In your case, there is no double in the expression a / b ; all int . Therefore, the compiler uses integer division. Only after he has a result, he considers what to do with it, and convert it to double .

+9
Sep 27 '11 at 3:11
source share

When you divide two integers, the result will be an integer, regardless of what you store it in double.

+6
Sep 27 '11 at 15:06
source share

c is a double variable, but the value assigned to it is an int value because it arises from the division of two int s, which gives you "integer division" (discarding the remainder). So what happens on line c=a/b is

  • a/b is evaluated by creating a temporary int type
  • the temporary value is assigned to c after conversion to double .

The value of a/b is determined without reference to its context (assignment double ).

+5
Sep 27 '11 at 15:04
source share

The / operator can be used for integer division or floating point division. You give it two integer operands, so it performs integer division, and then the result is stored in double.

+4
Sep 27 '11 at 15:07
source share

In C ++, the result of sub-exposure is never affected by the surrounding context (with some rare exceptions). This is one of the principles that should be followed in this language. The expression c = a / b contains an independent subexpression a / b , which is interpreted independently of anything outside this subexpression. The language does not care that you later assign the result to double . a / b is integer division. Everything else does not matter. You will see that this principle is respected in many corners of the language specification. This talks about how C ++ (and C) works.

One example of an exception that I mentioned above is the assignment / initialization of a function pointer in situations with function overloading

 void foo(int); void foo(double); void (*p)(double) = &foo; // automatically selects `foo(fouble)` 

This is one of the contexts where the left side of the assignment / initialization affects the behavior of the right side. (In addition, initializing the reference array prevents decay of the array type, which is another example of similar behavior.) In all other cases, the right side completely ignores the left side.

+4
Sep 27 '11 at 16:20
source share

It technically depends on the language, but almost all languages ​​relate to this issue the same way. When there is a type mismatch between two data types in an expression, most languages ​​will try to pass data on the one hand = to match the data on the other hand according to a set of predefined rules.

When dividing two numbers of the same type (integers, doubles, etc.), the result will always be of the same type (so that "int / int" will always be int).

In this case, you have double var = integer result which, after calculating, calculates the integer result in double, in which case fractional data is already lost. (most languages ​​will do this casting to prevent type inaccuracies without throwing an exception or error).

If you want to save the result as double, you will want to create a situation in which you double var = double result

The easiest way to do this is to force the expression on the right side of the equation to double reset:

c = a/(double)b

Separating between an integer and a double result will cause the integer to be double (note that when doing math, the compiler often β€œjumps” to the most specific data type to prevent data loss).

After overheating, a will end as a double, and now you have a separation between the two doubles. This will create the desired division and purpose.

AGAIN,, please note that this is specific to the language (and may even be specific to the compiler), however, almost all languages ​​(of course, everything that I can think of from the head) consider this example to be identical.

+2
Sep 27 2018-11-11T00:
source share



All Articles