Closed JohanCarlsen closed 1 year ago
Never mind, I solved it. I had to do:
double num1=1, num2=5;
double dt = num1/num2;
Hi @JohanCarlsen,
You've encountered what's called integer division. You can read about the mathematics here: https://mathworld.wolfram.com/IntegerDivision.html Short story is that with integer division, any fractional part (remainder) is discarded.
C++ will interpret both 1
and 5
as integers, and thus perform integer division. With integer division 1/5 = 0, so what's happening is that you have effectively written
double dt = 0;
However, if you include a decimal point for either 1 or 5 (or both), i.e. write 1.
or 5.
(or both), C++ will perform regular division of floating-point numbers. So this code will give you what you expect:
double dt = 1./5;
std::cout << dt << endl;
Hello, I'm trying to write the following:
double dt = 1/5;
std::cout << dt << endl;
But here, instead of dt being 0.2, it is 0.. Why is that?