You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1
in the expression you have above is int
, and the type of x
is int. When you do int / int
, you get an int back. You need at least one of the types involved to be floating point (float
or double
) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double
.