Well, I tried out a couple of lines but it seems not to work.
double equation= ( ((double)avg-(double)cur_avg) / (1/(double)n) );
cout<<"equation is "<<equation<<endl;
lld needed = ceil(equation);
cout<<"needed is "<<needed<<endl;
Here avg and n are integers of type long long int. And cur_avg is already a double. Also avg=4, n=5 and cur_avg=3.4 . The output in the terminal reads as follows:
output:
equation is 3
needed is 4
Now, my question is if "equation" is 3 then ceil(equation) should have been 3 as well, but why it reads 4 then?
In short, what I require is this:
output:
equation is 3
needed is 3
Also, how to modify the above code snippet to get this result?
Edit:
Thank you Sebastian for proposing the solution in the comments.
An explanation of this behavior could be found here
A probable solution is to subtract a small epsilon when calling ceil:
double equation= ( ((double)avg-(double)cur_avg) / (1/(double)n) );
cout<<"equation is "<<equation<<endl;
lld needed = ceil(equation - .000001);
cout<<"needed is "<<needed<<endl;