I divide two numbers on each other, for example, 1 / 2 and expect to get 0.5 as a result, since I assign the result to a variable with a floating point, but I get 0 . Here is the code:

 #include <iostream> int main() { double d = 1 / 2; std::cout << d << "\n"; } 
 0 

What could be the problem?

1 answer 1

The fact is that in C ++, as well as in C, C #, Java and many other languages, the division of integers gives exclusively the same result, i.e. the fractional part is simply discarded.

The language standard literally says that [expl.mul] :

For integral operands of the operator with any fractional part discarded;

This behavior is primarily due to performance, because integer arithmetic usually works faster than floating point arithmetic.

The fact that the result of dividing integers is assigned to a variable with a floating point does not affect the division itself, since assignment is performed after the fact of division. And we simply initialize the real variable d integer value 0 , which does not give us anything with a non-zero fractional part, for example, the expected 0.5 .

In order to achieve the desired result, it is necessary to make a real division. To do this, it suffices to at least one of the operands lead to a real type. This can be done in different ways. If a constant is used, it is enough to add a dot at the end of it, for example:

 double d = 1 / 2.; 

or

 double d = 1. / 2; 

It looks minimalistic, but sometimes a bit strange, because in mathematics, this record is not used. For greater clarity, add .0 , we get:

 double d = 1.0 / 2; 

If instead of an explicit numeric literal, a named integer variable is used, or a function that returns an integer type, then adding a point with zero does not solve the problem, and you need to perform a different type conversion, i.e. do static_cast :

 int one = 1; int two() { return 2; } double d = static_cast<double>(one) / two(); 

But you can use the multiplication (or division) of any operand by 1.0 , which also converts the result, which will act as the operand of the subsequent division, to a real type:

 double d = (one * 1.0) / two(); 

An alternative would be to add (or subtract) a real zero:

 double d = (one + 0.0) / two(); 

However, the option with static_cast will be more correct, because Such transformations are always easier to find in the program text than ordinary floating constants.

And of course, if we are talking about variables or functions, then you can simply change their type:

 double one = 1; double two() { return 2; } double d = one / two(); 

In such a situation, we must not forget that using one and two in other contexts may also give different results as they were before. Therefore, it is better to localize the task, and not to do everything in a program of a real type, if this is required only in some special cases.

  • You can add more (1 + .0) / 2 and analogues. It is good that it works similarly to the floating type caste and with variables too (although there was already a similar example with multiplying by 1.0). - avp
  • @avp added this option. I won't add log(exp(N)) :) - αλεχολυτ
  • But what about (double)one ? - Qwertiy
  • @Qwertiy ¡C no pasarán! - αλεχολυτ