+ 2

Math.Ceiling Problem with a challange

It has passed all the cases but two (Case 2, Case 5) In Case 2 the inputs are: 6 and 3 Isn't Math.Ceiling supposed to round 3.6 up to 4? Why isn't it working like that here? https://code.sololearn.com/cLNfNzXP3ERR/?ref=app

6th Aug 2021, 9:53 PM
Aykut AKSAKAL - avatar
2 Answers
+ 4
Your problem isn't with ceil, but a little problem having to do with type casting and how types interact within expressions. When you divide an integer by an integer, your result, remainder or no, will be an int, with the fractional portion truncated. This means that the fractional part of your number will be discarded. For example, 3/2 would normally be 1.5, however we leave off the fractional part for a result of 1. To fix this, you need either the numerator or the denominator to be a double type, otherwise this will continue to happen. Using (double)door/duct or door/(double)duct would suffice. This will stop the truncation because one of the types will become a double, and a int-double expression will result in a double.
6th Aug 2021, 11:07 PM
BootInk - avatar
BootInk Thanks a lot for real. I have completed the C# course but I dont remember such a thing as putting (double) or any other type there in lessons.. Either I missed or the lessons don't include the info. Thank you really much! Appreciated.
6th Aug 2021, 11:41 PM
Aykut AKSAKAL - avatar