I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!
In the expression 1 / 6
, both numbers are integers. This means that this division will perform integer division, which results in 0
. To do a double
division, one number has to be a double: 1.0 / 6
for example.