0

The assignment given to me requests all result after calculation must be "rounded up". So I've thought about using ceil. However in some circumstances, the result wasn't as i expected. Here is an example:

int a; cin>>a; //a=100
double b;
b=ceil(a*1.1);
cout<<b;

The result should be 110. But what i've got was 111. I don't khow why it came out like that and this also happen with numbers whose last digit is 0. Please explain it to me and if there is any way to fix i would be grateful if you could tell me.

  • 1
    Check what `a*1.1` is equal to, before `ceil()`ing it. If it has any fractional part, it'll get rounded up. And if so, that's just floating point I'm afraid, as the linked question shows. – underscore_d Mar 10 '21 at 09:44
  • 1.1 can't be represented exactly in binary floating point - the nearest double is slightly more than that. There must be some misunderstanding or detail you're missing about the assignment. – molbdnilo Mar 10 '21 at 09:45
  • Thanks you all. I've figured out the way to fix. Since 1.1 can't be represented exactly, so instead of a*1.1 I did this a*(1.1-0.00001) and the result came out the same as I expected. – Cao Minh Quang Mar 11 '21 at 10:22

0 Answers0