+ 2

Where is the bug?

#include <iostream> #include<cmath> using namespace std; int main() { int color; float fixed = 40.0; cin >> color; float cost = (5.0 * color) + fixed; cost += cost * 0.10; cout << ceil(cost); return 0; }

27th Jun 2025, 3:16 AM
Md Mehedi Hasan
Md Mehedi Hasan - avatar
7 Respuestas
+ 5
Afnan Irtesum Chowdhury ceil(123.0) gives 123.0, and cout << ceil(123.0) prints 123 visually. So it seems like (int) shouldn't matter. But it does — because ceil() returns a double, not an int. cout formats it nicely, but internally it's still a double. Some platforms compare exact output, and double can carry hidden `.000...` parts. Casting to (int) ensures clean integer output and avoids precision issues. So (int)ceil(...) isn't just about visuals — it's about avoiding hidden float behavior.
27th Jun 2025, 4:48 PM
Chitranshi Gupta
Chitranshi Gupta - avatar
+ 3
Md Mehedi Hasan I added explicit int conversion in the cout statement as: cout << (int)ceil(cost); And it worked. But I can't really understand what the issue is. The result normally returns an integer anyway(and so it passes 4 of the 5 test cases). The error is, to my understanding, due to the cost being a certain floating number, that when converted with ceil, rounds up to a number it shouldn't. Maybe because of floating point precision, but I'm not sure either. I did check by using double instead of float, but that didn't fix the issue like (int) conversion did.
27th Jun 2025, 4:36 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 2
I solved this and passed all the test cases #include <iostream> #include <cmath> using namespace std; int main() { int color; cin >> color; double fixed = 40.0; double variable = 5.0 * color; double total = fixed + variable; total += total * 0.10; // adding 10% tax cout << (int)ceil(total) << endl; return 0; } Afnan Irtesum Chowdhury Md Mehedi Hasan
27th Jun 2025, 11:49 AM
Chitranshi Gupta
Chitranshi Gupta - avatar
+ 2
this exercise is a 'code coach' from the community section in sololearn app. the requirement for output says: Output Format: A number that represents the cost of your purchase rounded up to the nearest *whole* number. there for (in some cases depending on the input number) we need an int casting.
27th Jun 2025, 11:59 AM
Lothar
Lothar - avatar
+ 1
Lothar The thing is that cout << ceil(cost) outputs an integer(ceil returns a double with a trailing zero like 7.0, and cout removes trailing zeros so that becomes 7. So a number like 6.8 becomes 7 when rounded up, just like the task specifies), it's some issue with the 5th test case that's failing without int conversion, even though the result of ceil(combined with cout) is an int anyway. So if, for example, ceil(combined with cout) returns 154, we are converting the number 154 to an integer(which seems redundant)
27th Jun 2025, 12:02 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 1
Chitranshi Gupta I know, (int) fixes it, I also said it. The confusion is how(and why) it works. Since, cout << ceil(cost) [Without the (int), just like Mehedi's code] outputs an integer anyway, and it passes all the test cases except the last one. There seems to be no use of (int) here as the result is already an integer(ceil returns {n} such as 8.0 and cout makes that 8 by removing trailing zeros), but for some reason, this (int) makes the code pass the last test case.
27th Jun 2025, 2:16 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar